1 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 minValue; 39 40 private Float 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 unit; 51 52 public NumberField() { 53 setStyleClass("number"); 54 } 55 56 public void setMaxValue(Float maxValue) { 57 this.maxValue = maxValue; 58 } 59 60 public void setMinValue(Float 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 getUnit() { 77 return unit; 78 } 79 80 public void setUnit(String 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 getInitScript() { 90 StringBuffer sb = new StringBuffer (); 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 sb, String name, Float 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 type = getEditorBinding().getPropertyType(); 134 Assert.notNull(type, "Unable to determine type of property '" + 135 getEditorBinding().getProperty() + "'"); 136 137 if (type.equals(Float .class) || type.equals(Double .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 |