KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > NumberField


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element;
25
26 import org.riotfamily.forms.DHTMLElement;
27 import org.riotfamily.forms.ErrorUtils;
28 import org.riotfamily.forms.resource.FormResource;
29 import org.riotfamily.forms.resource.ResourceElement;
30 import org.riotfamily.forms.resource.Resources;
31 import org.riotfamily.forms.resource.ScriptResource;
32 import org.springframework.util.Assert;
33 import org.springframework.util.StringUtils;
34
35 public class NumberField extends TextField implements DHTMLElement,
36         ResourceElement {
37
38     private Float JavaDoc minValue;
39
40     private Float JavaDoc maxValue;
41
42     private int precision = 2;
43
44     private boolean allowFloats;
45
46     private boolean spinner;
47
48     private float stepSize = 1;
49
50     private String JavaDoc unit;
51
52     public NumberField() {
53         setStyleClass("number");
54     }
55
56     public void setMaxValue(Float JavaDoc maxValue) {
57         this.maxValue = maxValue;
58     }
59
60     public void setMinValue(Float JavaDoc minValue) {
61         this.minValue = minValue;
62     }
63
64     public void setPrecision(int precision) {
65         this.precision = precision;
66     }
67
68     public void setSpinner(boolean spinner) {
69         this.spinner = spinner;
70     }
71
72     public void setStepSize(float stepSize) {
73         this.stepSize = stepSize;
74     }
75
76     public String JavaDoc getUnit() {
77         return unit;
78     }
79
80     public void setUnit(String JavaDoc unit) {
81         this.unit = unit;
82     }
83
84     public FormResource getResource() {
85         return new ScriptResource("riot-js/number-input.js", "NumberInput",
86                 Resources.PROTOTYPE);
87     }
88
89     public String JavaDoc getInitScript() {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91         sb.append("NumberInput.create('");
92         sb.append(getId());
93         sb.append("', {");
94         sb.append("required:").append(isRequired()).append(',');
95         appendValue(sb, "minValue", minValue);
96         appendValue(sb, "maxValue", maxValue);
97         if (allowFloats) {
98             sb.append("allowFloats:true,");
99             sb.append("precision:").append(precision).append(',');
100         }
101         if (unit != null) {
102             sb.append("unit:'").append(unit).append("',");
103         }
104         if (spinner) {
105             if (allowFloats) {
106                 sb.append("stepSize:").append(stepSize).append(',');
107             }
108             else {
109                 sb.append("stepSize:").append((int)stepSize).append(',');
110             }
111             sb.append("spinButtonTag:'div'");
112         }
113         else {
114             sb.append("spinner:false");
115         }
116         sb.append("});");
117         return sb.toString();
118     }
119
120     private static void appendValue(StringBuffer JavaDoc sb, String JavaDoc name, Float JavaDoc value) {
121         sb.append(name);
122         sb.append(':');
123         if (value != null) {
124             sb.append(value);
125         }
126         else {
127             sb.append(false);
128         }
129         sb.append(',');
130     }
131
132     protected void afterBindingSet() {
133         Class JavaDoc type = getEditorBinding().getPropertyType();
134         Assert.notNull(type, "Unable to determine type of property '" +
135                 getEditorBinding().getProperty() + "'");
136
137         if (type.equals(Float JavaDoc.class) || type.equals(Double JavaDoc.class)
138                 || type.equals(float.class) || type.equals(double.class)) {
139
140             allowFloats = true;
141         }
142         super.afterBindingSet();
143     }
144
145     protected void validate(boolean formSubmitted) {
146         super.validate(formSubmitted);
147         if (getEditorBinding().getPropertyType().isPrimitive()
148                 && !StringUtils.hasLength(getText())) {
149
150             ErrorUtils.reject(this, "required");
151         }
152     }
153
154
155
156 }
157
Popular Tags