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.form;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011:
012: /**
013: * Numeric text field that provides automatic keystroke filtering and numeric validation.
014: */
015: public class NumberField extends TextField {
016:
017: //tempory fix until fix gets rolled into Ext 1.1.2
018: //see http://extjs.com/forum/showthread.php?p=79055#post79055
019: //see http://code.google.com/p/gwt-ext/issues/detail?id=76&can=1
020: //see http://groups.google.com/group/gwt-ext/browse_thread/thread/36cf6032ad34feeb
021: static {
022: init();
023: }
024:
025: private native static void init() /*-{
026: $wnd.Ext.form.NumberField.prototype.fixPrecision = function(value){
027: var nan = isNaN(value);
028: if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
029: return nan ? '' : value;
030: }
031: return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
032: }
033: }-*/;
034:
035: /**
036: * Creates a new NumberField.
037: */
038: public NumberField() {
039: }
040:
041: public NumberField(String fieldLabel) {
042: super (fieldLabel);
043: }
044:
045: public NumberField(String fieldLabel, String name) {
046: super (fieldLabel, name);
047: }
048:
049: public NumberField(String fieldLabel, String name, int width) {
050: super (fieldLabel, name, width);
051: }
052:
053: public NumberField(String fieldLabel, String name, int width,
054: float value) {
055: super (fieldLabel, name, width);
056: setValue(value);
057: }
058:
059: public NumberField(JavaScriptObject jsObj) {
060: super (jsObj);
061: }
062:
063: protected native JavaScriptObject create(JavaScriptObject jsObj)/*-{
064: return new $wnd.Ext.form.NumberField(jsObj);
065: }-*/;
066:
067: /**
068: * Returns the field value.
069: *
070: * @return the field value
071: */
072: public native Number getValue() /*-{
073: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
074: var value = field.getValue();
075: return (value == null || value === undefined || value === '') ? null : $wnd.GwtExt.convertToJavaType(parseFloat(value));
076: }-*/;
077:
078: /**
079: * Sets the fields value.
080: *
081: * @param value the field value
082: */
083: public native void setValue(float value) /*-{
084: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
085: field.setValue(value);
086: }-*/;
087:
088: /**
089: * Sets the fields value.
090: *
091: * @param value the field value
092: */
093: public void setValue(Number value) {
094: if (value == null) {
095: setNullValue();
096: } else {
097: setValue(value.floatValue());
098: }
099: }
100:
101: /**
102: * Sets the fields value to null.
103: *
104: */
105: private native void setNullValue() /*-{
106: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
107: field.setValue(null);
108: }-*/;
109:
110: /**
111: * Validates a value according to the field's validation rules and marks the field as invalid if the validation fails.
112: *
113: * @param value the value to validate
114: * @return true if valid
115: */
116: public boolean validateValue(Number value) {
117: return value == null ? validateNullValue()
118: : validateValue(value.floatValue());
119: }
120:
121: /**
122: * Validates a null value according to the field's validation rules and marks the field as invalid if the validation fails.
123: *
124: * @return true if valid
125: */
126: private native boolean validateNullValue() /*-{
127: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
128: return field.validateValue(null);
129: }-*/;
130:
131: /**
132: * Validates a value according to the field's validation rules and marks the field as invalid if the validation fails.
133: *
134: * @param value the value to validate
135: * @return true if valid
136: */
137: public native boolean validateValue(float value) /*-{
138: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
139: return field.validateValue(value);
140: }-*/;
141:
142: // --- config properties ---
143: public String getXType() {
144: return "numberfield";
145: }
146:
147: /**
148: * False to disallow decimal values (defaults to true).
149: *
150: * @param allowDecimals false to disallow decimal values
151: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
152: */
153: public void setAllowDecimals(boolean allowDecimals)
154: throws IllegalStateException {
155: setAttribute("allowDecimals", allowDecimals, true);
156: }
157:
158: /**
159: * False to prevent entering a negative sign (defaults to true).
160: *
161: * @param allowNegative false to prevent entering a negative sign
162: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
163: */
164: public void setAllowNegative(boolean allowNegative)
165: throws IllegalStateException {
166: setAttribute("allowNegative", allowNegative, true);
167: }
168:
169: /**
170: * The maximum precision to display after the decimal separator (defaults to 2).
171: *
172: * @param decimalPrecision the decimal precision
173: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
174: */
175: public void setDecimalPrecision(int decimalPrecision)
176: throws IllegalStateException {
177: setAttribute("decimalPrecision", decimalPrecision, true);
178: }
179:
180: /**
181: * Character(s) to allow as the decimal separator (defaults to '.').
182: *
183: * @param decimalSeparator decimal separator
184: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
185: */
186: public void setDecimalSeparator(String decimalSeparator)
187: throws IllegalStateException {
188: setAttribute("decimalSeparator", decimalSeparator, true);
189: }
190:
191: /**
192: * Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}").
193: *
194: * @param maxText the max error text
195: */
196: public void setMaxText(String maxText) {
197: setAttribute("maxText", maxText, true, true);
198: }
199:
200: /**
201: * The maximum allowed value (defaults to Number.MAX_VALUE).
202: *
203: * @param maxValue the max value
204: */
205: public void setMaxValue(int maxValue) {
206: setAttribute("maxValue", maxValue, true, true);
207: }
208:
209: /**
210: * Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}").
211: *
212: * @param minText the min error text
213: */
214: public void setMinText(String minText) {
215: setAttribute("minText", minText, true, true);
216: }
217:
218: /**
219: * The minimum allowed value (defaults to Number.NEGATIVE_INFINITY).
220: *
221: * @param minValue the min value
222: */
223: public void setMinValue(int minValue) {
224: setAttribute("minValue", minValue, true, true);
225: }
226:
227: /**
228: * Error text to display if the value is not a valid number. For example, this can happen if a valid character like
229: * '.' or '-' is left in the field with no number (defaults to "throws IllegalArgumentException {value} is not a valid number").
230: *
231: * @param nanText the Nan text
232: */
233: public void setNanText(String nanText) {
234: setAttribute("nanText", nanText, true, true);
235: }
236: }
|