1 7 8 package org.jdesktop.swing.data; 9 10 import java.util.Locale ; 11 12 import javax.swing.UIDefaults ; 13 14 41 42 public class NumberMetaData extends MetaData { 43 44 protected Number minimum = null; 45 protected Number maximum = null; 46 protected boolean currency = false; 47 48 private Validator rangeValidator = null; 49 50 57 public NumberMetaData() { 58 this("numbervalue"); 59 } 60 61 66 public NumberMetaData(String name) { 67 super(name); 68 this.klass = Integer .class; 69 } 70 71 77 public NumberMetaData(String name, Class klass) { 78 this(name); 79 this.klass = klass; 80 } 81 82 90 public NumberMetaData(String name, Class klass, String label) { 91 this(name, klass); 92 this.label = label; 93 } 94 95 102 public Number getMinimum() { 103 return minimum; 104 } 105 106 115 public void setMinimum(Number minimum) { 116 if (klass != minimum.getClass()) { 117 throw new IllegalArgumentException (getName() + ": minimum value is class "+ 118 minimum.getClass().getName() + 119 " but should be "+ getClass().getName()); 120 } 121 Number oldMinimum = this.minimum; 122 this.minimum = minimum; 123 setupRangeValidator(); 124 firePropertyChange("minimum", oldMinimum, minimum); 125 } 126 127 134 public Number getMaximum() { 135 return maximum; 136 } 137 138 147 public void setMaximum(Number maximum) { 148 if (getElementClass() != maximum.getClass()) { 149 throw new IllegalArgumentException (getName() + 150 ": maximum value is class " + 151 maximum.getClass().getName() + 152 " but should be " + 153 getClass().getName()); 154 } 155 Number oldMaximum = this.maximum; 156 this.maximum = maximum; 157 setupRangeValidator(); 158 firePropertyChange("maximum", oldMaximum, maximum); 159 } 160 161 168 public boolean isCurrency() { 169 return currency; 170 } 171 172 177 public void setCurrency(boolean currency) { 178 boolean oldCurrency = this.currency; 179 this.currency = currency; 180 firePropertyChange("currency", oldCurrency, currency); 181 } 182 183 private void setupRangeValidator() { 184 if (maximum != null || minimum != null) { 185 if (rangeValidator == null) { 186 rangeValidator = getRangeValidator(); 187 addValidator(rangeValidator); 188 } 189 } else if (rangeValidator != null) { 190 removeValidator(rangeValidator); 191 rangeValidator = null; 192 } 193 } 194 195 private Validator getRangeValidator() { 196 if (klass.equals(Integer .class)) { 197 return new IntegerRangeValidator(); 198 } else if (klass.equals(Long .class)) { 199 return new LongRangeValidator(); 200 } else if (klass.equals(Float .class)) { 201 return new FloatRangeValidator(); 202 } else if (klass.equals(Short .class)) { 203 return new ShortRangeValidator(); 204 } else if (klass.equals(Double .class)) { 205 return new DoubleRangeValidator(); 206 } 207 return null; 208 } 209 210 213 private class IntegerRangeValidator implements Validator { 214 215 public boolean validate(Object value, Locale locale, String [] error) { 216 217 int intValue = ((Integer )value).intValue(); 218 if (maximum != null && 219 intValue > ((Integer )maximum).intValue()) { 220 error[0] = getName()+ ": value " + intValue + " exceeds maximum "+ 221 ((Integer )maximum).intValue(); 222 return false; 223 } 224 if (minimum != null && 225 intValue < ((Integer )minimum).intValue()) { 226 error[0] = getName() + ": value " + intValue + " is less than the minimum" + 227 ((Integer )minimum).intValue(); 228 return false; 229 } 230 return true; 231 } 232 } 233 234 private class LongRangeValidator implements Validator { 235 236 public boolean validate(Object value, Locale locale, String [] error) { 237 238 long longValue = ((Long )value).longValue(); 239 if (maximum != null && 240 longValue > ((Long )maximum).longValue()) { 241 error[0] = getName()+ ": value " + longValue + " exceeds maximum "+ 242 ((Long )maximum).longValue(); 243 return false; 244 } 245 if (minimum != null && 246 longValue < ((Long )minimum).longValue()) { 247 error[0] = getName() + ": value " + longValue + " is less than the minimum" + 248 ((Long )minimum).longValue(); 249 return false; 250 } 251 return true; 252 } 253 } 254 255 private class ShortRangeValidator implements Validator { 256 257 public boolean validate(Object value, Locale locale, String [] error) { 258 259 short shortValue = ((Short )value).shortValue(); 260 if (maximum != null && 261 shortValue > ((Short )maximum).shortValue()) { 262 error[0] = getName()+ ": value " + shortValue + " exceeds maximum "+ 263 ((Short )maximum).shortValue(); 264 return false; 265 } 266 if (minimum != null && 267 shortValue < ((Short )minimum).shortValue()) { 268 error[0] = getName() + ": value " + shortValue + " is less than the minimum" + 269 ((Short )minimum).shortValue(); 270 return false; 271 } 272 return true; 273 } 274 } 275 276 private class FloatRangeValidator implements Validator { 277 278 public boolean validate(Object value, Locale locale, String [] error) { 279 280 float floatValue = ((Float )value).floatValue(); 281 if (maximum != null && 282 floatValue > ((Float )maximum).floatValue()) { 283 error[0] = getName()+ ": value " + floatValue + " exceeds maximum "+ 284 ((Float )maximum).floatValue(); 285 return false; 286 } 287 if (minimum != null && 288 floatValue < ((Float )minimum).floatValue()) { 289 error[0] = getName() + ": value " + floatValue + " is less than the minimum" + 290 ((Float )minimum).floatValue(); 291 return false; 292 } 293 return true; 294 } 295 } 296 297 private class DoubleRangeValidator implements Validator { 298 299 public boolean validate(Object value, Locale locale, String [] error) { 300 301 double doubleValue = ((Double )value).doubleValue(); 302 if (maximum != null && 303 doubleValue > ((Double )maximum).doubleValue()) { 304 error[0] = getName()+ ": value " + doubleValue + " exceeds maximum "+ 305 ((Double )maximum).doubleValue(); 306 return false; 307 } 308 if (minimum != null && 309 doubleValue < ((Double )minimum).doubleValue()) { 310 error[0] = getName() + ": value " + doubleValue + " is less than the minimum" + 311 ((Double )minimum).doubleValue(); 312 return false; 313 } 314 return true; 315 } 316 } 317 } 318 | Popular Tags |