package com.sample.client.treegrid;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.Cursor;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.HeaderSpan;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;


public class TreeGridSample  extends VLayout implements EntryPoint {

	TreeGrid 			treeGridSample;
	Tree 				treeSample;

	private TreeGridSampleDataprovider dataProvider;


	public void onModuleLoad() {

		if (!GWT.isScript()) { 
			KeyIdentifier debugKey = new KeyIdentifier(); 
			debugKey.setCtrlKey(true); 
			debugKey.setKeyName("D"); 
			Page.registerKey(debugKey, new PageKeyHandler() {

				@Override
				public void execute(String keyName) {
					SC.showConsole();      
				}   
			});
		}


		dataProvider = new TreeGridSampleDataprovider();
		buildUI();
		this.show();
	}

	public void buildUI() {

		VLayout vlMainLayout = new VLayout();			
		vlMainLayout.addMember(getButtonLayout());
		vlMainLayout.addMember(getTreeGrid());

		this.setWidth100();
		this.setHeight100();
		this.setMembersMargin(0);
		this.addMember(vlMainLayout);

		treeSample.setIsFolderProperty(TreeGridSampleDataprovider.IS_FOLDER);

		treeSample.setData(dataProvider.getRecords());

		treeGridSample.setData(treeSample);
	}


	private Canvas getButtonLayout() {




		Button buttonWithIssue = new Button("setFieldTitle and showFields");
		buttonWithIssue.setPrompt("When setFieldTitle and showFields is called, data columns in the tree grid is not getting aligned with the Header Titles");
		buttonWithIssue.setWidth(200);
		buttonWithIssue.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				treeGridSample.setFieldTitle(TreeGridSampleDataprovider.COL_9, "PCS");
				treeGridSample.setFieldTitle(TreeGridSampleDataprovider.COL_10, "LBS");

				treeGridSample.showFields(TreeGridSampleDataprovider.COL_9, TreeGridSampleDataprovider.COL_10);

			}
		});

		Button buttonWithIssue2 = new Button("showFields and setFieldTitle");
		buttonWithIssue2.setPrompt("When showFields and setFieldTitle is called, 'Bal To Pieces' columns not getting autofit.");

		buttonWithIssue2.setWidth(200);
		buttonWithIssue2.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				treeGridSample.showFields(TreeGridSampleDataprovider.COL_9, TreeGridSampleDataprovider.COL_10);

				treeGridSample.setFieldTitle(TreeGridSampleDataprovider.COL_9, "PCS");
				treeGridSample.setFieldTitle(TreeGridSampleDataprovider.COL_10, "LBS");
			}
		});

		Button buttonAutoFitFields = new Button("autoFitFields");
		buttonAutoFitFields.setPrompt("autoFitFields() on the tree grid solved the issue of alignment of data columns to the headers but created the other issue of Header span Title being cut off as you can see in the Tree grid 'Bal To...'");
		buttonAutoFitFields.setWidth(200);
		buttonAutoFitFields.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				treeGridSample.autoFitFields();

			}
		});

		Button buttonLinknodes = new Button("LinkNodes to '402'");
		buttonLinknodes.setPrompt("Add Child on the root node '402'");
		buttonLinknodes.setWidth(200);
		buttonLinknodes.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				treeSample.linkNodes(dataProvider.getSubRecords());

			}
		});

		HLayout hLayout = new HLayout(10);
		hLayout.setMembersMargin(10);
		hLayout.setPadding(10);
		hLayout.setAutoHeight();
		hLayout.addMember(buttonWithIssue);
		hLayout.addMember(buttonWithIssue2);
		hLayout.addMember(buttonAutoFitFields);
		hLayout.addMember(buttonLinknodes);

		return hLayout;
	}

	private VLayout getTreeGrid() {
		// create this layout so that it can contain the scroll bars instead of the grid
		VLayout vlGridLayout = new VLayout();
		vlGridLayout.setID("vlGridLayout");
		vlGridLayout.addMember(setupDetailGrid());
		vlGridLayout.setHeight100();
		vlGridLayout.setWidth100();
		return vlGridLayout;
	}

	private TreeGrid setupDetailGrid() {
		getDetailTree();
		treeGridSample.show();
		treeGridSample.setCursor(Cursor.ARROW);

		createTree();

		return treeGridSample;
	}

	public void createTree() {
		treeSample= new Tree();
		treeSample.setModelType(TreeModelType.PARENT);  
		treeSample.setIdField(TreeGridSampleDataprovider.ID_FIELD);  // curr node
		treeSample.setParentIdField(TreeGridSampleDataprovider.PARENT_ID_FIELD); // prnt node
	}

	private TreeGrid getDetailTree() {

		treeGridSample = new TreeGrid();

		setTreeGrid(treeGridSample, "treeGridSample");	

		treeGridSample.setCanResizeFields(true);

		TreeGridField tgf1 = new TreeGridField(TreeGridSampleDataprovider.COL_1, ("ID"));
		tgf1.setPrompt(("ID"));

		TreeGridField tgf2 = new TreeGridField(TreeGridSampleDataprovider.COL_2, "");
		tgf2.setShowTitle(false);

		TreeGridField tgf3 = new TreeGridField(TreeGridSampleDataprovider.COL_3, ("SRC-DEST-NM"));
		tgf3.setShowTitle(false);

		TreeGridField tgf4 = new TreeGridField(TreeGridSampleDataprovider.COL_4, ("Product"));

		TreeGridField tgf5 = new TreeGridField(TreeGridSampleDataprovider.COL_5, ("PCS"));
		tgf5.setAlign(Alignment.RIGHT);

		TreeGridField tgf6 = new TreeGridField(TreeGridSampleDataprovider.COL_6, ("LBS"));
		tgf6.setAlign(Alignment.RIGHT);

		TreeGridField tgf7 = new TreeGridField(TreeGridSampleDataprovider.COL_7, ("PCS"));
		tgf7.setAlign(Alignment.RIGHT);

		TreeGridField tgf8 = new TreeGridField(TreeGridSampleDataprovider.COL_8, ("LBS"));
		tgf8.setAlign(Alignment.RIGHT);

		TreeGridField tgf9 = new TreeGridField(TreeGridSampleDataprovider.COL_9, ("BAL_PCS_STR"));
		tgf9.setAlign(Alignment.RIGHT);
		tgf9.setHidden(true);

		TreeGridField tgf10 = new TreeGridField(TreeGridSampleDataprovider.COL_10, ("BAL_WGT_STR"));
		tgf10.setAlign(Alignment.RIGHT);
		tgf10.setHidden(true);

		TreeGridField tgf11 = new TreeGridField(TreeGridSampleDataprovider.COL_11, ("PCS"));
		tgf11.setAlign(Alignment.RIGHT);

		TreeGridField tgf12 = new TreeGridField(TreeGridSampleDataprovider.COL_12, ("LBS"));
		tgf12.setAlign(Alignment.RIGHT);

		TreeGridField tgf13 = new TreeGridField(TreeGridSampleDataprovider.COL_13, ("PCS"));
		tgf13.setAlign(Alignment.RIGHT);

		TreeGridField tgf14 = new TreeGridField(TreeGridSampleDataprovider.COL_14, ("LBS"));
		tgf14.setAlign(Alignment.RIGHT);

		TreeGridField tgf15 = new TreeGridField(TreeGridSampleDataprovider.COL_15, ("Contact")); 

		TreeGridField tgf16 = new TreeGridField(TreeGridSampleDataprovider.COL_16, ("Shipment Reference")); 

		TreeGridField tgf17 = new TreeGridField(TreeGridSampleDataprovider.COL_17, ("DM")); 

		TreeGridField tgf18 = new TreeGridField(TreeGridSampleDataprovider.COL_18, ("Carrier"));

		TreeGridField tgf19 = new TreeGridField(TreeGridSampleDataprovider.COL_19, ("Transport No"));

		TreeGridField tgf20 = new TreeGridField(TreeGridSampleDataprovider.COL_20, ("Voyage"));

		TreeGridField tgf21 = new TreeGridField(TreeGridSampleDataprovider.COL_21, ("Direct Ship"));

		TreeGridField tgf22 = new TreeGridField(TreeGridSampleDataprovider.COL_22, ("Instructions"));

		HeaderSpan hsReqd =   new HeaderSpan(("Required"), new String[] { 
				TreeGridSampleDataprovider.COL_5,
				TreeGridSampleDataprovider.COL_6});
		hsReqd.setAlign(Alignment.CENTER);

		HeaderSpan hsPlnd =   new HeaderSpan(("Planned"), new String[] { 
				TreeGridSampleDataprovider.COL_7,
				TreeGridSampleDataprovider.COL_8});
		hsReqd.setAlign(Alignment.CENTER);

		HeaderSpan hsBalance =   new HeaderSpan(("Bal To Pieces"), new String[] { 
				TreeGridSampleDataprovider.COL_9,
				TreeGridSampleDataprovider.COL_10});
		hsBalance.setAlign(Alignment.CENTER);

		HeaderSpan hsComp =   new HeaderSpan(("Shipped"), new String[] { 
				TreeGridSampleDataprovider.COL_11,
				TreeGridSampleDataprovider.COL_12});
		hsComp.setAlign(Alignment.CENTER);

		HeaderSpan hsRcvd =   new HeaderSpan(("Recieved"), new String[] { 
				TreeGridSampleDataprovider.COL_13,
				TreeGridSampleDataprovider.COL_14});
		hsRcvd.setAlign(Alignment.CENTER);

		treeGridSample.setHeaderSpans(hsReqd, hsPlnd, hsBalance, hsComp, hsRcvd);

		treeGridSample.setFields( tgf1, tgf2, tgf3, tgf4, tgf5, tgf6, 
				tgf7, tgf8, tgf9, tgf10, tgf11, tgf12, tgf13, 
				tgf14, tgf15, tgf16, tgf17, tgf18, tgf19, tgf20,  
				tgf21, tgf22 );

		return treeGridSample;
	}

	public static void setTreeGrid(TreeGrid treeGrid, String componentID){


		treeGrid.setID(componentID);
		treeGrid.setWidth100();  
		treeGrid.setHeight100();		
		treeGrid.setAlternateRecordStyles(true);
		treeGrid.setBorder("1px solid lightgrey");
		treeGrid.setShowHeaderContextMenu(false);
		treeGrid.setShowEmptyMessage(false);
		treeGrid.setEmptyMessage("");		
		treeGrid.setBodyOverflow(Overflow.SCROLL);
		treeGrid.setLeaveScrollbarGap(true);
		treeGrid.setCanSort(false);
		treeGrid.setCanResizeFields(true);	
		treeGrid.setFastCellUpdates(true);
		// Setting this to false disables keyboard scrolling on all browsers with the exception of MSIE 
		//treeGrid.setCanFocus(false);
		treeGrid.setShowRollOver(false);
		treeGrid.setAutoFitFieldsFillViewport(false);
		treeGrid.setSelectionType(SelectionStyle.SINGLE);
		treeGrid.setShowSelectedStyle(true);
		treeGrid.setCellPadding(5);
		// this is required so that when a column is sized so small that its contents are clipped, the cell value will be displayed
		// when the user hovers over that cell
		treeGrid.setCanHover(null);
		treeGrid.setShowClippedValuesOnHover(true);

		treeGrid.setNodeIcon(null);  
		treeGrid.setFolderIcon(null);		
		treeGrid.setHeaderHeight(46);
		treeGrid.setOpenerIconSize(20);
		treeGrid.setIndentSize(30);
		treeGrid.setShowOpener(true);
		treeGrid.setShowOpenIcons(true);  
		treeGrid.setShowDropIcons(false);
		treeGrid.setAutoSizeHeaderSpans(true);

		// the next 4 values can potentially cause a performance issue 
		// if a developer needs to not auto size the grid, they should override these next 2 values
		treeGrid.setAutoFitFieldWidths(true);
		treeGrid.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
		treeGrid.setShowAllColumns(true);
		treeGrid.setShowAllRecords(true);		

		treeGrid.setBaseStyle("myTreeCell");

		//the next 3 values prevents the display of the context menu on header spans 
		treeGrid.setShowHeaderSpanTitlesInSortEditor(false);
		treeGrid.setCanFreezeFields(false);
		treeGrid.setCanPickFields(false);

	}


}