// Filename: GridItem.java
//
// Jeppesen Sanderson, Inc. Confidential & Proprietary
//
// This work contains valuable confidential and proprietary
// information.  Disclosure, use or reproduction outside of
// Jeppesen Sanderson, Inc. is prohibited, except as authorized
// in writing.  This unpublished work is protected by the laws
// of the United States and other countries.  In the event of
// publication, the following notice shall apply:
//
// Copyright 2012-14  Jeppesen Sanderson, Inc.  All Rights Reserved
//

package com.smartgwt.sample.client;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;
import com.smartgwt.client.widgets.form.fields.events.ShowValueEvent;
import com.smartgwt.client.widgets.form.fields.events.ShowValueHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class GridItem extends CanvasItem {

	private static final int AUTO_FIT_MAX_RECORDS = 1000;
	private DataSource gridDataSource;
	private final String title;
	private Label titleLabel;
	private ListGrid grid = null;

	public GridItem(String name, String title) {

		super(name);
		this.title = title;

		setWidth("*");
		setHeight("*");

		setShowTitle(false);
		setColSpan("*");
		setEndRow(true);
		setStartRow(true);

		setInitHandler(new FormItemInitHandler() {

			@Override
			public void onInit(FormItem item) {
				grid = new ListGrid();

				// dataSource and fields to use, provided to a listGridItem
				// as listGridItem.gridDataSource and optional gridFields
				grid.setDataSource(getGridDataSource());

				// The form item's data is set to a list of records
				RecordList value = (RecordList) item.getValue();
				if (value != null) {
					grid.setData(value);
				}

				VLayout layout = new VLayout();

				HLayout header = makeSectionHeader();
				grid.setAlternateRecordStyles(true);
				grid.setAutoFitData(Autofit.VERTICAL);
				grid.setAutoFitMaxRecords(AUTO_FIT_MAX_RECORDS);

				header = makeSectionHeader();
				layout.addMember(header);
				layout.addMember(grid);

				setCanvas(layout);

				addShowValueHandler(new ShowValueHandler() {
					@Override
					public void onShowValue(ShowValueEvent event) {

						VLayout layout = (VLayout) event.getItem().getCanvas();
						int idx = layout.getMemberNumber(grid.getID());
						ListGrid theGrid = (ListGrid) layout.getMember(idx);

						if (theGrid != null) {
							int nRecs = 0;
							RecordList records = event
									.getDataValueAsRecordList();

							if (records != null) {
								theGrid.setData(records);
								nRecs = records.getLength();
							} else {
								theGrid.setData(new Record[0]);
							}

							setSectionTitle(nRecs);
						}
					}
				});
			}
		});
	}

	private void setSectionTitle(int recordCount) {
		StringBuffer countTitle = new StringBuffer(this.title).append(" (")
				.append(recordCount).append(")");
		titleLabel.setContents(countTitle.toString());
	}

	/**
	 * Make new section header with the given title.
	 * 
	 * @param title
	 *            the title of the section header.
	 * @return an HLayout for the section header.
	 */
	private HLayout makeSectionHeader() {

		HLayout sectionHeader = new HLayout();
		sectionHeader.setStyleName("sectionHeaderopened");
		sectionHeader.setHeight(26);
		HLayout paddingSpacer = new HLayout();
		paddingSpacer.setWidth(3);
		sectionHeader.addMember(paddingSpacer);
		titleLabel = new Label(this.title);
		titleLabel.setHeight(26);
		titleLabel.setWidth(200);
		titleLabel.setBaseStyle("flightPackageSectionLabel");
		sectionHeader.addMember(titleLabel);

		return sectionHeader;
	}

	public DataSource getGridDataSource() {
		return this.gridDataSource;
	}

	public void setGridDataSource(DataSource gridDataSource) {
		this.gridDataSource = gridDataSource;
	}

	public ListGrid getListGrid() {
		return this.grid;
	}

};
