View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
3    * not use this file except in compliance with the License. You may obtain a copy of the License at
4    * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
5    * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
6    * implied. See the License for the specific language governing permissions and limitations under the License.
7    */
8   
9   package org.kuali.student.common.ui.client.widgets;
10  
11  import com.google.gwt.dom.client.Element;
12  import com.google.gwt.event.dom.client.BlurEvent;
13  import com.google.gwt.event.dom.client.BlurHandler;
14  import com.google.gwt.event.dom.client.FocusEvent;
15  import com.google.gwt.event.dom.client.FocusHandler;
16  import com.google.gwt.event.dom.client.KeyUpEvent;
17  import com.google.gwt.event.dom.client.KeyUpHandler;
18  import com.google.gwt.event.dom.client.MouseOutEvent;
19  import com.google.gwt.event.dom.client.MouseOutHandler;
20  import com.google.gwt.event.dom.client.MouseOverEvent;
21  import com.google.gwt.event.dom.client.MouseOverHandler;
22  import com.google.gwt.event.logical.shared.ValueChangeEvent;
23  import com.google.gwt.event.logical.shared.ValueChangeHandler;
24  import com.google.gwt.user.client.ui.Label;
25  import com.google.gwt.user.client.ui.TextArea;
26  
27  /**
28   * KSTextArea wraps gwt TextArea. This class provides most of the same functionality, but sets KS css styles for its default
29   * look and a variety of TextArea events (for improved browser compatibility and customizability).
30   * 
31   * @author Kuali Student Team
32   */
33  public class KSTextArea extends TextArea implements HasWatermark {
34      private boolean hasWatermark = false;
35      private boolean watermarkShowing = false;
36      private String watermarkText;
37      private int left = 0;
38      private int maxLength = 0;
39  
40      /**
41       * Creates a new empty text area.
42       */
43      public KSTextArea() {
44          super();
45          setupDefaultStyle();
46      }
47  
48      /**
49       * Creates a new text area using the text area element specified.
50       * 
51       * @param element
52       *            a <TextArea> element
53       */
54      public KSTextArea(Element element) {
55          super(element);
56          setupDefaultStyle();
57      }
58  
59      public KSTextArea(final Label countLabel, final int maximumChar) {
60          super();
61          setupDefaultStyle();
62          this.setMaxLength(maximumChar);
63          setLabel(countLabel);
64          this.addKeyUpHandler(new KeyUpHandler() {
65  
66              @Override
67              public void onKeyUp(KeyUpEvent event) {
68                  setLabel(countLabel);
69              }
70          });
71  
72          this.addValueChangeHandler(new ValueChangeHandler<String>() {
73  
74              @Override
75              public void onValueChange(ValueChangeEvent<String> event) {
76                  setLabel(countLabel);
77              }
78          });
79  
80      }
81      
82      public KSTextArea(final Label countLabel) {
83          super();
84          setupDefaultStyle();
85          countLabel.setText(this.maxLength + " characters left");
86          this.addKeyUpHandler(new KeyUpHandler() {
87  
88              @Override
89              public void onKeyUp(KeyUpEvent event) {
90                  setLabel(countLabel);
91              }
92          });
93  
94          this.addValueChangeHandler(new ValueChangeHandler<String>() {
95  
96              @Override
97              public void onValueChange(ValueChangeEvent<String> event) {
98                  setLabel(countLabel);
99              }
100         });
101 
102     }
103 
104     /**
105      * This method sets the default style for the text area and text area events.
106      */
107     private void setupDefaultStyle() {
108         addStyleName("KS-Textarea");
109 
110         this.addBlurHandler(new BlurHandler() {
111             public void onBlur(BlurEvent event) {
112                 KSTextArea.this.removeStyleName("KS-Textarea-Focus");
113 
114             }
115         });
116 
117         this.addFocusHandler(new FocusHandler() {
118             public void onFocus(FocusEvent event) {
119                 KSTextArea.this.addStyleName("KS-Textarea-Focus");
120 
121             }
122         });
123 
124         this.addMouseOverHandler(new MouseOverHandler() {
125             public void onMouseOver(MouseOverEvent event) {
126                 KSTextArea.this.addStyleName("KS-Textarea-Hover");
127 
128             }
129         });
130 
131         this.addMouseOutHandler(new MouseOutHandler() {
132 
133             public void onMouseOut(MouseOutEvent event) {
134                 KSTextArea.this.removeStyleName("KS-Textarea-Hover");
135 
136             }
137 
138         });
139 
140     }
141 
142     @Override
143     public void setWatermarkText(String text) {
144         if (!hasWatermark) {
145             hasWatermark = true;
146             watermarkText = text;
147             if (getText() == null || getText().isEmpty()) {
148                 addStyleName("watermark-text");
149                 KSTextArea.super.setText(watermarkText);
150                 watermarkShowing = true;
151             }
152 
153             this.addFocusHandler(new FocusHandler() {
154 
155                 @Override
156                 public void onFocus(FocusEvent event) {
157                     if (watermarkShowing) {
158                         removeStyleName("watermark-text");
159                         KSTextArea.super.setText("");
160                         watermarkShowing = false;
161                     }
162                 }
163             });
164 
165             this.addBlurHandler(new BlurHandler() {
166 
167                 @Override
168                 public void onBlur(BlurEvent event) {
169                     if (getText() == null || getText().isEmpty()) {
170                         addStyleName("watermark-text");
171                         KSTextArea.super.setText(watermarkText);
172                         watermarkShowing = true;
173                     }
174                 }
175             });
176         } else {
177             watermarkText = text;
178             if (getText() == null || getText().isEmpty()) {
179                 addStyleName("watermark-text");
180                 KSTextArea.super.setText(watermarkText);
181                 watermarkShowing = true;
182             }
183         }
184     }
185 
186     @Override
187     public boolean hasWatermark() {
188         return hasWatermark;
189     }
190 
191     @Override
192     public boolean watermarkShowing() {
193         return watermarkShowing;
194     }
195 
196     @Override
197     public String getText() {
198         if (!watermarkShowing) {
199             return super.getText();
200         }
201         return null;
202     }
203 
204     @Override
205     public String getValue() {
206         if (!watermarkShowing) {
207             return super.getValue();
208         }
209         return null;
210     }
211 
212     @Override
213     public void setValue(String value) {
214         if (hasWatermark) {
215             if (value == null || (value != null && value.isEmpty())) {
216                 super.setValue(watermarkText);
217                 addStyleName("watermark-text");
218                 watermarkShowing = true;
219             } else {
220                 super.setValue(value);
221                 removeStyleName("watermark-text");
222                 watermarkShowing = false;
223             }
224         } else {
225             super.setValue(value);
226         }
227     }
228 
229     @Override
230     public void setText(String text) {
231         String oldValue = super.getText();
232         if (hasWatermark) {
233             if (text == null || (text != null && text.isEmpty())) {
234                 super.setText(watermarkText);
235                 addStyleName("watermark-text");
236                 watermarkShowing = true;
237             } else {
238                 super.setText(text);
239                 removeStyleName("watermark-text");
240                 watermarkShowing = false;
241             }
242         } else {
243             super.setText(text);
244         }
245         ValueChangeEvent.fireIfNotEqual(this, oldValue, text);
246     }
247 
248     public void setLabel(final Label countLabel) {
249         left = this.maxLength - KSTextArea.this.getText().length();
250         if (KSTextArea.this.getText().length() > this.maxLength) {
251             countLabel.setText("Please remove " + left * -1 + " characters");
252         } else {
253             countLabel.setText(left + " characters left");
254         }
255         if ((left <= (this.maxLength * 0.1)) || (left <= 10)) {
256             countLabel.getElement().setAttribute("style", "color: red;");
257         } else {
258             countLabel.getElement().removeAttribute("style");
259         }
260     }
261 
262     public void setMaxLength(int maxLength) {
263         this.maxLength = maxLength;
264     }
265 
266     public int getMaxLength() {
267         return this.maxLength;
268     }
269     
270 }