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.EventObject;
011: import com.gwtext.client.data.*;
012: import com.gwtext.client.widgets.*;
013: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
014: import com.gwtext.client.widgets.form.NumberField;
015: import com.gwtext.client.widgets.grid.ColumnConfig;
016: import com.gwtext.client.widgets.grid.ColumnModel;
017: import com.gwtext.client.widgets.grid.GridPanel;
018: import com.gwtext.client.widgets.layout.FitLayout;
019: import com.gwtext.sample.showcase2.client.SampleData;
020: import com.gwtext.sample.showcase2.client.ShowcasePanel;
021:
022: public class LargeGridSample extends ShowcasePanel {
023: private Store store;
024:
025: public String getSourceUrl() {
026: return "source/grid/LargeGridSample.java.html";
027: }
028:
029: public Panel getViewPanel() {
030: if (panel == null) {
031: panel = new Panel();
032: panel.setLayout(new FitLayout());
033: RecordDef recordDef = new RecordDef(new FieldDef[] {
034: new StringFieldDef("company"),
035: new FloatFieldDef("price"),
036: new FloatFieldDef("change"),
037: new FloatFieldDef("pctChange"),
038: new DateFieldDef("lastChanged", "n/j h:ia"),
039: new StringFieldDef("symbol"),
040: new StringFieldDef("industry") });
041:
042: final GridPanel gridPanel = new GridPanel();
043:
044: Object[][] data = SampleData.getCompanyDataLarge();
045: MemoryProxy proxy = new MemoryProxy(data);
046:
047: ArrayReader reader = new ArrayReader(recordDef);
048: store = new Store(proxy, reader);
049: store.load();
050: gridPanel.setStore(store);
051:
052: ColumnConfig[] columns = new ColumnConfig[] {
053: //column ID is company which is later used in setAutoExpandColumn
054: new ColumnConfig("Company", "company", 160, true,
055: null, "company"),
056: new ColumnConfig("Price", "price", 35),
057: new ColumnConfig("Change", "change", 45),
058: new ColumnConfig("% Change", "pctChange", 65),
059: new ColumnConfig("Last Updated", "lastChanged", 65),
060: new ColumnConfig("Industry", "industry", 60, true) };
061:
062: ColumnModel columnModel = new ColumnModel(columns);
063: gridPanel.setColumnModel(columnModel);
064:
065: gridPanel.setTitle("Large Grid");
066: gridPanel.setEnableColumnResize(false);
067: gridPanel.setEnableDragDrop(false);
068: gridPanel.setEnableColumnHide(false);
069: gridPanel.setEnableColumnMove(false);
070:
071: Toolbar top = new Toolbar();
072: top
073: .addItem(new ToolbarTextItem(
074: "This example dynamically generates data to populate the grid. Creating very large Grids might cause the browser to get unstable and might require a restart."));
075: gridPanel.setTopToolbar(top);
076:
077: Toolbar bottom = new Toolbar();
078: bottom
079: .addItem(new ToolbarTextItem(
080: "<sup>*</sup>Total time for Grid update includes the time taken to generate the local data"));
081:
082: bottom.addFill();
083: bottom.addItem(new ToolbarTextItem("Columns"));
084: bottom.addSpacer();
085: final NumberField cols = new NumberField("Columns",
086: "numCols", 40, 10);
087: cols.setAllowDecimals(false);
088: bottom.addField(cols);
089:
090: bottom.addSeparator();
091:
092: bottom.addItem(new ToolbarTextItem("Rows"));
093: bottom.addSpacer();
094: final NumberField rows = new NumberField("Rows", "numRows",
095: 40, 200);
096: rows.setAllowDecimals(false);
097: bottom.addField(rows);
098:
099: bottom.addSeparator();
100:
101: ToolbarButton generateButton = new ToolbarButton(
102: "Generate", new ButtonListenerAdapter() {
103: public void onClick(Button button, EventObject e) {
104: updateGrid(gridPanel, cols.getValue()
105: .intValue(), rows.getValue()
106: .intValue());
107: }
108: });
109: generateButton.setIconCls("database-add-icon");
110: bottom.addButton(generateButton);
111:
112: ToolbarButton clearButton = new ToolbarButton("Clear Data",
113: new ButtonListenerAdapter() {
114: public void onClick(Button button, EventObject e) {
115: store.removeAll();
116: }
117: });
118: clearButton.setIconCls("database-delete-icon");
119: bottom.addButton(clearButton);
120:
121: gridPanel.setBottomToolbar(bottom);
122: panel.add(gridPanel);
123: }
124: return panel;
125: }
126:
127: private void updateGrid(GridPanel gridPanel, int cols, int rows) {
128: if (store != null) {
129: store.removeAll();
130: }
131:
132: Object[][] data = new Object[rows][cols];
133: ColumnConfig[] columns = new ColumnConfig[cols];
134: FieldDef[] fields = new FieldDef[cols];
135:
136: for (int i = 0; i < cols; i++) {
137: String colName = "Column" + i;
138: fields[i] = new StringFieldDef(colName);
139: columns[i] = new ColumnConfig(colName, colName, 90);
140: }
141:
142: RecordDef recordDef = new RecordDef(fields);
143: ColumnModel columnModel = new ColumnModel(columns);
144:
145: for (int i = 0; i < rows; i++) {
146: for (int j = 0; j < cols; j++) {
147: data[i][j] = "(" + i + "," + j + ")";
148: }
149: }
150:
151: MemoryProxy proxy = new MemoryProxy(data);
152: ArrayReader reader = new ArrayReader(recordDef);
153: store = new Store(proxy, reader);
154: store.load();
155:
156: gridPanel.reconfigure(store, columnModel);
157: }
158: }
|