package com.ebizserve.gwt.sample.client;

import java.util.LinkedHashMap;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.RecordComponentPoolingMode;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
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.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class TestListGrid extends VLayout {
	
	public TestListGrid(){
		
		setWidth100();
		setHeight100();

		final ListGrid listGrid = new ListGrid(){
			protected Canvas createRecordComponent(final ListGridRecord record,
					Integer colNum) {
				String fieldName = this.getFieldName(colNum);
				DynamicForm form = new DynamicForm();
				if (fieldName.equals("dimensionality")) {
					SelectItem dimensionalityItem = new SelectItem();
					dimensionalityItem.setWidth(110);


					dimensionalityItem.setShowTitle(false);
					form.setFields(dimensionalityItem);
				}
				if (fieldName.equals("field")) {

					SelectItem fieldItem = new SelectItem();
					fieldItem.setWidth(110);
					fieldItem.setShowTitle(false);
					form.setFields(fieldItem);
				}
				if (fieldName.equals("contrast")) {
					SelectItem contrastItem = new SelectItem();
					contrastItem.setShowTitle(false);
					contrastItem.setWidth(110);
					contrastItem.setValueMap("=", "!=", ">", "<", ">=", "<=",
							"not", "not in", "like");
					form.setFields(contrastItem);
				}
				if (fieldName.equals("value")) {
					
					LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
					valueMap.put("1", "ALL");
					valueMap.put("2", "");
					RadioGroupItem radioItem = new RadioGroupItem();
					radioItem.setValueMap(valueMap);
					radioItem.setVertical(false);
					radioItem.setWrapTitle(false);
					
					radioItem.setDefaultValue("1");
					radioItem.setShowTitle(false);
					radioItem.setName("value");

					TextItem textItem = new TextItem();
					textItem.setShowTitle(false);

					form.setNumCols(5);
					form.setFields(radioItem, textItem);
					form.setColWidths("10","10","10","10","*");
				}

				return form;

			}
		};
//		listGrid.setAlwaysShowEditors(true);
		listGrid.setShowRecordComponents(true);
		listGrid.setShowRecordComponentsByCell(true);
		listGrid.setRecordComponentPoolingMode(RecordComponentPoolingMode.DATA);

		// 增加条件按钮
		final IButton addButton = new IButton("增加条件");
		addButton.setAutoFit(true);
		addButton.setIcon("[SKINIMG]/actions/add.png");
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ListGridRecord gridRecord = new ListGridRecord();
				gridRecord.setAttribute("dimensionality", "");
				gridRecord.setAttribute("field", "");
				gridRecord.setAttribute("contrast", "");
				gridRecord.setAttribute("value", "");
				listGrid.addData(gridRecord);
				listGrid.cancelEditing();
				if(listGrid.getRecords().length==1){
					
				}
			}
		});
		// 删除条件按钮
		final IButton delButton = new IButton("删除条件");
		delButton.setAutoFit(true);
		delButton.setIcon("[SKINIMG]/actions/remove.png");
		delButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				listGrid.removeSelectedData();
			}
		});
		
		HLayout secondLayout = new HLayout();
		secondLayout.addMember(addButton);
		secondLayout.addMember(delButton);
		secondLayout.setMembersMargin(6);
		secondLayout.setAlign(Alignment.RIGHT);
		secondLayout.setHeight(25);

		
		// listGrid=new ListGrid();
		listGrid.setHeight100();
		// listGrid.setAutoFetchData(true);
		listGrid.setCanEdit(false);
		listGrid.setAutoSaveEdits(true);
		listGrid.setSelectionType(SelectionStyle.SIMPLE);
		listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

		// 维度Field
		ListGridField dimensionalityField = new ListGridField("dimensionality",
				"维度", 120);
		dimensionalityField.setAlign(Alignment.LEFT);

		// 栏位Field
		ListGridField fieldField = new ListGridField("field", "栏位", 120);
		fieldField.setAlign(Alignment.LEFT);


		// 对比Field
		ListGridField contrastField = new ListGridField("contrast", "对比", 120);
		contrastField.setAlign(Alignment.LEFT);

		// 值Field
		ListGridField valueField = new ListGridField("value", "值");
		valueField.setAlign(Alignment.LEFT);

		listGrid.setFields(dimensionalityField, fieldField, contrastField,
				valueField);

		addMember(secondLayout);
		addMember(listGrid);
	}

}
