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.widgets.form;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.core.Connection;
013: import com.gwtext.client.core.UrlParam;
014: import com.gwtext.client.data.Record;
015: import com.gwtext.client.util.JavaScriptObjectHelper;
016: import com.gwtext.client.widgets.BaseExtWidget;
017: import com.gwtext.client.widgets.form.event.FormListener;
018:
019: /**
020: * Form widget.
021: */
022: public class Form extends BaseExtWidget {
023:
024: public Form(JavaScriptObject jsObj) {
025: super (jsObj);
026: }
027:
028: protected native JavaScriptObject create(JavaScriptObject config)/*-{
029: return new $wnd.Ext.form.BasicForm(config);
030: }-*/;
031:
032: public static Form instance(JavaScriptObject formJS) {
033: return new Form(formJS);
034: }
035:
036: /**
037: * Add a Field to the current open container (e.g. column, fieldset, etc.).
038: *
039: * @param field the field to add
040: */
041: public native void add(Field field) /*-{
042: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
043: var fieldJS = field.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
044: form.add(fieldJS);
045: }-*/;
046:
047: /**
048: * Copies all properties of the passed config to all fields in this form with the passed object if they don't already exist.
049: *
050: * @param fieldConfig the config object
051: */
052: //TODO
053: /*public native void applyIfToFields(FieldConfig fieldConfig) *//*-{
054: var field = this .@com.gwtext.client.widgets.BaseExtWidget::jsObj;
055: var configJS = fieldConfig.@com.gwtext.client.core.JsObject::getJsObj()();
056: field.applyIfToFields(configJS);
057: }-*//*;
058: */
059: /**
060: * Copies all properties of the passed config to all fields in this form with the passed object.
061: *
062: * @param fieldConfig the config object
063: */
064: //TODO
065: /* public native void applyToFields(FieldConfig fieldConfig) *//*-{
066: var field = this .@com.gwtext.client.widgets.BaseExtWidget::jsObj;
067: var configJS = fieldConfig.@com.gwtext.client.core.JsObject::getJsObj()();
068: field.applyToFields(configJS);
069: }-*//*;*/
070:
071: /**
072: * Clears all invalid messages in this form.
073: */
074: public native void clearInvalid() /*-{
075: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
076: form.clearInvalid();
077: }-*/;
078:
079: /**
080: * Find a {@link Field} in this form by id, dataIndex, name or hiddenName.
081: *
082: * @param id the field ID,dataIndex, name or hiddenName to search for
083: * @return the field
084: */
085: public native Field findField(String id)/*-{
086: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
087: var fieldJS = form.findField(id);
088: return fieldJS != null ? @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(fieldJS) : null;
089: }-*/;
090:
091: //todo add getValues() that returns Map
092: /**
093: * Returns the fields in this form as a String
094: *
095: * @return form vield values as String
096: */
097: public native String getValues() /*-{
098: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
099: return form.getValues(true);
100: }-*/;
101:
102: /**
103: * Returns true if any fields in this form have changed since their original load.
104: *
105: * @return true if dirty
106: */
107: public native boolean isDirty() /*-{
108: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
109: return form.isDirty();
110: }-*/;
111:
112: /**
113: * Returns true if client-side validation on the form is successful.
114: *
115: * @return true if valid
116: */
117: public native boolean isValid() /*-{
118: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
119: return form.isValid();
120: }-*/;
121:
122: //http://extjs.com/forum/showthread.php?t=4340
123: public void load(String url) {
124: JavaScriptObject configJS = JavaScriptObjectHelper
125: .createObject();
126: JavaScriptObjectHelper.setAttribute(configJS, "url", url);
127: load(configJS);
128: }
129:
130: public void load(String url, UrlParam[] params,
131: Connection.Method method, String waitMsg) {
132: JavaScriptObject configJS = JavaScriptObjectHelper
133: .createObject();
134: JavaScriptObjectHelper.setAttribute(configJS, "url", url);
135: if (params != null && params.length > 0) {
136: JavaScriptObject paramObj = UrlParam.getJsObj(params);
137: JavaScriptObjectHelper.setAttribute(configJS, "params",
138: paramObj);
139: }
140:
141: if (method != null) {
142: JavaScriptObjectHelper.setAttribute(configJS, "method",
143: method.getMethod());
144: }
145: if (waitMsg != null) {
146: JavaScriptObjectHelper.setAttribute(jsObj, "waitMsg",
147: waitMsg);
148: }
149: load(configJS);
150: }
151:
152: private native void load(JavaScriptObject configJS) /*-{
153: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
154: form.load(configJS);
155: }-*/;
156:
157: /**
158: * Loads a {@link Record} into this form. The name of the Fields in the {@link com.gwtext.client.data.RecordDef}
159: * must match the names of the Fields in the From.
160: *
161: * @param record the Record to load
162: */
163: public native void loadRecord(Record record) /*-{
164: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
165: var recordJS = record.@com.gwtext.client.core.JsObject::getJsObj()();
166: form.loadRecord(recordJS);
167: }-*/;
168:
169: //markInvalid( ) - no need since can findField by ID and call markInvalid on field itself
170:
171: /**
172: * Removes a field from the items collection (does NOT remove its markup).
173: *
174: * @param field the field to remove
175: */
176: public native void remove(Field field) /*-{
177: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
178: var fieldJS = field.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
179: form.remove(fieldJS);
180: }-*/;
181:
182: /**
183: * Resets this form.
184: */
185: public native void reset() /*-{
186: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
187: form.reset();
188: }-*/;
189:
190: //setValues -- no need for now
191:
192: /**
193: * Shortcut to do a submit action.
194: */
195: public native void submit() /*-{
196: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
197: form.submit();
198: }-*/;
199:
200: /**
201: * Submit the form.
202: *
203: * @param url the url to sumbit to
204: */
205: public void submit(String url) {
206: JavaScriptObject configJS = JavaScriptObjectHelper
207: .createObject();
208: JavaScriptObjectHelper.setAttribute(configJS, "url", url);
209: submit(configJS);
210: }
211:
212: /**
213: * Subit the form.
214: * @param url the url to submit to
215: * @param params additional params to submit
216: * @param method the submit method
217: * @param waitMsg the wait message
218: * @param clientValidation pass true to call form.isValid() prior to posting to validate the form on the client (defaults to false)
219: *
220: * @see com.gwtext.client.core.Connection#GET
221: * @see com.gwtext.client.core.Connection#POST
222: */
223: public void submit(String url, UrlParam[] params,
224: Connection.Method method, String waitMsg,
225: boolean clientValidation) {
226: JavaScriptObject configJS = JavaScriptObjectHelper
227: .createObject();
228: JavaScriptObjectHelper.setAttribute(configJS, "url", url);
229: if (params != null && params.length > 0) {
230: JavaScriptObject paramObj = UrlParam.getJsObj(params);
231: JavaScriptObjectHelper.setAttribute(configJS, "params",
232: paramObj);
233: }
234:
235: if (method != null) {
236: JavaScriptObjectHelper.setAttribute(configJS, "method",
237: method.getMethod());
238: }
239: if (waitMsg != null) {
240: JavaScriptObjectHelper.setAttribute(configJS, "waitMsg",
241: waitMsg);
242: }
243: JavaScriptObjectHelper.setAttribute(configJS,
244: "clientValidation", clientValidation);
245: submit(configJS);
246: }
247:
248: /**
249: * Submit the form using the specified configuration.
250: *
251: * @param configJS form submit config
252: */
253: public native void submit(JavaScriptObject configJS) /*-{
254: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
255: form.submit(configJS);
256: }-*/;
257:
258: /**
259: * Persists the values in this Form into the passed Record object.
260: *
261: * @param record the record to edit
262: */
263: public native void updateRecord(Record record) /*-{
264: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
265: var recordJS = record.@com.gwtext.client.core.JsObject::getJsObj()();
266: form.updateRecord(recordJS);
267: }-*/;
268:
269: /**
270: * Add a Form listener.
271: *
272: * @param listener the form listener
273: */
274: public native void addListener(FormListener listener) /*-{
275: var formJ = this;
276: var form = this.@com.gwtext.client.widgets.BaseExtWidget::jsObj;
277:
278: form.addListener('actioncomplete',
279: function(frm, action) {
280: var responseText = '';
281: var status = 200;
282: if(action.response && action.response != null) {
283: responseText = action.response.responseText;
284: status = action.response.status;
285: }
286: listener.@com.gwtext.client.widgets.form.event.FormListener::onActionComplete(Lcom/gwtext/client/widgets/form/Form;ILjava/lang/String;)(formJ, status, responseText);
287: }
288: );
289:
290: form.addListener('actionfailed',
291: function(frm, action) {
292: var responseText = '';
293: var status = 200;
294: if(action.response && action.response != null) {
295: responseText = action.response.responseText;
296: status = action.response.status;
297: }
298: listener.@com.gwtext.client.widgets.form.event.FormListener::onActionFailed(Lcom/gwtext/client/widgets/form/Form;ILjava/lang/String;)(formJ, status, '');
299: }
300: );
301:
302: form.addListener('beforeaction',
303: function(frm) {
304: return listener.@com.gwtext.client.widgets.form.event.FormListener::doBeforeAction(Lcom/gwtext/client/widgets/form/Form;)(formJ);
305: }
306: );
307: }-*/;
308: }
|