package com.oc.smartgwt.test.client;

import java.util.Date;

import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.SortArrow;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
import com.smartgwt.client.widgets.tree.events.FolderClosedEvent;
import com.smartgwt.client.widgets.tree.events.FolderClosedHandler;
import com.smartgwt.client.widgets.tree.events.FolderOpenedEvent;
import com.smartgwt.client.widgets.tree.events.FolderOpenedHandler;

import com.google.gwt.core.client.EntryPoint;

public class TreeGridLoadTest implements EntryPoint {

    private String dataFileName = "my_datafile.xml";
    
    public void onModuleLoad() {

		System.out.println(new Date() + "-----> onModuleLoad");  
		HLayout layout = new HLayout();  
        layout.setTop(0);  
    	layout.setWidth100();
        layout.setHeight100();  
    	layout.setOverflow(Overflow.HIDDEN);
    	layout.setShowResizeBar(false);     
    	
        layout.addMember(getTreeGrid());
        layout.draw();
    }
    
    public TreeGrid getTreeGrid() {

	    final TreeGrid treeGrid = new TreeGrid();  
        treeGrid.setTitle("TreeGridTitle");
        
        treeGrid.setKeepParentsOnFilter(true);

        treeGrid.setShowOpenIcons(true);  
        treeGrid.setShowDropIcons(false);  
        treeGrid.setFolderIcon("folder.png");  
        treeGrid.setClosedIconSuffix("closed");  

      	treeGrid.setAutoFetchData(false);
		treeGrid.setDataSource(myDS.getInstance(dataFileName));  
		treeGrid.setLoadDataOnDemand(true);
		treeGrid.setDataPageSize(500);
		treeGrid.setDataFetchMode(FetchMode.PAGED);

		treeGrid.setNodeIcon("person.png");  
          
        treeGrid.setSelectionType(SelectionStyle.SIMPLE);
        treeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

        treeGrid.setCanSelectAll(true);
        treeGrid.setCascadeSelection(true);
        treeGrid.setShowPartialSelection(true);
        treeGrid.setShowConnectors(true);
        treeGrid.setShowHeader(true);
        treeGrid.setShowSortArrow(SortArrow.FIELD);

        treeGrid.setShowEmptyMessage(true);
        treeGrid.setShowAllRecords(true);
        treeGrid.setWrapCells(false);
        treeGrid.setEmptyMessage("noDataAvailable");

        treeGrid.setHeight100();
        treeGrid.setWidth100();  

        treeGrid.setOverflow(Overflow.SCROLL);
        treeGrid.setCanFocus(true);
        treeGrid.setArrowKeyAction("select");
        treeGrid.setCanHover(true);
        treeGrid.setHoverWidth(100);

        treeGrid.setAutoFitFieldWidths(true); 
        treeGrid.setSortFoldersBeforeLeaves(true);
        treeGrid.sort("Name", SortDirection.ASCENDING);
		
        treeGrid.setAutoFitWidth("Name", true);
        treeGrid.setAutoFitWidthApproach(AutoFitWidthApproach.VALUE);
        treeGrid.setShowHover(true);
        
        treeGrid.setShowSelectedStyle(false);

	    TreeGridField nameField = new TreeGridField("Name", "NameFieldTitle");
	    nameField.setShowHover(true);
	    
        treeGrid.setFields(nameField);
        
        treeGrid.addDrawHandler(new DrawHandler() {
            public void onDraw(DrawEvent event) {
    			System.out.println(new Date() + "onDraw.fetchData now"); 
        		treeGrid.fetchData();
            }
        });
        
        treeGrid.addDataArrivedHandler(new DataArrivedHandler() {
				public void onDataArrived(DataArrivedEvent event) {
	        		try {
	            		Tree tree = treeGrid.getTree();
	            		if (tree != null) {
	            			System.out.println(new Date() + "onDataArrived.treeModelType = " + tree.getModelType() + " | treeRootValue = " + tree.getRootValue());  
	                		TreeNode[] nodes = tree.getAllNodes();
	                		if (nodes != null) {
	                			System.out.println(new Date() + "onDataArrived.AllNodesSize = " + nodes.length); 
	                			int count = 0;
	                			for (TreeNode node : nodes) {
	                				String groupType = node.getAttribute("GroupType");
	                				String groupId = node.getAttribute("GroupID");
	                				String empId = node.getAttribute("EmployeeId");
	                				String name = node.getAttribute("Name");
	                				if (groupType != null && groupType.equalsIgnoreCase("group") && name.startsWith("Group"))
	                					System.out.println(new Date() + "onDataArrived.node.name = " + name + " | id = " + empId + " | groupId = " + groupId);
	                				if (empId.equalsIgnoreCase("1"))
	                					System.out.println(new Date() + "onDataArrived.node.name = " + name + " | groupType = " + groupType + " | groupId = " + groupId + " | id = " + empId + " | name = " + name);
		            				if (count == 10 || count == 20 || count == 30)
		            					treeGrid.selectRecord(node);
		            				count++;
		            			}
	                		}
	            		}
	        		} catch (Throwable t ) {
	        			System.out.println(new Date() + "onDataArrived.Error = " + t);
	        		}
				}
	        });
        
        treeGrid.addFolderOpenedHandler(new FolderOpenedHandler() {
			public void onFolderOpened(FolderOpenedEvent event) {
		        TreeNode node = (TreeNode) treeGrid.getRecord(treeGrid.getEventRow());
		        System.out.println(new Date() + "onFolderOpened.EventRow = " + treeGrid.getEventRow() + " | TreeNode = " + node);
		        TreeNode[] nodes = treeGrid.getTree().getAllNodes();
		        if (nodes != null) System.out.println(new Date() + "onFolderOpened.AllNodesSize = " + nodes.length);
			}
		});
        
        treeGrid.addFolderClosedHandler(new FolderClosedHandler() {
			public void onFolderClosed(FolderClosedEvent event) {
		        TreeNode node = (TreeNode) treeGrid.getRecord(treeGrid.getEventRow());
		        System.out.println(new Date() + "onFolderClosed.EventRow = " + treeGrid.getEventRow() + " | TreeNode = " + node);
		        TreeNode[] nodes = treeGrid.getTree().getAllNodes();
		        if (nodes != null) System.out.println(new Date() + "onFolderClosed.AllNodesSize = " + nodes.length);
			}
		});

        return treeGrid;
    }
}