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.client.widgets;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.google.gwt.user.client.Element;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013: import com.gwtext.client.widgets.event.EditorListener;
014: import com.gwtext.client.widgets.form.Field;
015:
016: import java.util.Date;
017:
018: /**
019: * A base editor field that handles displaying/hiding on demand and has some built-in sizing and event handling logic.
020: */
021: public class Editor extends Component {
022:
023: private static JavaScriptObject configPrototype;
024: private Field field;
025:
026: static {
027: init();
028: }
029:
030: private static native void init()/*-{
031: var c = new $wnd.Ext.Editor();
032: @com.gwtext.client.widgets.Editor::configPrototype = c.initialConfig;
033: }-*/;
034:
035: protected JavaScriptObject getConfigPrototype() {
036: return configPrototype;
037: }
038:
039: public String getXType() {
040: return "editor";
041: }
042:
043: public Editor() {
044: }
045:
046: //todo2 field must be rendered at this point
047: protected native JavaScriptObject create(JavaScriptObject config) /*-{
048: var field = this.@com.gwtext.client.widgets.Editor::field;
049: var fieldJS = field.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
050: return new $wnd.Ext.Editor(fieldJS, config);
051: }-*/;
052:
053: /**
054: * Create a new Editor.
055: *
056: * @param field the editor field
057: */
058: public Editor(Field field) {
059: this .field = field;
060: }
061:
062: public Editor(JavaScriptObject jsObj) {
063: super (jsObj);
064: }
065:
066: private native JavaScriptObject create(JavaScriptObject field,
067: JavaScriptObject config) /*-{
068: return new $wnd.Ext.Editor(field, config);
069: }-*/;
070:
071: /**
072: * Cancels the editing process and hides the editor without persisting any changes. The field value will be reverted to the original starting value.
073: *
074: * @param remainVisible override the default behavior and keep the editor visible after cancel (defaults to false)
075: */
076: public native void cancelEdit(boolean remainVisible) /*-{
077: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
078: editor.cancelEdit();
079: }-*/;
080:
081: /**
082: * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
083: *
084: * @param remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
085: */
086: public native void completeEdit(boolean remainVisible) /*-{
087: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
088: editor.completeEdit(true);
089: }-*/;
090:
091: /**
092: * Gets the data value of the editor.
093: *
094: * @return the value as String
095: */
096: public native String getValueAsString() /*-{
097: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
098: var val = editor.getValue();
099: return val == null ? null : val.toString();
100: }-*/;
101:
102: /**
103: * Gets the data value of the editor.
104: *
105: * @return the editor value
106: */
107: public native Object getValue()/*-{
108: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
109: var val = editor.getValue();
110: var valJ = (val == null || val === undefined || val === '') ? null : $wnd.GwtExt.convertToJavaType(val);
111: return valJ;
112: }-*/;
113:
114: /**
115: * Realigns the editor to the bound field based on the current alignment config value.
116: */
117: public native void realign() /*-{
118: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
119: editor.realign();
120: }-*/;
121:
122: /**
123: * Sets the height and width of this editor.
124: *
125: * @param width the new width
126: * @param height the new height
127: */
128: public native void setSize(int width, int height) /*-{
129: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
130: editor.setSize(width, height);
131: }-*/;
132:
133: /**
134: * Sets the data value of the editor.
135: *
136: * @param value the value
137: */
138: private native void setValueRendered(String value) /*-{
139: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
140: editor.setValue(value);
141: }-*/;
142:
143: /**
144: * Sets the data value of the editor.
145: *
146: * @param value the value
147: */
148: public native void setValueRendered(boolean value) /*-{
149: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
150: editor.setValue(value);
151: }-*/;
152:
153: /**
154: * Sets the data value of the editor.
155: *
156: * @param value the value
157: */
158: public native void setValueRendered(double value) /*-{
159: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
160: editor.setValue(value);
161: }-*/;
162:
163: /**
164: * Sets the data value of the editor.
165: *
166: * @param value the value
167: */
168: public native void setValueRendered(long value) /*-{
169: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
170: editor.setValue(value);
171: }-*/;
172:
173: /**
174: * Sets the data value of the editor.
175: *
176: * @param value the value
177: */
178: public native void setValueRendered(Date value)/*-{
179: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(value);
180: var dateJS = new $wnd.Date(millis);
181: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
182: editor.setValue(dateJS);
183: }-*/;
184:
185: /**
186: * Starts the editing process and shows the editor.
187: *
188: * @param id the element ID to edit
189: */
190: public native void startEdit(String id) /*-{
191: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
192: editor.startEdit(id);
193: }-*/;
194:
195: /**
196: * Starts the editing process and shows the editor.
197: *
198: * @param id the element ID to edit
199: * @param value A value to initialize the editor with. If a value is not provided, it defaults to the innerHTML of the element.
200: */
201: public native void startEdit(String id, String value) /*-{
202: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
203: editor.startEdit(id, value);
204: }-*/;
205:
206: /**
207: * Starts the editing process and shows the editor.
208: *
209: * @param el the element to edit
210: */
211: public native void startEdit(Element el) /*-{
212: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
213: editor.startEdit(el);
214: }-*/;
215:
216: /**
217: * Starts the editing process and shows the editor.
218: *
219: * @param el the element to edit
220: * @param value A value to initialize the editor with. If a value is not provided, it defaults to the innerHTML of the element.
221: */
222: public native void startEdit(Element el, String value) /*-{
223: var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
224: editor.startEdit(el, value);
225: }-*/;
226:
227: /**
228: * Add an Editor listener.
229: *
230: * @param listener the listener
231: */
232: public native void addListener(EditorListener listener) /*-{
233: this.@com.gwtext.client.widgets.Component::addListener(Lcom/gwtext/client/widgets/event/ComponentListener;)(listener);
234: var editorJ = this;
235:
236: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('beforecomplete',
237: function(source, value, startValue) {
238: var valueJ = (value == null || value === undefined || value === '') ? null : $wnd.GwtExt.convertToJavaType(value);
239: var startValueJ = (startValue == null || startValue === undefined || startValue === '') ? null : $wnd.GwtExt.convertToJavaType(startValue);
240: return listener.@com.gwtext.client.widgets.event.EditorListener::doBeforeComplete(Lcom/gwtext/client/widgets/Editor;Ljava/lang/Object;Ljava/lang/Object;)(editorJ, valueJ, startValueJ);
241: }
242: );
243:
244: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('beforestartedit',
245: function(source, boundEl, value) {
246: var boundElJ = @com.gwtext.client.core.ExtElement::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(boundEl);
247: var valueJ = (value == null || value === undefined || value === '') ? null : $wnd.GwtExt.convertToJavaType(value);
248: return listener.@com.gwtext.client.widgets.event.EditorListener::doBeforeStartEdit(Lcom/gwtext/client/widgets/Editor;Lcom/gwtext/client/core/ExtElement;Ljava/lang/Object;)(editorJ, boundElJ, valueJ);
249: }
250: );
251:
252: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('complete',
253: function(source, value, startValue) {
254: var valueJ = (value == null || value === undefined || value === '') ? null : $wnd.GwtExt.convertToJavaType(value);
255: var startValueJ = (startValue == null || startValue === undefined || startValue === '') ? null : $wnd.GwtExt.convertToJavaType(startValue);
256: listener.@com.gwtext.client.widgets.event.EditorListener::onComplete(Lcom/gwtext/client/widgets/Editor;Ljava/lang/Object;Ljava/lang/Object;)(editorJ, valueJ, startValueJ);
257: }
258: );
259:
260: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('specialkey',
261: function(source, field, e) {
262: var eJ = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(e);
263: var fieldJ = field.obj;
264: listener.@com.gwtext.client.widgets.event.EditorListener::onSpeciakKey(Lcom/gwtext/client/widgets/form/Field;Lcom/gwtext/client/core/EventObject;)(editorJ, fieldJ, eJ);
265: }
266: );
267: }-*/;
268:
269: // --- config properties ---
270:
271: /**
272: * The position to align to.
273: *
274: * @param anchorPosition defaults to "c-c"
275: */
276: public void setAlignment(String anchorPosition) {
277: setAttribute("alignment", anchorPosition, true, true);
278: }
279:
280: /**
281: * True for the editor to automatically adopt the size of the underlying field.
282: *
283: * @param autosize true to autosize
284: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
285: */
286: public void setAutosize(boolean autosize)
287: throws IllegalStateException {
288: setAttribute("autosize", autosize, true);
289: }
290:
291: /**
292: * Set to "width" to adopt the width only, or "height" to adopt the height only.
293: *
294: * @param autosize the autosize value
295: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
296: */
297: public void setAutosize(String autosize)
298: throws IllegalStateException {
299: setAttribute("autosize", autosize, true);
300: }
301:
302: /**
303: * True to cancel the edit when the escape key is pressed (defaults to false).
304: *
305: * @param cancelOnEsc cancel on escape
306: */
307: public void setCancelOnEsc(boolean cancelOnEsc) {
308: setAttribute("cancelOnEsc", cancelOnEsc, true, true);
309: }
310:
311: /**
312: * True to complete the edit when the enter key is pressed (defaults to false).
313: *
314: * @param completeOnEnter true to complete on enter
315: */
316: public void setCompleteOnEnter(boolean completeOnEnter) {
317: setAttribute("completeOnEnter", completeOnEnter, true, true);
318: }
319:
320: /**
321: * True to constrain the editor to the viewport.
322: *
323: * @param constrain true to constrain the editor to the viewport
324: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
325: */
326: public void setConstrain(boolean constrain)
327: throws IllegalStateException {
328: setAttribute("constrain", constrain, true);
329: }
330:
331: //todo2 on rendere pass field to object constructor
332: public void setField(Field field) {
333: this .field = field;
334: }
335:
336: /**
337: * False to keep the bound element visible while the editor is displayed (defaults to true).
338: *
339: * @param hideEl true to hide element
340: */
341: public void setHideEl(boolean hideEl) {
342: setAttribute("hideEl", hideEl, true, true);
343: }
344:
345: /**
346: * True to skip the the edit completion process (no save, no events fired) if the user completes an edit and the
347: * value has not changed (defaults to false). Applies only to string values - edits for other data types will never be ignored.
348: *
349: * @param ignoreNoChange true to ingnore no change
350: */
351: public void setIgnoreNoChange(boolean ignoreNoChange) {
352: setAttribute("ignoreNoChange", ignoreNoChange, true, true);
353: }
354:
355: /**
356: * True to automatically revert the field value and cancel the edit when the user completes an edit and the field validation fails (defaults to true).
357: *
358: * @param revertInvalid true to rever invalid field value
359: */
360: public void setRevertInvalid(boolean revertInvalid) {
361: setAttribute("revertInvalid", revertInvalid, true, true);
362: }
363:
364: /**
365: * "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop" for bottom-right shadow (defaults to "frame")
366: *
367: * @param shadow the shadow setting
368: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
369: */
370: public void setShadow(boolean shadow) throws IllegalStateException {
371: setAttribute("shadow", shadow, true);
372: }
373:
374: /**
375: * "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop" for bottom-right shadow (defaults to "frame")
376: *
377: * @param shadow the shadow setting
378: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
379: */
380: public void setShadow(String shadow) throws IllegalStateException {
381: setAttribute("shadow", shadow, true);
382: }
383:
384: /**
385: * Handle the keydown/keypress events so they don't propagate (defaults to true).
386: *
387: * @param swallowKeys true to swallow keys
388: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
389: */
390: public void setSwallowKeys(boolean swallowKeys)
391: throws IllegalStateException {
392: setAttribute("swallowKeys", swallowKeys, true);
393: }
394:
395: /**
396: * True to update the innerHTML of the bound element when the update completes (defaults to false).
397: *
398: * @param updateEl true to update the element
399: */
400: public void setUpdateEl(boolean updateEl) {
401: setAttribute("updateEl", updateEl, true, true);
402: }
403:
404: /**
405: * The data value of the underlying field (defaults to "").
406: *
407: * @param value the field value
408: */
409: public void setValue(String value) {
410: if (!isRendered()) {
411: setAttribute("value", value, true);
412: } else {
413: setValueRendered(value);
414: }
415: }
416:
417: /**
418: * The data value of the underlying field (defaults to "").
419: *
420: * @param value the field value
421: */
422: public void setValue(Date value) {
423: if (!isRendered()) {
424: setAttribute("value", value, true);
425: } else {
426: setValueRendered(value);
427: }
428: }
429:
430: /**
431: * The data value of the underlying field (defaults to "").
432: *
433: * @param value the field value
434: */
435: public void setValue(long value) {
436: if (!isRendered()) {
437: setAttribute("value", value, true);
438: } else {
439: setValueRendered(value);
440: }
441: }
442:
443: /**
444: * The data value of the underlying field (defaults to "").
445: *
446: * @param value the field value
447: */
448: public void setValue(double value) {
449: if (!isRendered()) {
450: setAttribute("value", value, true);
451: } else {
452: setValueRendered(value);
453: }
454: }
455:
456: /**
457: * The data value of the underlying field (defaults to "").
458: *
459: * @param value the field value
460: */
461: public void setValue(boolean value) {
462: if (!isRendered()) {
463: setAttribute("value", value, true);
464: } else {
465: setValueRendered(value);
466: }
467: }
468: }
|