package com.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
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;

public class MainWindowGroupTree {
	private static MainWindowGroupTree instance = null;

	public static MainWindowGroupTree getInstance() {
		
		if(null == instance){
			instance = new MainWindowGroupTree();
		}
		return instance;
	}
	private VLayout treeVLayout;

	public VLayout getTreeVLayout() {
		return treeVLayout;
	}
	private int getTreeWidth() {
		String treeWidth = (String) Offline.get("groupTreeWidth");
		if (treeWidth != null) {
			return Integer.valueOf(treeWidth);
		}
		return 250; // Default width
	}
	private HStack groupToolHStack;	
	private MainWindowGroupTree() {
		treeVLayout = new VLayout();
		treeVLayout.setWidth(getTreeWidth());
		treeVLayout.setStyleName("LeftMargin");
		treeVLayout.setOverflow(Overflow.AUTO);
		treeVLayout.setShowResizeBar(true);
		groupToolHStack = new HStack(1); 	

		groupToolHStack.setAlign(Alignment.RIGHT);
		treeVLayout.addMember(groupToolHStack);
		
		Tree employeeTree = new Tree();  
        employeeTree.setModelType(TreeModelType.PARENT);  
        employeeTree.setIdField("EmployeeId");  
        employeeTree.setParentIdField("ReportsTo");  
        employeeTree.setNameProperty("Name");  
        employeeTree.setRootValue(1);  
        employeeTree.setData(employeeData);  
         
        TreeGrid employeeTreeGrid = new TreeGrid();  
       
        employeeTreeGrid.setWidth100();
        employeeTreeGrid.setHeight100();
        employeeTreeGrid.setShowOpenIcons(false);  
        employeeTreeGrid.setShowDropIcons(false);  
        employeeTreeGrid.setClosedIconSuffix("");  
        employeeTreeGrid.setFields(new TreeGridField("Name"));  
        employeeTreeGrid.setData(employeeTree);  
          
        employeeTreeGrid.getData().openAll();  
				treeVLayout.addMember(employeeTreeGrid);
				
	}
	 public static class EmployeeTreeNode extends TreeNode {  
	        public EmployeeTreeNode(String employeeId, String reportsTo, String name) {  
	            setAttribute("EmployeeId", employeeId);  
	            setAttribute("ReportsTo", reportsTo);  
	            setAttribute("Name", name);  
	        }  
	    }
	 public static final TreeNode[] employeeData = new TreeNode[] {  
	        new EmployeeTreeNode("4", "1", "Charles Madigen"),  
	        new EmployeeTreeNode("189", "4", "Gene Porter"),  
	        new EmployeeTreeNode("265", "189", "Olivier Doucet"),  
	        new EmployeeTreeNode("264", "189", "Cheryl Pearson"),  
	        new EmployeeTreeNode("188", "4", "Rogine Leger")  
	    }; 
	
	 private VLayout mainVLayout = null;
		private TabSet mainScreenTabSet = null;
		public VLayout getMainVLayout(){	
			Tab groupTab = new Tab();
			groupTab.setID("Tab1");
			groupTab.setCanClose(true);
			groupTab.setAttribute("tab1", 0);
			
			mainVLayout = new VLayout();
			mainVLayout.setHeight("100%");
			mainVLayout.setOverflow(Overflow.AUTO);
			
			
			mainScreenTabSet = new TabSet();
			mainScreenTabSet.setTabBarAlign(Side.LEFT);
			mainScreenTabSet.setTabBarPosition(Side.TOP);
			mainScreenTabSet.setWidth100();
			mainScreenTabSet.setHeight100();
			CustomeDataGrid custGrid = new CustomeDataGrid();
			groupTab.setPane(custGrid);
			
			mainScreenTabSet.addTab(groupTab);
			mainVLayout.addMember(mainScreenTabSet);
			return mainVLayout;
		
		}
	
}
