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: import java.util.Date;
016:
017: /**
018: * Instances of this class encapsulate both record definition information, and record value information for use in Store objects,
019: * or any code which needs to access Records cached in a Store object.
020: *
021: * @see com.gwtext.client.data.Store
022: */
023: public class Record extends JsObject {
024:
025: public static final class Operation {
026: private final String operation;
027:
028: private Operation(String operation) {
029: this .operation = operation;
030: }
031:
032: public String getOperation() {
033: return operation;
034: }
035:
036: public boolean equals(Object o) {
037: if (this == o)
038: return true;
039: if (!(o instanceof Operation))
040: return false;
041:
042: Operation operation1 = (Operation) o;
043:
044: if (!operation.equals(operation1.operation))
045: return false;
046:
047: return true;
048: }
049:
050: public int hashCode() {
051: return operation.hashCode();
052: }
053: }
054:
055: public static Operation EDIT = new Operation("edit");
056: public static Operation REJECT = new Operation("reject");
057: public static Operation COMMIT = new Operation("commit");
058:
059: public Record(JavaScriptObject jsObj) {
060: super (jsObj);
061: }
062:
063: private static Record instance(JavaScriptObject rec) {
064: return new Record(rec);
065: }
066:
067: /**
068: * Returns the ID of the record or null if not defined.
069: *
070: * @return ID of the record
071: */
072: public native String getId() /*-{
073: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
074: var id = record.id;
075: return (id == null || id === undefined) ? null : id.toString();
076: }-*/;
077:
078: /**
079: * The Records modified state.
080: *
081: * @return true if this record has been modified
082: */
083: public native boolean isDirty() /*-{
084: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
085: return record.dirty;
086: }-*/;
087:
088: /**
089: * Begin an edit. While in edit mode, no events are relayed to the containing store.
090: */
091: public native void beginEdit() /*-{
092: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
093: record.beginEdit();
094: }-*/;
095:
096: /**
097: * Cancels all changes made in the current edit operation.
098: */
099: public native void cancelEdit() /*-{
100: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
101: record.cancelEdit();
102: }-*/;
103:
104: /**
105: * Creates a copy of this record.
106: *
107: * @return copy of this Record
108: */
109: public native Record copy() /*-{
110: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
111: var record2 = record.copy();
112: return @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(record2);
113: }-*/;
114:
115: /**
116: * Creates a copy of this record.
117: *
118: * @param id a new record id if you don't want to use this record's id
119: * @return copy of this Record
120: */
121: public native Record copy(String id) /*-{
122: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
123: var record2 = record.copy(id);
124: return @com.gwtext.client.data.Record::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(record2);
125: }-*/;
126:
127: /**
128: * Usually called by the Store which owns the Record. Commits all changes made to the Record since either creation,
129: * or the last commit operation. Developers should subscribe to {@link com.gwtext.client.data.event.StoreListener#onUpdate(Store, Record, com.gwtext.client.data.Record.Operation)} event
130: * to have their code notified of commit operations.
131: */
132: public native void commit() /*-{
133: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
134: record.commit();
135: }-*/;
136:
137: /**
138: * Usually called by the Store which owns the Record. Commits all changes made to the Record since either creation,
139: * or the last commit operation. Developers should subscribe to {@link com.gwtext.client.data.event.StoreListener#onUpdate(Store, Record, com.gwtext.client.data.Record.Operation)} event
140: * to have their code notified of commit operations.
141: *
142: * @param silent true to skip notification of the owning store of the change (defaults to false)
143: */
144: public native void commit(boolean silent) /*-{
145: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
146: record.commit(silent);
147: }-*/;
148:
149: /**
150: * End an edit. If any data was modified, the containing store is notified.
151: */
152: public native void endEdit() /*-{
153: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
154: record.endEdit();
155: }-*/;
156:
157: //todo throw exception if field is invalid as opposed to null value for field?
158: //throw new Error("JS Error Object");
159: /**
160: * Return the field value as String.
161: *
162: * @param field the field name
163: * @return the field value
164: */
165: public native String getAsString(String field) /*-{
166: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
167: var value = record.get(field);
168: //todo?
169: //if (value === undefined) throw new Error("Invalid field " + field);
170: return (value === undefined || value == null || value == '') ? null : value.toString();
171: }-*/;
172:
173: /**
174: * Return the field value as Object.
175: *
176: * @param field the field name
177: * @return the filed value
178: */
179: public native Object getAsObject(String field) /*-{
180: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
181: var value = record.get(field);
182: return (value === undefined || value == null || value == '') ? null : value;
183: }-*/;
184:
185: /**
186: * Sets the fields value.
187: *
188: * @param field the field name
189: * @param value the field value
190: */
191: public native void set(String field, String value) /*-{
192: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
193: record.set(field, value);
194: }-*/;
195:
196: /**
197: * Returns the Records value as an integer.
198: *
199: * @param field the field name
200: * @return the field value
201: */
202: public native int getAsInteger(String field) /*-{
203: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
204: var value = record.get(field);
205: return (value === undefined || value == null ) ? 0 : parseInt(value);
206: }-*/;
207:
208: /**
209: * Sets the fields value.
210: *
211: * @param field the field name
212: * @param value the field value
213: */
214: public native void set(String field, int value) /*-{
215: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
216: record.set(field, value);
217: }-*/;
218:
219: /**
220: * Returns the Records value as a float.
221: *
222: * @param field the field name
223: * @return the field value
224: */
225: public native float getAsFloat(String field) /*-{
226: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
227: var value = record.get(field);
228: return (value === undefined || value == null ) ? null : parseFloat(value);
229: }-*/;
230:
231: /**
232: * Sets the fields value.
233: *
234: * @param field the field name
235: * @param value the field value
236: */
237: public native void set(String field, float value) /*-{
238: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
239: record.set(field, value);
240: }-*/;
241:
242: /**
243: * Returns the Records value as a double.
244: *
245: * @param field the field name
246: * @return the field value
247: */
248: public native double getAsDouble(String field) /*-{
249: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
250: var value = record.get(field);
251: return (value === undefined || value == null ) ? null : parseFloat(value);
252: }-*/;
253:
254: /**
255: * Sets the fields value.
256: *
257: * @param field the field name
258: * @param value the field value
259: */
260: public native void set(String field, double value) /*-{
261: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
262: record.set(field, value);
263: }-*/;
264:
265: /**
266: * Returns the Records value as a boolean value. If val is null, undefined, false, -0, +0, NaN, or an empty string (""),
267: * this method returns false, otherwise it returns true for all other numbers and strings.
268: *
269: * @param field the field name
270: * @return the field value
271: */
272: public native boolean getAsBoolean(String field) /*-{
273: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
274: var value = record.get(field);
275: return (value === undefined || value == null) ? false : Boolean(value);
276: }-*/;
277:
278: /**
279: * Sets the fields value.
280: *
281: * @param field the field name
282: * @param value the field value
283: */
284: public native void set(String field, boolean value) /*-{
285: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
286: record.set(field, value);
287: }-*/;
288:
289: /**
290: * Set the Record's ID.
291: *
292: * @param id the record ID
293: */
294: public native void setId(String id) /*-{
295: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
296: record.id = id;
297: }-*/;
298:
299: /**
300: * Return the fields value as a Date.
301: *
302: * @param field the field name
303: * @return the field value
304: */
305: public native Date getAsDate(String field) /*-{
306: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
307: var val = record.get(field);
308: if(val == null || val === undefined || val == '') {
309: return null;
310: } else {
311: return @com.gwtext.client.util.DateUtil::create(J)(val.getTime());
312: }
313: }-*/;
314:
315: /**
316: * Sets the fields value.
317: *
318: * @param field the field name
319: * @param value the field value
320: */
321: public native void set(String field, Date value) /*-{
322: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
323: if(value == null) {
324: record.set(field, null);
325: } else {
326: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(value);
327: record.set(field, new $wnd.Date(millis));
328: }
329: }-*/;
330:
331: /**
332: * Return the names of the fields that are modified in this record.
333: *
334: * @return modified field names. Returns an empty array if no fields are modified.
335: */
336: public String[] getModifiedFields() {
337: JavaScriptObject nativeArray = getModifiedFeilds(jsObj);
338: return JavaScriptObjectHelper
339: .convertToJavaStringArray(nativeArray);
340: }
341:
342: private native JavaScriptObject getModifiedFeilds(
343: JavaScriptObject record)/*-{
344: if (record.modified === undefined) return null;
345: var fields = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
346: for(var key in record.modified) {
347: fields.push(key);
348: }
349: return fields;
350: }-*/;
351:
352: //see http://extjs.com/forum/showthread.php?t=2834&highlight=validateedit&page=2 for use
353: /**
354: * Usually called by the Store which owns the Record. Rejects all changes made to the Record since either creation,
355: * or the last commit operation. Modified fields are reverted to their original values. Developers should subscribe
356: * to the {@link com.gwtext.client.data.event.StoreListener#onUpdate(Store, Record, com.gwtext.client.data.Record.Operation)} event
357: * to have their code notified of reject operations.
358: */
359: public native void reject() /*-{
360: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
361: record.reject();
362: }-*/;
363:
364: /**
365: * Usually called by the Store which owns the Record. Rejects all changes made to the Record since either creation,
366: * or the last commit operation. Modified fields are reverted to their original values. Developers should subscribe
367: * to the {@link com.gwtext.client.data.event.StoreListener#onUpdate(Store, Record, com.gwtext.client.data.Record.Operation)} event
368: * to have their code notified of reject operations.
369: *
370: * @param silent true to skip notification of the owning store of the change (defaults to false)
371: */
372: public native void reject(boolean silent) /*-{
373: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
374: record.reject(silent);
375: }-*/;
376:
377: /**
378: * Returns the field value as an Object.
379: *
380: * @return the data object
381: */
382: public native Object getDataAsObject()/*-{
383: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
384: return record.bean === undefined ? null : record.bean ;
385: }-*/;
386:
387: /**
388: * Returns the underlying native data representation like a Json or XML node.
389: *
390: * @return data as native object
391: */
392: public native JavaScriptObject getDataAsJsObject()/*-{
393: var record = this.@com.gwtext.client.core.JsObject::getJsObj()();
394: var val = record.json || record.node;
395: return val === undefined ? null : val;
396: }-*/;
397: }
|