1 package net.suberic.util.gui.propedit; 2 3 7 public class NumberFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener { 8 String originalValue; 9 10 boolean checkMin = false; 11 int min; 12 boolean checkMax = false; 13 int max; 14 15 18 public void configureListener(String key, String property, String propertyBase, String editorTemplate, PropertyEditorManager manager) { 19 originalValue = manager.getProperty(property, ""); 20 21 String minProp = manager.getProperty(key + ".min", ""); 22 if (minProp != "") { 23 try { 24 min = Integer.parseInt(minProp); 25 checkMin = true; 26 } catch (NumberFormatException nfe) { 27 } 28 } 29 30 String maxProp = manager.getProperty(key + ".max", ""); 31 if (maxProp != "") { 32 try { 33 max = Integer.parseInt(maxProp); 34 checkMax = true; 35 } catch (NumberFormatException nfe) { 36 } 37 } 38 } 39 40 47 public void propertyChanging(PropertyEditorUI source, String property, String newValue) throws PropertyValueVetoException { 48 try { 49 if (newValue != null && newValue.length() > 0 && ! newValue.equals(originalValue)) { 50 int currentValue = Integer.parseInt(newValue); 51 if (checkMin) { 52 if (currentValue < min) { 53 throw new PropertyValueVetoException(property, newValue, "value must be at least " + min, this); 54 } 55 } 56 57 if (checkMax) { 58 if (currentValue > max) { 59 throw new PropertyValueVetoException(property, newValue, "value must be at most " + max, this); 60 } 61 } 62 } 63 } catch (NumberFormatException nfe) { 64 throw new PropertyValueVetoException(property, newValue, "entry must be a number.", this); 65 } 66 } 67 68 } 69 | Popular Tags |