import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

public class TypeBrowser extends Window
{
	protected TreeGrid grid;

	public TypeBrowser()
	{
		layoutComponent();
	}

	private void layoutComponent()
	{
		this.setHeight(500);
		this.setWidth(200);

		this.setTitle("Types");
		this.setShowTitle(true);
		
		grid = new TreeGrid();
		grid.setHeight100();
		grid.setWidth100();

		grid.setShowEmptyMessage(false);

		grid.setCanEdit(true);
		grid.setEditEvent(ListGridEditEvent.DOUBLECLICK);
		grid.setListEndEditAction(RowEndEditAction.NEXT);

		grid.setLoadDataOnDemand(false);
		grid.setAutoFetchData(false);
		grid.setDataSource(new TypeDataSource());
		
		
		TreeGridField topicName = new TreeGridField("name", "Name");
		TreeGridField topicId = new TreeGridField("internal_id", "Internal Id");
		TreeGridField idField = new TreeGridField("id", "Id");
		TreeGridField parentKey = new TreeGridField("parent_id", "Parent");
		parentKey.setHidden(true);
		topicId.setHidden(true);
		idField.setHidden(true);
		
		grid.setFields(topicName, idField, topicId, parentKey);

		grid.setShowConnectors(true);
		grid.setShowFullConnectors(false);
		grid.setCanSort(true);
		grid.setAnimateFolders(true);

		grid.setAnimateFolderSpeed(200);
		grid.setPreventDuplicates(false);

		grid.setSelectionType(SelectionStyle.MULTIPLE);
		grid.setConfirmDiscardEdits(false);
		grid.setDragDataAction(DragDataAction.MOVE);

		grid.setCanAcceptDroppedRecords(true);
		grid.setCanReparentNodes(true);
		grid.setCanDragRecordsOut(true);
		grid.setCanReorderRecords(true);

		LayoutSpacer spacer = new LayoutSpacer();
		spacer.setOpacity(0);
		spacer.setWidth("*");

		LayoutSpacer smallSpacer = new LayoutSpacer();
		smallSpacer.setOpacity(0);
		smallSpacer.setWidth(2);

		HeaderControl refreshControl = new HeaderControl(HeaderControl.REFRESH, new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) 
			{
				refresh();
			}});
		
		this.setHeaderControls(smallSpacer, HeaderControls.HEADER_LABEL, spacer, refreshControl);

		this.setCanDrag(false);
		this.setCanDragResize(false);
		this.setCanDragReposition(false);
		this.setOverflow(Overflow.VISIBLE);
		this.setAlign(Alignment.RIGHT);
		this.addItem(grid);
		grid.fetchData();
	}

	public void refresh()
	{
		final String selectedPaths = grid.getSelectedPaths();
		final String openState = grid.getOpenState();

		grid.invalidateCache();
		grid.fetchData(null, new DSCallback()
		{
			public void execute(DSResponse response, Object rawData, DSRequest request)
			{
				grid.setOpenState(openState);
				grid.setSelectedPaths(selectedPaths);
			}
		});
	}

}
