View Javadoc

1   package org.informagen.echo.app;
2   
3   import org.informagen.echo.app.ActiveTextField;
4   
5   import nextapp.echo.app.TextField;
6   
7   import java.lang.NumberFormatException;
8   
9   /**
10   * 
11   */
12  
13  public class IntegerTextField extends ActiveTextField {
14  
15      public static final String PROPERTY_MINIMUM_VALUE = "minimumValue";
16      public static final String PROPERTY_MAXIMUM_VALUE = "maximumValue";
17  
18  
19      private int minimumValue = Integer.MIN_VALUE;
20      private int maximumValue = Integer.MAX_VALUE;
21  
22  
23      public IntegerTextField() {
24          this(Integer.MIN_VALUE, Integer.MAX_VALUE);
25      }
26  
27      public IntegerTextField(int minimumValue, int maximumValue) {
28          super();
29          
30          // Ensure min is less than max, otherwise swap them. 
31          //  Maybe better to throw illegalArgumentException?
32          
33          if(minimumValue < maximumValue) {
34              setMinimumValue(minimumValue);
35              setMaximumValue(maximumValue);
36          } else {
37              setMinimumValue(maximumValue);
38              setMaximumValue(minimumValue);
39          }
40      }
41  
42      public boolean isValid() {
43          return !isEmpty(getText()) && isInRange();
44      }
45  
46      public boolean isInRange() {
47          
48          int value = getValue();
49          int min = getMinimumValue();
50          int max = getMaximumValue();
51      
52          return (min <= value) && (value <= max);
53      }
54  
55  
56      /**
57       * Set the integer value as an int
58       * 
59       * @param value the int value
60      */
61         
62      public void setValue(int value) {
63          setText(Integer.toString(value));
64      }
65  
66  
67      /**
68       * Get the value as an int
69       * 
70       * @returns int the int value
71      */
72         
73      public int getValue() {
74          int value;
75          try {
76              value = Integer.parseInt(getText());
77          } catch (NumberFormatException nfe) {
78              value = 0;
79          }
80          
81          return value;
82      }
83  
84      
85      /**
86       * Set the minimum allowed value 
87       * 
88       * @param minValue the minimum value
89      */
90         
91      public void setMinimumValue(int minimumValue) {
92          int oldValue = this.minimumValue;
93          this.minimumValue = minimumValue;
94          firePropertyChange(PROPERTY_MINIMUM_VALUE, oldValue, minimumValue);
95      }
96  
97  
98      /**
99       * Get the minimum value for this integer field
100      * 
101      * @returns int the minimum value
102     */
103        
104     public int getMinimumValue() {
105         return minimumValue;
106     }
107 
108     
109     /**
110      * Set the maximum allowed value
111      * 
112      * @param maxValue the maximum value
113     */
114        
115     public void setMaximumValue(int maximumValue) {
116         int oldValue = this.maximumValue;
117         this.maximumValue = maximumValue;
118         firePropertyChange(PROPERTY_MAXIMUM_VALUE, oldValue, maximumValue);
119     }
120 
121 
122     /**
123      * Get the maximum value for this integer field
124      * 
125      * @returns int the maximum value
126     */
127        
128     public int getMaximumValue() {
129         return maximumValue;
130     }
131 
132 
133 }
134