package test.tbltree;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ClientContext;
import com.ulcjava.base.application.ULCBorderLayoutPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCFlowLayoutPane;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCPollingTimer;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTableTree;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.tabletree.ITableTreeModel;
import com.ulcjava.base.application.tree.TreePath;
import com.ulcjava.base.application.util.Color;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.shared.UlcEventConstants;


public class MyTableTreeApp extends AbstractApplication{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ULCTableTree tableTree = null;
	TreeNode root = null;
	TableRowModel model = null;
	int nodecount = 0;
	int NUMROOTCHILD = 75;  //num of children off of root
	boolean method1 = false; //which method of expansion to use

	public MyTableTreeApp(){
	}
	
	public ULCComponent initialize(){
		ULCBorderLayoutPane p = new ULCBorderLayoutPane();
		tableTree = new ULCTableTree();
		tableTree.setShowsRootHandles(true);
		tableTree.setRootVisible(true);
		tableTree.setModel(createTableTreeModel());
		ClientContext.setModelUpdateMode(model,
				UlcEventConstants.SYNCHRONOUS_MODE);
	    ULCScrollPane scroll = new ULCScrollPane(tableTree);
	    
	    ULCButton b = new ULCButton("Expand All");
	    b.addActionListener(new IActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				TreePath path = tableTree.getPathForRow(0);
				expandAll(tableTree, path, true);
				
			}});
	    ULCFlowLayoutPane flowPane = new ULCFlowLayoutPane(ULCFlowLayoutPane.LEFT);
	    flowPane.setBackground(Color.white);
	    flowPane.add(b);
	    
	    p.add(flowPane,ULCBorderLayoutPane.NORTH);
		p.add(scroll,ULCBorderLayoutPane.CENTER);
		
		return p;
		
	}
	
	private ITableTreeModel createTableTreeModel(){
		root = new TreeNode(1);
		for(int i=0;i<NUMROOTCHILD;i++){
			TreeNode n = new TreeNode(i);
			root.addChild(n);
			addChildren(n, 10);
		}
		model = new TableRowModel(root,new String[]{"Col1","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13","Col14","Col15","Col16","Col17","Col18","Col19","Col20","Col21","Col22","Col23","Col24","Col25","Col26","Col27","Col28","Col29","Col30","Col31","Col32"});
		return model;
	}
	
	private void addChildren(TreeNode parent, int numChild){
		if(numChild>0){
			for(int i=0;i<numChild;i++){
				TreeNode n = new TreeNode(i);
				parent.addChild(n);
				addChildren(n, numChild-5);
			}
		}
	}
	
	
	 public static void main(String[] args) {
	        DevelopmentRunner.setApplicationClass(MyTableTreeApp.class);
	       // String myArgs[] = new String[0];
	        //DevelopmentRunner.main(myArgs);
	        DevelopmentRunner.checkApplicationClass();
	        DevelopmentRunner.run();
	    }
	 
	 public void start(){
		 MyTableTreeApp t = new MyTableTreeApp();
		 
		 ULCFrame frame = new ULCFrame("tableTree");
	     frame.setSize(800,400);
	     frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
	     frame.add(t.initialize());
	     frame.setVisible(true);
	 }
	 
	 public void expandAll(final ULCTableTree tree, final TreePath parent,
				boolean expand) {
		 
		 if(method1){
			 int count = tree.getRowCount();
			 for(int i=0;i<count;i++){
				 tree.expandRow(i);
				 count = tree.getRowCount();
			 }
		 }else{
			 

			Object node = parent.getLastPathComponent();
			TreeNode n = (TreeNode)node;
			//System.out.println("EXPANDING "+n.getValueAt(0));
			int childCount = tree.getModel().getChildCount(node);
			if (childCount >= 0) {
				for (int i = 0; i < childCount; i++) {
					
					TreePath tp = parent.pathByAddingChild(tree.getModel()
							.getChild(node, i));
					
					expandAll(tree, tp, expand);
				}
			}

			if (expand) {
				//System.out.println("expanding Path"+ n.getValueAt(0));
				ULCPollingTimer timer = new ULCPollingTimer(10, new IActionListener() {
				     public void actionPerformed(ActionEvent event) {
				    	 tree.expandPath(parent);
				     }
				 });
				timer.setRepeats(false);
				 timer.start();
				//tree.expandPath(parent);
				//System.out.println("done expanding Path "+(System.currentTimeMillis()-t)+"\n");
			} else {
				tree.collapsePath(parent);
			}
		 }
		}

}

