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.core.JsObject;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014:
015: /**
016: * A record definition where the fields in the source data of a {@link Reader} are declared.
017: * <p/>
018: * <pre>
019: * <code>
020: * <p/>
021: * Object[][] states = new Object[][]{
022: * new Object[]{"AL", "Alabama"},
023: * new Object[]{"AK", "Alaska"},
024: * new Object[]{"AZ", "Arizona"},
025: * new Object[]{"AR", "Arkansas"},
026: * new Object[]{"CA", "California"}};
027: * <p/>
028: * Reader reader = new ArrayReader(new RecordDef(
029: * new FieldDef[]{
030: * new StringFieldDef("abbr"),
031: * new StringFieldDef("state")
032: * }));
033: * <p/>
034: * Store store = new Store(proxy, reader);
035: * </code>
036: * </pre>
037: *
038: * @see Store
039: * @see com.gwtext.client.data.MemoryProxy
040: */
041: public class RecordDef extends JsObject {
042:
043: private FieldDef[] fields;
044:
045: /**
046: * Construct a new RecordDef using the passed field definitions.
047: *
048: * @param fields the field defs
049: */
050: public RecordDef(FieldDef[] fields) {
051: this .fields = fields;
052: int numFields = fields.length;
053: Object[] jsObjs = new Object[numFields];
054: for (int i = 0; i < numFields; i++) {
055: JavaScriptObject jsObj = fields[i].getJsObj();
056: jsObjs[i] = jsObj;
057: }
058: jsObj = getJsObj(JavaScriptObjectHelper
059: .convertToJavaScriptArray(jsObjs));
060: }
061:
062: private native JavaScriptObject getJsObj(JavaScriptObject recordDef) /*-{
063: return $wnd.Ext.data.Record.create(recordDef);
064: }-*/;
065:
066: /**
067: * Create a new Record instance using the passed data.
068: *
069: * @param id the Record ID
070: * @param rowData the record data
071: * @return a Record instance
072: */
073: public Record createRecord(String id, Object[] rowData) {
074: Record record = createRecord(rowData);
075: record.setId(id);
076: return record;
077: }
078:
079: /**
080: * Create a new Record instance using the passed data.
081: *
082: * @param rowData the record data
083: * @return a Record instance
084: */
085: public Record createRecord(Object[] rowData) {
086: int numFields = fields.length;
087: if (rowData.length != numFields) {
088: throw new IllegalArgumentException("Expected " + numFields
089: + " fields but was passed " + rowData.length
090: + " fields.");
091: }
092: MemoryProxy proxy = new MemoryProxy(new Object[][] { rowData });
093: ArrayReader reader = new ArrayReader(this );
094: Store temp = new Store(proxy, reader);
095: temp.load();
096: return temp.getAt(0);
097: }
098:
099: /**
100: * Return the fields defined for this RecordDef.
101: *
102: * @return the field defs
103: */
104: public FieldDef[] getFields() {
105: return fields;
106: }
107: }
|