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.util.JavaScriptObjectHelper;
013: import com.gwtext.client.widgets.form.event.TextFieldListener;
014:
015: /**
016: * Basic text field.
017: */
018: public class TextField extends Field {
019:
020: private static JavaScriptObject configPrototype;
021:
022: static {
023: init();
024: }
025:
026: private static native void init()/*-{
027: var c = new $wnd.Ext.form.TextField();
028: @com.gwtext.client.widgets.form.TextField::configPrototype = c.initialConfig;
029: }-*/;
030:
031: protected JavaScriptObject getConfigPrototype() {
032: return configPrototype;
033: }
034:
035: public String getXType() {
036: return "textfield";
037: }
038:
039: /**
040: * Create a new TextField.
041: */
042: public TextField() {
043: }
044:
045: /**
046: * Create a new TextField.
047: *
048: * @param fieldLabel the field label
049: */
050: public TextField(String fieldLabel) {
051: super (fieldLabel);
052: }
053:
054: /**
055: * Create a new TextField.
056: *
057: * @param fieldLabel the field label
058: * @param name the field name
059: */
060: public TextField(String fieldLabel, String name) {
061: super (fieldLabel, name);
062: }
063:
064: /**
065: * Create a new TextField.
066: *
067: * @param fieldLabel the field label
068: * @param name the field name
069: * @param width the field width
070: */
071: public TextField(String fieldLabel, String name, int width) {
072: super (fieldLabel, name, width);
073: }
074:
075: /**
076: * Create a new TextField.
077: *
078: * @param fieldLabel the field label
079: * @param name the field name
080: * @param width the field width
081: * @param value the field value
082: */
083: public TextField(String fieldLabel, String name, int width,
084: String value) {
085: super (fieldLabel, name, width);
086: setValue(value);
087: }
088:
089: public TextField(JavaScriptObject jsObj) {
090: super (jsObj);
091: }
092:
093: protected native JavaScriptObject create(JavaScriptObject jsObj) /*-{
094: return new $wnd.Ext.form.TextField(jsObj);
095: }-*/;
096:
097: /**
098: * Add a TextField listener.
099: *
100: * @param listener the listener
101: */
102: public native void addListener(TextFieldListener listener) /*-{
103: this.@com.gwtext.client.widgets.form.Field::addListener(Lcom/gwtext/client/widgets/form/event/FieldListener;)(listener);
104: var fieldJ = this;
105:
106: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('autosize',
107: function(fld, width) {
108: listener.@com.gwtext.client.widgets.form.event.TextFieldListener::onAutoSize(Lcom/gwtext/client/widgets/form/Field;I)(fieldJ, width);
109: }
110: );
111: }-*/;
112:
113: /**
114: * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed. This only takes effect if grow = true,
115: * and fires the autosize event.
116: */
117: public native void autoSize() /*-{
118: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
119: field.autoSize();
120: }-*/;
121:
122: /**
123: * Selects text in this field.
124: */
125: public native void selectText() /*-{
126: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
127: field.selectText();
128: }-*/;
129:
130: /**
131: * Selects text in this field.
132: *
133: * @param start the index where the selection should start
134: * @param end the index where the selection should end
135: */
136: public native void selectText(int start, int end) /*-{
137: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
138: field.selectText(start, end);
139: }-*/;
140:
141: /**
142: * Validates a value according to the field's validation rules and marks the field as invalid if the validation fails.
143: *
144: * @param value the value to valdiate
145: * @return true if valid
146: */
147: public native boolean validateValue(String value) /*-{
148: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
149: return field.validateValue(value);
150: }-*/;
151:
152: /**
153: * Returns the value of the text field.
154: *
155: * @return the text field value
156: */
157: public String getText() {
158: return getValueAsString();
159: }
160:
161: // --- config properties ---
162:
163: /**
164: * False to validate that the value length > 0 (defaults to true).
165: *
166: * @param allowBlank false to disallow blank
167: */
168: public void setAllowBlank(boolean allowBlank) {
169: setAttribute("allowBlank", allowBlank, true, true);
170: }
171:
172: /**
173: * Error text to display if the allow blank validation fails (defaults to "This field is required").
174: *
175: * @param blankText error message for blank field
176: */
177: public void setBlankText(String blankText) {
178: setAttribute("blankText", blankText, true, true);
179: }
180:
181: /**
182: * True to disable input keystroke filtering (defaults to false).
183: *
184: * @param disableKeyFilter true to disable
185: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
186: */
187: public void setDisableKeyFilter(boolean disableKeyFilter)
188: throws IllegalStateException {
189: setAttribute("disableKeyFilter", disableKeyFilter, true);
190: }
191:
192: /**
193: * The CSS class to apply to an empty field to style the emptyText (defaults to 'x-form-empty-field'). This class is
194: * automatically added and removed as needed depending on the current field value.
195: *
196: * @param emptyClass the empty field CSS class
197: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
198: */
199: public void setEmptyClass(String emptyClass)
200: throws IllegalStateException {
201: setAttribute("emptyClass", emptyClass, true);
202: }
203:
204: /**
205: * The default text to display in an empty field (defaults to null).
206: *
207: * @param emptyText the empty field text
208: */
209: public void setEmptyText(String emptyText) {
210: setAttribute("emptyText", emptyText, true, true);
211: }
212:
213: /**
214: * True if this field should automatically grow and shrink to its content.
215: *
216: * @param grow true to allow grow
217: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
218: */
219: public void setGrow(boolean grow) throws IllegalStateException {
220: setAttribute("grow", grow, true);
221: }
222:
223: /**
224: * The maximum width to allow when grow = true (defaults to 800).
225: *
226: * @param growMax the max width
227: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
228: */
229: public void setGrowMax(int growMax) throws IllegalStateException {
230: setAttribute("growMax", growMax, true);
231: }
232:
233: /**
234: * The minimum width to allow when grow = true (defaults to 30).
235: *
236: * @param growMin the minimum width
237: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
238: */
239: public void setGrowMin(int growMin) throws IllegalStateException {
240: setAttribute("growMin", growMin, true);
241: }
242:
243: /**
244: * An input mask regular expression that will be used to filter keystrokes that
245: * don't match (defaults to null).
246: *
247: * <br><br>
248: * <b>Note:</b> This property cannot be changed after the Component has been rendered.
249: *
250: * @param maskRe the mask regular expression
251: */
252: public native void setMaskRe(String maskRe) /*-{
253: var config = this.@com.gwtext.client.widgets.Component::config;
254: config['maskRe'] = new $wnd.RegExp(maskRe);
255: }-*/;
256:
257: public void setMaxLength(int maxLength) {
258: setAttribute("maxLength", maxLength, true, true);
259: }
260:
261: /**
262: * Error text to display if the maximum length validation fails.
263: * (defaults to "The maximum length for this field is {maxLength}")
264: *
265: * @param maxLengthText the max lenght error text
266: */
267: public void setMaxLengthText(String maxLengthText) {
268: setAttribute("maxLengthText", maxLengthText, true, true);
269: }
270:
271: /**
272: * Minimum input field length required (defaults to 0).
273: *
274: * @param minLength the min length
275: */
276: public void setMinLength(int minLength) {
277: setAttribute("minLength", minLength, true, true);
278: }
279:
280: /**
281: * Error text to display if the minimum length validation fails. (defaults to "The minimum length for this field is {minLength}")
282: *
283: * @param minLengthText the min length error text
284: */
285: public void setMinLengthText(String minLengthText) {
286: setAttribute("minLengthText", minLengthText, true, true);
287: }
288:
289: /**
290: * Set true if field is a password field.
291: *
292: * @param password true if passowrd field
293: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
294: */
295: public void setPassword(boolean password)
296: throws IllegalStateException {
297: if (password)
298: setInputType("password");
299: }
300:
301: /**
302: * A Regular Expressionto be tested against the field value during validation (defaults to null).
303: * If available, this regex will be evaluated only after the basic validators all return true, and will be passed
304: * the current field value. If the test fails, the field will be marked invalid using regexText.
305: *
306: *
307: * @param regex the regular expression
308: */
309: public void setRegex(String regex) {
310: setRegex(isCreated() ? getJsObj() : config, regex);
311: }
312:
313: private native void setRegex(JavaScriptObject config, String regex) /*-{
314: config['regex'] = new $wnd.RegExp(regex);
315: }-*/;
316:
317: /**
318: * The error text to display if regex is used and the test fails during validation (defaults to "").
319: *
320: * <br><br>
321: * <b>Note:</b> This property cannot be changed after the Component has been rendered.
322: *
323: * @param regexText the regexp text
324: */
325: public void setRegexText(String regexText) {
326: setAttribute("regexText", regexText, true, true);
327: }
328:
329: /**
330: * True to automatically select any existing field text when the field receives input focus (defaults to false).
331: *
332: * @param selectOnFocus true to select text on focus
333: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
334: */
335: public void setSelectOnFocus(boolean selectOnFocus)
336: throws IllegalStateException {
337: setAttribute("selectOnFocus", selectOnFocus, true);
338: }
339:
340: /**
341: * Set a custom Validator for the Field.
342: *
343: *
344: * @param validator the field validator
345: */
346: public void setValidator(Validator validator) {
347: setValidator(config, validator);
348: }
349:
350: private static String doValidate(Validator validator, String value) {
351: try {
352: return validator.validate(value) ? "true-val" : "false-val";
353: } catch (ValidationException e) {
354: return e.getMessage();
355: }
356: }
357:
358: private native void setValidator(JavaScriptObject config,
359: Validator validator) /*-{
360: config['validator'] = function(value) {
361: var val = @com.gwtext.client.widgets.form.TextField::doValidate(Lcom/gwtext/client/widgets/form/Validator;Ljava/lang/String;)(validator, value);
362: if(val === "true-val") {
363: return true;
364: } else if ( val === "false-val") {
365: return false;
366: } else {
367: return val;
368: }
369: }
370: }-*/;
371:
372: //http://extjs.com/forum/showthread.php?t=5717&highlight=vtype
373: /**
374: * A validation type name as defined in {@link VType} (defaults to null).
375: *
376: * @param vtype the validation type
377: */
378: public void setVtype(VType vtype) {
379: setAttribute("vtype", vtype.getVType(), true);
380: }
381:
382: /**
383: * The validation type text if the validation specified by {@link #setVtype(VType)} fails.
384: *
385: * @param vtypeText the vtype
386: */
387: public void setVtypeText(String vtypeText) {
388: setAttribute("vtypeText", vtypeText, true);
389: }
390:
391: }
|