001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import org.riotfamily.forms.DHTMLElement;
027: import org.riotfamily.forms.resource.FormResource;
028: import org.riotfamily.forms.resource.ResourceElement;
029: import org.riotfamily.forms.resource.Resources;
030: import org.riotfamily.forms.resource.ScriptResource;
031: import org.springframework.util.Assert;
032:
033: public class NumberField extends TextField implements DHTMLElement,
034: ResourceElement {
035:
036: private Float minValue;
037:
038: private Float maxValue;
039:
040: private int precision = 2;
041:
042: private boolean allowFloats;
043:
044: private boolean spinner;
045:
046: private float stepSize = 1;
047:
048: private String unit;
049:
050: public NumberField() {
051: setStyleClass("number");
052: }
053:
054: public void setMaxValue(Float maxValue) {
055: this .maxValue = maxValue;
056: }
057:
058: public void setMinValue(Float minValue) {
059: this .minValue = minValue;
060: }
061:
062: public void setPrecision(int precision) {
063: this .precision = precision;
064: }
065:
066: public void setSpinner(boolean spinner) {
067: this .spinner = spinner;
068: }
069:
070: public void setStepSize(float stepSize) {
071: this .stepSize = stepSize;
072: }
073:
074: public String getUnit() {
075: return unit;
076: }
077:
078: public void setUnit(String unit) {
079: this .unit = unit;
080: }
081:
082: public FormResource getResource() {
083: return new ScriptResource("riot-js/number-input.js",
084: "NumberInput", Resources.PROTOTYPE);
085: }
086:
087: public String getInitScript() {
088: StringBuffer sb = new StringBuffer();
089: sb.append("NumberInput.create('");
090: sb.append(getId());
091: sb.append("', {");
092: sb.append("required:").append(isRequired()).append(',');
093: appendValue(sb, "minValue", minValue);
094: appendValue(sb, "maxValue", maxValue);
095: if (allowFloats) {
096: sb.append("allowFloats:true,");
097: sb.append("precision:").append(precision).append(',');
098: }
099: if (unit != null) {
100: sb.append("unit:'").append(unit).append("',");
101: }
102: if (spinner) {
103: if (allowFloats) {
104: sb.append("stepSize:").append(stepSize).append(',');
105: } else {
106: sb.append("stepSize:").append((int) stepSize).append(
107: ',');
108: }
109: sb.append("spinButtonTag:'div'");
110: } else {
111: sb.append("spinner:false");
112: }
113: sb.append("});");
114: return sb.toString();
115: }
116:
117: private static void appendValue(StringBuffer sb, String name,
118: Float value) {
119: sb.append(name);
120: sb.append(':');
121: if (value != null) {
122: sb.append(value);
123: } else {
124: sb.append(false);
125: }
126: sb.append(',');
127: }
128:
129: protected void afterBindingSet() {
130: Class type = getEditorBinding().getPropertyType();
131: Assert.notNull(type, "Unable to determine type of property '"
132: + getEditorBinding().getProperty() + "'");
133:
134: if (type.equals(Float.class) || type.equals(Double.class)
135: || type.equals(float.class)
136: || type.equals(double.class)) {
137:
138: allowFloats = true;
139: }
140: if (type.isPrimitive()) {
141: setRequired(true);
142: }
143: super.afterBindingSet();
144: }
145:
146: }
|