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:
009: package com.gwtext.client.data;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013:
014: /**
015: * Data reader class that is setup to consume data from an array data source. This class sets up the
016: * field metadata for the data present in the array data source.
017: * <pre>
018: * Object[][] data = new Object[][]{
019: * new Object[]{"3m Co", new Double(71.72), new Double(0.02), new Double(0.03), "9/1 12:00am", 1},
020: * new Object[]{"Alcoa Inc", new Double(29.01), new Double(0.42), new Double(1.47), "9/1 12:00am", 2}};
021: * ArrayReader reader = new ArrayReader(new RecordDef(
022: * new FieldDef[]{
023: * new StringFieldDef("company"),
024: * new FloatFieldDef("price"),
025: * new FloatFieldDef("change"),
026: * new FloatFieldDef("pctChange"),
027: * new DateFieldDef("lastChanged", "n/j h:ia")
028: * }
029: * ));
030: * </pre>
031: * <p/>
032: * If the data array has data that doesn't exactly match the the fields you need in the Reader, then you can specify a
033: * (zero based) positional mapper in the field definitions.
034: * <p/>
035: * <pre>
036: * ArrayReader reader = new ArrayReader(new RecordDef(
037: * new FieldDef[]{
038: * new StringFieldDef("company"), //equivalent to new StringField("company", 0);
039: * new FloatFieldDef("change", 2),
040: * new DateFieldDef("lastChanged", "n/j h:ia", 4)
041: * }
042: * ));
043: * </pre>
044: * <p/>
045: * <pre>
046: * MemoryProxy dataProxy = new MemoryProxy(data);
047: * Store store = new Store(dataProxy, reader);
048: * store.load();
049: * </pre>
050: * <p/>
051: * Sometimes you might want to map a field in the data source to be the "ID" of the record. You indicate the position
052: * of the data element that represents the ID by using constructor that takes the field ID index as the first argument.
053: * <p/>
054: * <pre>
055: * ArrayReader reader = new ArrayReader(5, new RecordDef(
056: * new FieldDef[]{
057: * new StringFieldDef("company"), //equivalent to new StringFieldDef("company", 0);
058: * new FloatFieldDef("change", 2),
059: * new DateFieldDef("lastChanged", "n/j h:ia", 4)
060: * }
061: * ));
062: * </pre>
063: * <p/>
064: * So for the above data set the ID for the first record will get mapped to 1 (data index of 5 corresponts to the last
065: * element in the array) and the ID of the second row gets mapped to 2. Mapping ID's for Record's are useful when, say,
066: * the data soource is mapped to a widget like a Grid and when the user clicks on a row, you want to geta handle of the
067: * ID for that record which might correspond to the primary key of the database table from which the data is being read.
068: *
069: * @author Sanjiv Jivan
070: * @see RecordDef
071: * @since 0.9
072: */
073: public class ArrayReader extends Reader {
074:
075: /**
076: * Construct an ArrayRader using the specified {@link RecordDef}.
077: *
078: * @param recordDef the record definition
079: */
080: public ArrayReader(RecordDef recordDef) {
081: setRecordDef(recordDef);
082: }
083:
084: /**
085: * Construct an ArrayRader using the specified {@link RecordDef}.
086: *
087: * @param id position of the ID of the field in the underlying array data that provides an ID for the Record
088: * @param recordDef the record definition
089: * @see Record
090: */
091: public ArrayReader(int id, RecordDef recordDef) {
092: setId(id);
093: setRecordDef(recordDef);
094: }
095:
096: protected native JavaScriptObject create(JavaScriptObject config,
097: JavaScriptObject recordDef) /*-{
098: return new $wnd.Ext.data.ArrayReader(config, recordDef);
099: }-*/;
100:
101: //config
102: /**
103: * The position of the ID of the field in the underlying array data that provides an ID for the Record
104: *
105: * @param id the ID
106: */
107: public void setId(int id) {
108: JavaScriptObjectHelper.setAttribute(configJS, "id", id);
109: }
110:
111: }
|