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;
009:
010: import com.google.gwt.i18n.client.DateTimeFormat;
011: import com.google.gwt.i18n.client.NumberFormat;
012: import com.gwtext.client.data.*;
013: import com.gwtext.client.widgets.grid.*;
014:
015: import java.util.Date;
016:
017: public class SampleGrid extends GridPanel {
018:
019: private static final NumberFormat nf = NumberFormat.getFormat(
020: "#,##0.00", "$");
021: private static final NumberFormat nfc = NumberFormat
022: .getFormat("#,##0.00");
023: private static final DateTimeFormat dateFormatter = DateTimeFormat
024: .getFormat("M/d/y");
025:
026: protected static BaseColumnConfig[] columns = new BaseColumnConfig[] {
027:
028: new ColumnConfig("Company", "company", 160, true, null,
029: "company"),
030:
031: new ColumnConfig("Price", "price", 35, true,
032: new Renderer() {
033: public String render(Object value,
034: CellMetadata cellMetadata,
035: Record record, int rowIndex,
036: int colNum, Store store) {
037: return nf.format(((Number) value)
038: .doubleValue());
039: }
040: }),
041:
042: new ColumnConfig("Change", "change", 45, true,
043: new Renderer() {
044: public String render(Object value,
045: CellMetadata cellMetadata,
046: Record record, int rowIndex,
047: int colNum, Store store) {
048: float val = ((Float) value).floatValue();
049: String valString = nfc.format(val);
050: if (val < 0) {
051: return "<span style='color:red;'>"
052: + valString + "</span>";
053: } else {
054: return valString;
055: }
056: }
057: }, "change"),
058: new ColumnConfig("% Change", "pctChange", 65, true, null,
059: "pctChange"),
060:
061: new ColumnConfig("Last Updated", "lastChanged", 65, true,
062: new Renderer() {
063: public String render(Object value,
064: CellMetadata cellMetadata,
065: Record record, int rowIndex,
066: int colNum, Store store) {
067: Date date = (Date) value;
068: return dateFormatter.format(date);
069: }
070: }),
071: new ColumnConfig("Industry", "industry", 60, true) };
072:
073: private static RecordDef recordDef = new RecordDef(new FieldDef[] {
074: new StringFieldDef("company"), new FloatFieldDef("price"),
075: new FloatFieldDef("change"),
076: new FloatFieldDef("pctChange"),
077: new DateFieldDef("lastChanged", "n/j h:ia"),
078: new StringFieldDef("symbol"),
079: new StringFieldDef("industry") });
080:
081: private int[] columnIndexes;
082:
083: public SampleGrid() {
084: this (false);
085: }
086:
087: public SampleGrid(boolean small) {
088: this (false, null);
089: }
090:
091: public SampleGrid(boolean small, int[] columnIndexes) {
092:
093: this .columnIndexes = columnIndexes;
094: Object[][] data = small ? SampleData.getCompanyDataSmall()
095: : SampleData.getCompanyDataLarge();
096: MemoryProxy proxy = new MemoryProxy(data);
097:
098: ArrayReader reader = new ArrayReader(recordDef);
099: Store store = new Store(proxy, reader);
100: store.load();
101: setStore(store);
102:
103: ColumnModel columnModel = getColumnConfigs();
104: setColumnModel(columnModel);
105:
106: setFrame(true);
107: setStripeRows(true);
108: setAutoExpandColumn("company");
109: setIconCls("grid-icon");
110: }
111:
112: protected ColumnModel getColumnConfigs() {
113: ColumnModel columnModel = null;
114: if (columnIndexes == null) {
115: columnModel = new ColumnModel(columns);
116: } else {
117: BaseColumnConfig[] columnConfigs = new BaseColumnConfig[columnIndexes.length];
118: for (int i = 0; i < columnIndexes.length; i++) {
119: int columnIndex = columnIndexes[i];
120: columnConfigs[i] = columns[i];
121: }
122: columnModel = new ColumnModel(columnConfigs);
123: }
124: return columnModel;
125: }
126: }
|