001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.sample.showcase2.client.grid;
009:
010: import com.gwtext.client.core.Connection;
011: import com.gwtext.client.core.EventObject;
012: import com.gwtext.client.core.TextAlign;
013: import com.gwtext.client.core.UrlParam;
014: import com.gwtext.client.data.*;
015: import com.gwtext.client.widgets.Button;
016: import com.gwtext.client.widgets.Panel;
017: import com.gwtext.client.widgets.Toolbar;
018: import com.gwtext.client.widgets.ToolbarButton;
019: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
020: import com.gwtext.client.widgets.form.ComboBox;
021: import com.gwtext.client.widgets.form.DateField;
022: import com.gwtext.client.widgets.form.NumberField;
023: import com.gwtext.client.widgets.form.TextField;
024: import com.gwtext.client.widgets.grid.*;
025: import com.gwtext.client.widgets.grid.event.GridCellListenerAdapter;
026: import com.gwtext.sample.showcase2.client.ShowcasePanel;
027:
028: import java.util.Date;
029:
030: public class EditableGridSample extends ShowcasePanel {
031:
032: private EditorGridPanel grid;
033:
034: public String getSourceUrl() {
035: return "source/grid/EditableGridSample.java.html";
036: }
037:
038: public String getXmlDataUrl() {
039: return "source/grid/EditableGridSample.xml.html";
040: }
041:
042: public Panel getViewPanel() {
043: if (panel == null) {
044: HttpProxy proxy = new HttpProxy("data/plants.xml",
045: Connection.GET);
046:
047: final RecordDef recordDef = new RecordDef(new FieldDef[] {
048: new StringFieldDef("common"),
049: new StringFieldDef("botanical"),
050: new StringFieldDef("light"),
051: new FloatFieldDef("price"),
052: new DateFieldDef("availDate", "availability",
053: "m/d/Y"), new BooleanFieldDef("indoor") });
054:
055: XmlReader reader = new XmlReader("plant", recordDef);
056: final Store store = new Store(proxy, reader);
057: SimpleStore cbStore = new SimpleStore("lightTypes",
058: new String[] { "Shade", "Mostly Shady",
059: "Sun or Shade", "Mostly Sunny", "Sunny" });
060:
061: store.load();
062: cbStore.load();
063:
064: final ComboBox cb = new ComboBox();
065: cb.setDisplayField("lightTypes");
066: cb.setStore(cbStore);
067:
068: ColumnConfig commonCol = new ColumnConfig("Common Name",
069: "common", 220, true, null, "common");
070: commonCol.setEditor(new GridEditor(new TextField()));
071:
072: ColumnConfig lightCol = new ColumnConfig("Light", "light",
073: 130);
074: lightCol.setEditor(new GridEditor(cb));
075:
076: ColumnConfig priceCol = new ColumnConfig("Price", "price",
077: 70, true);
078: priceCol.setAlign(TextAlign.RIGHT);
079: priceCol.setRenderer(new Renderer() {
080: public String render(Object value,
081: CellMetadata cellMetadata, Record record,
082: int rowIndex, int colNum, Store store) {
083: return "$" + value;
084: }
085: });
086: NumberField numberField = new NumberField();
087: numberField.setAllowBlank(false);
088: numberField.setAllowNegative(false);
089: numberField.setMaxValue(1000);
090: priceCol.setEditor(new GridEditor(numberField));
091:
092: ColumnConfig availableCol = new ColumnConfig("Available",
093: "availDate", 95, true);
094:
095: DateField dateField = new DateField();
096: dateField.setFormat("m/d/Y");
097: dateField.setMinValue("01/01/06");
098: dateField.setDisabledDays(new int[] { 0, 6 });
099: dateField
100: .setDisabledDaysText("Plants are not available on the weekend");
101: availableCol.setEditor(new GridEditor(dateField));
102:
103: ColumnConfig indoorCol = new ColumnConfig("Indoor?",
104: "indoor", 55);
105:
106: indoorCol.setRenderer(new Renderer() {
107: public String render(Object value,
108: CellMetadata cellMetadata, Record record,
109: int rowIndex, int colNum, Store store) {
110: boolean checked = ((Boolean) value).booleanValue();
111: return "<img class=\"checkbox\" src=\"js/ext/resources/images/default/menu/"
112: + (checked ? "checked.gif"
113: : "unchecked.gif") + "\"/>";
114: }
115: });
116:
117: ColumnConfig[] columnConfigs = { commonCol, lightCol,
118: priceCol, availableCol, indoorCol };
119:
120: ColumnModel columnModel = new ColumnModel(columnConfigs);
121: columnModel.setDefaultSortable(true);
122:
123: Toolbar toolbar = new Toolbar();
124:
125: ToolbarButton button = new ToolbarButton("Add Plant",
126: new ButtonListenerAdapter() {
127: public void onClick(Button button, EventObject e) {
128:
129: Record plant = recordDef
130: .createRecord(new Object[] {
131: "New Plant1",
132: "Anguinaria Canadensis",
133: "Mostly Shady",
134: new Float(5), "",
135: Boolean.FALSE });
136: grid.stopEditing();
137: store.insert(0, plant);
138: grid.startEditing(0, 0);
139: }
140: });
141:
142: grid = new EditorGridPanel();
143: grid.setStore(store);
144: grid.setColumnModel(columnModel);
145: grid.setWidth(500);
146: grid.setHeight(300);
147: grid.setAutoExpandColumn("common");
148: grid.setTitle("Editor Grid Example");
149: grid.setFrame(true);
150: grid.setClicksToEdit(1);
151: grid.setTopToolbar(toolbar);
152:
153: grid.addGridCellListener(new GridCellListenerAdapter() {
154: public void onCellClick(GridPanel grid, int rowIndex,
155: int colIndex, EventObject e) {
156: if (grid.getColumnModel().getDataIndex(colIndex)
157: .equals("indoor")
158: && e.getTarget(".checkbox", 1) != null) {
159: Record record = grid.getStore().getAt(rowIndex);
160: record.set("indoor", !record
161: .getAsBoolean("indoor"));
162: }
163: }
164: });
165:
166: store.load(new UrlParam[] { new UrlParam("rnd", new Date()
167: .getTime()
168: + "") });
169: panel = new Panel();
170: panel.add(grid);
171: }
172: return panel;
173: }
174:
175: public String getIntro() {
176: return "<p>This example shows how to create a grid with inline editing. Try clicking on the table cells. Each colum can be assigned its own cell editor.<p>"
177: + "<p>The 'Common Name' column has a TextField editor, the 'Light' column has a ComboBox editor, 'Price' has a NumberField editor, 'Available' has a DateFieldEditor"
178: + " and 'Indoor' has a custom Boolean editor.";
179: }
180: }
|