View Javadoc

1   package org.informagen.echo.app;
2   
3   import nextapp.echo.app.Alignment;
4   import nextapp.echo.app.Color;
5   import nextapp.echo.app.TextField;
6   
7   import java.lang.IllegalArgumentException;
8   
9   
10  public abstract class ActiveTextField extends TextField {
11      
12  
13      public static final String PROPERTY_MESSAGE = "message";
14      public static final String PROPERTY_VALID_MESSAGE = "validMessage";
15      public static final String PROPERTY_INVALID_MESSAGE = "invalidMessage";
16      public static final String PROPERTY_MESSAGE_POSITION = "messagePosition";
17  
18      public static final String PROPERTY_EMPTY_ICON = "emptyIcon";
19      public static final String PROPERTY_VALID_ICON = "validIcon";
20      public static final String PROPERTY_INVALID_ICON = "invalidIcon";
21      public static final String PROPERTY_ICON_POSITION = "iconPosition";
22  
23      public static final String PROPERTY_INVALID_FOREGROUND_COLOR = "invalidForegroundColor";
24      public static final String PROPERTY_INVALID_BACKGROUND_COLOR = "invalidBackgroundColor";
25  
26      private boolean required = false;
27      private String message = null;
28      private String validMessage = null;
29      private String invalidMessage = null;
30  
31      /**
32      */
33         
34      public void setRequired(boolean required) {
35          this.required = required;
36      }
37  
38  
39      /**
40      */
41         
42      public boolean isRequired() {
43          return required;
44      }
45  
46  
47      abstract boolean isValid();
48  
49      public void setEmptyIcon(ActiveTextFieldIcon icon) {
50          set(PROPERTY_EMPTY_ICON, (icon != null) ? icon.getName() : ActiveTextFieldIcon.EMPTY.getName());
51      }
52  
53      public void setValidIcon(ActiveTextFieldIcon icon) {
54          set(PROPERTY_VALID_ICON, (icon != null) ? icon.getName() : ActiveTextFieldIcon.GOOD.getName());
55      }
56  
57      public void setInvalidIcon(ActiveTextFieldIcon icon) {
58          set(PROPERTY_INVALID_ICON, (icon != null) ? icon.getName() : ActiveTextFieldIcon.ERROR.getName());
59      }
60  
61  
62      /**
63       * Set the position of the icon 
64       * 
65       * @param iconPosition the status icon position
66      */
67         
68      public void setIconPosition(int iconPosition) {
69          
70          switch (iconPosition) {
71              
72              // Allowed positions and default
73              case Alignment.LEFT:
74              case Alignment.RIGHT:
75              case Alignment.TRAILING:
76              case Alignment.LEADING:
77                  break;
78                  
79              case Alignment.DEFAULT:
80                  iconPosition = Alignment.TRAILING;
81                  break;
82                  
83              default:
84                  throw new IllegalArgumentException("Icon must be positioned on the RIGHT, LEFT, LEADING, TRAILING or DEFAULT");
85          }
86          
87          // Alignment(horizontal, vertical)
88          set(PROPERTY_ICON_POSITION, new Alignment(iconPosition, Alignment.DEFAULT));
89      }
90  
91  
92      /**
93       * Get the icon position
94       * 
95       * @returns Alignment the icon position relative to the input field
96      */
97         
98      public int getIconPosition() {
99          Alignment iconPosition = (Alignment)get(PROPERTY_ICON_POSITION);
100         return (iconPosition != null) ? iconPosition.getHorizontal() : Alignment.TRAILING;
101     }
102 
103 
104     /** ----------------------------------------------------------------------------------------
105     */
106        
107     public void setMessage(String message) {
108         String oldValue = this.message;
109         this.message = message;
110         firePropertyChange(PROPERTY_MESSAGE, oldValue, message);
111     }
112 
113 
114     /**
115     */
116        
117     public String getMessage() {
118         return message;
119     }
120 
121     /** ----------------------------------------------------------------------------------------
122     */
123        
124     public void setValidMessage(String validMessage) {
125         String oldValue = this.validMessage;
126         this.validMessage = validMessage;
127         firePropertyChange(PROPERTY_VALID_MESSAGE, oldValue, validMessage);
128     }
129 
130 
131     /**
132     */
133        
134     public String getValidMessage() {
135         return validMessage;
136     }
137 
138 
139     /** ----------------------------------------------------------------------------------------
140     */
141        
142     public void setInvalidMessage(String invalidMessage) {
143         String oldValue = this.invalidMessage;
144         this.invalidMessage = invalidMessage;
145         firePropertyChange(PROPERTY_INVALID_MESSAGE, oldValue, invalidMessage);
146     }
147 
148 
149     /**
150     */
151        
152     public String getInvalidMessage() {
153         return invalidMessage;
154     }
155 
156     /**
157      * Set the position of the message 
158      * 
159      * @param messagePosition the status icon position
160     */
161        
162     public void setMessagePosition(int messagePosition) {
163         
164         switch (messagePosition) {
165             
166             // Allowed positions and default
167             case Alignment.TOP:
168             case Alignment.BOTTOM:
169                 break;
170                 
171             case Alignment.DEFAULT:
172                 messagePosition = Alignment.BOTTOM;
173                 break;
174                 
175             default:
176                 throw new IllegalArgumentException("Message must be positioned on the TOP, BOTTOM or DEFAULT");
177         }
178         
179         // Alignment(horizontal, vertical)
180         set(PROPERTY_MESSAGE_POSITION, new Alignment(Alignment.DEFAULT, messagePosition));
181     }
182 
183 
184     /**
185      * Get the message position
186      * 
187      * @returns Alignment the message position relative to the input field
188     */
189        
190     public int getMessagePosition() {
191         Alignment messagePosition = (Alignment)get(PROPERTY_MESSAGE_POSITION);
192         return (messagePosition != null) ? messagePosition.getVertical() : Alignment.BOTTOM;
193     }
194 
195 
196     /** ----------------------------------------------------------------------------------------
197     */
198        
199     public void setInvalidForegroundColor(Color color) {
200         set(PROPERTY_INVALID_FOREGROUND_COLOR, color);
201     }
202 
203 
204     /**
205     */
206        
207     public Color getInvalidForegroundColor() {
208         return (Color)get(PROPERTY_INVALID_FOREGROUND_COLOR);
209     }
210 
211     /** ----------------------------------------------------------------------------------------
212     */
213        
214     public void setInvalidBackgroundColor(Color color) {
215         set(PROPERTY_INVALID_BACKGROUND_COLOR, color);
216     }
217 
218 
219     /**
220     */
221        
222     public Color getInvalidBackgroundColor() {
223         return (Color)get(PROPERTY_INVALID_BACKGROUND_COLOR);
224     }
225 
226 
227     //-----------------------------------------------------------------------------------------
228 
229     protected static boolean isEmpty(String string) {
230         return string == null || string.length() == 0;
231     }
232 
233 }
234 
235