package com.smartgwt.sample.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CustomDS implements EntryPoint {
	private IButton printButton;
	private VLayout layout;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void onUncaughtException(Throwable t) {
				if (t instanceof UmbrellaException) {
					t.getCause().printStackTrace();
				} else {
					t.printStackTrace();
				}
			}
		});

		VStack vStack = new VStack();
		vStack.setLeft(175);
		vStack.setTop(75);
		vStack.setWidth("70%");
		vStack.setMembersMargin(20);

		layout = new VLayout();
		layout.setHeight(100);

		Label label = new Label();
		label.setContents("Test Me");
		label.setHeight(20);

		layout.addMember(label);

		DynamicForm form = new DynamicForm();
		form.setHeight(100);

		GridItem item = new GridItem("gridItem", "Grid Item");
		DataSource dataSource = new DataSource();

		dataSource.setID("supplyItem");

		DataSourceSequenceField itemID = new DataSourceSequenceField("itemID");
		itemID.setPrimaryKey(true);
		itemID.setHidden(true);

		DataSourceTextField itemName = new DataSourceTextField("itemName",
				"Item", 128, true);

		DataSourceTextField SKU = new DataSourceTextField("SKU", "SKU", 10);
		// SKU.setCanFilter(false);

		DataSourceTextField description = new DataSourceTextField(
				"description", "Description");
		DataSourceTextField category = new DataSourceTextField("category",
				"Category", 128);
		DataSourceTextField units = new DataSourceTextField("units", "Units", 5);

		DataSourceFloatField unitCost = new DataSourceFloatField("unitCost",
				"Unit Cost");
		DataSourceBooleanField inStock = new DataSourceBooleanField("inStock",
				"In Stock");

		DataSourceDateField nextShipment = new DataSourceDateField(
				"nextShipment", "Next Shipment");

		dataSource.setFields(itemID, itemName, SKU, description, category,
				units, unitCost, inStock, nextShipment);

		item.setGridDataSource(dataSource);
		form.setItems(item);

		layout.addMember(form);

		vStack.addMember(layout);

		ToolStrip toolbar = new ToolStrip();
		toolbar.setMembersMargin(10);
		toolbar.setHeight(22);

		printButton = new IButton("Print");
		printButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				printMe();
			}
		});
		toolbar.addMember(printButton);

		vStack.addMember(toolbar);

		vStack.draw();

	}

	/**
	 * 
	 */
	protected void printMe() {
		DSRequest requestProperties = new DSRequest();
		requestProperties.setExportFilename("printme.pdf");
		requestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
		requestProperties.setContentType("application/pdf");
		requestProperties.setDownloadResult(true);
		requestProperties.setDownloadToNewWindow(true);
		RPCManager.exportContent(layout, requestProperties);
		System.out.println("printed pdf at " + new Date());
	}

}
