1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
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 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class KSTextArea extends TextArea implements HasWatermark { |
34 | 0 | private boolean hasWatermark = false; |
35 | 0 | private boolean watermarkShowing = false; |
36 | |
private String watermarkText; |
37 | 0 | private int left = 0; |
38 | 0 | private int maxLength = 0; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public KSTextArea() { |
44 | 0 | super(); |
45 | 0 | setupDefaultStyle(); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public KSTextArea(Element element) { |
55 | 0 | super(element); |
56 | 0 | setupDefaultStyle(); |
57 | 0 | } |
58 | |
|
59 | |
public KSTextArea(final Label countLabel, final int maximumChar) { |
60 | 0 | super(); |
61 | 0 | setupDefaultStyle(); |
62 | 0 | this.setMaxLength(maximumChar); |
63 | 0 | setLabel(countLabel); |
64 | 0 | this.addKeyUpHandler(new KeyUpHandler() { |
65 | |
|
66 | |
@Override |
67 | |
public void onKeyUp(KeyUpEvent event) { |
68 | 0 | setLabel(countLabel); |
69 | 0 | } |
70 | |
}); |
71 | |
|
72 | 0 | this.addValueChangeHandler(new ValueChangeHandler<String>() { |
73 | |
|
74 | |
@Override |
75 | |
public void onValueChange(ValueChangeEvent<String> event) { |
76 | 0 | setLabel(countLabel); |
77 | 0 | } |
78 | |
}); |
79 | |
|
80 | 0 | } |
81 | |
|
82 | |
public KSTextArea(final Label countLabel) { |
83 | 0 | super(); |
84 | 0 | setupDefaultStyle(); |
85 | 0 | countLabel.setText(this.maxLength + " characters left"); |
86 | 0 | this.addKeyUpHandler(new KeyUpHandler() { |
87 | |
|
88 | |
@Override |
89 | |
public void onKeyUp(KeyUpEvent event) { |
90 | 0 | setLabel(countLabel); |
91 | 0 | } |
92 | |
}); |
93 | |
|
94 | 0 | this.addValueChangeHandler(new ValueChangeHandler<String>() { |
95 | |
|
96 | |
@Override |
97 | |
public void onValueChange(ValueChangeEvent<String> event) { |
98 | 0 | setLabel(countLabel); |
99 | 0 | } |
100 | |
}); |
101 | |
|
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private void setupDefaultStyle() { |
108 | 0 | addStyleName("KS-Textarea"); |
109 | |
|
110 | 0 | this.addBlurHandler(new BlurHandler() { |
111 | |
public void onBlur(BlurEvent event) { |
112 | 0 | KSTextArea.this.removeStyleName("KS-Textarea-Focus"); |
113 | |
|
114 | 0 | } |
115 | |
}); |
116 | |
|
117 | 0 | this.addFocusHandler(new FocusHandler() { |
118 | |
public void onFocus(FocusEvent event) { |
119 | 0 | KSTextArea.this.addStyleName("KS-Textarea-Focus"); |
120 | |
|
121 | 0 | } |
122 | |
}); |
123 | |
|
124 | 0 | this.addMouseOverHandler(new MouseOverHandler() { |
125 | |
public void onMouseOver(MouseOverEvent event) { |
126 | 0 | KSTextArea.this.addStyleName("KS-Textarea-Hover"); |
127 | |
|
128 | 0 | } |
129 | |
}); |
130 | |
|
131 | 0 | this.addMouseOutHandler(new MouseOutHandler() { |
132 | |
|
133 | |
public void onMouseOut(MouseOutEvent event) { |
134 | 0 | KSTextArea.this.removeStyleName("KS-Textarea-Hover"); |
135 | |
|
136 | 0 | } |
137 | |
|
138 | |
}); |
139 | |
|
140 | 0 | } |
141 | |
|
142 | |
@Override |
143 | |
public void setWatermarkText(String text) { |
144 | 0 | if (!hasWatermark) { |
145 | 0 | hasWatermark = true; |
146 | 0 | watermarkText = text; |
147 | 0 | if (getText() == null || getText().isEmpty()) { |
148 | 0 | addStyleName("watermark-text"); |
149 | 0 | KSTextArea.super.setText(watermarkText); |
150 | 0 | watermarkShowing = true; |
151 | |
} |
152 | |
|
153 | 0 | this.addFocusHandler(new FocusHandler() { |
154 | |
|
155 | |
@Override |
156 | |
public void onFocus(FocusEvent event) { |
157 | 0 | if (watermarkShowing) { |
158 | 0 | removeStyleName("watermark-text"); |
159 | 0 | KSTextArea.super.setText(""); |
160 | 0 | watermarkShowing = false; |
161 | |
} |
162 | 0 | } |
163 | |
}); |
164 | |
|
165 | 0 | this.addBlurHandler(new BlurHandler() { |
166 | |
|
167 | |
@Override |
168 | |
public void onBlur(BlurEvent event) { |
169 | 0 | if (getText() == null || getText().isEmpty()) { |
170 | 0 | addStyleName("watermark-text"); |
171 | 0 | KSTextArea.super.setText(watermarkText); |
172 | 0 | watermarkShowing = true; |
173 | |
} |
174 | 0 | } |
175 | |
}); |
176 | |
} else { |
177 | 0 | watermarkText = text; |
178 | 0 | if (getText() == null || getText().isEmpty()) { |
179 | 0 | addStyleName("watermark-text"); |
180 | 0 | KSTextArea.super.setText(watermarkText); |
181 | 0 | watermarkShowing = true; |
182 | |
} |
183 | |
} |
184 | 0 | } |
185 | |
|
186 | |
@Override |
187 | |
public boolean hasWatermark() { |
188 | 0 | return hasWatermark; |
189 | |
} |
190 | |
|
191 | |
@Override |
192 | |
public boolean watermarkShowing() { |
193 | 0 | return watermarkShowing; |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public String getText() { |
198 | 0 | if (!watermarkShowing) { |
199 | 0 | return super.getText(); |
200 | |
} |
201 | 0 | return null; |
202 | |
} |
203 | |
|
204 | |
@Override |
205 | |
public String getValue() { |
206 | 0 | if (!watermarkShowing) { |
207 | 0 | return super.getValue(); |
208 | |
} |
209 | 0 | return null; |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
public void setValue(String value) { |
214 | 0 | if (hasWatermark) { |
215 | 0 | if (value == null || (value != null && value.isEmpty())) { |
216 | 0 | super.setValue(watermarkText); |
217 | 0 | addStyleName("watermark-text"); |
218 | 0 | watermarkShowing = true; |
219 | |
} else { |
220 | 0 | super.setValue(value); |
221 | 0 | removeStyleName("watermark-text"); |
222 | 0 | watermarkShowing = false; |
223 | |
} |
224 | |
} else { |
225 | 0 | super.setValue(value); |
226 | |
} |
227 | 0 | } |
228 | |
|
229 | |
@Override |
230 | |
public void setText(String text) { |
231 | 0 | String oldValue = super.getText(); |
232 | 0 | if (hasWatermark) { |
233 | 0 | if (text == null || (text != null && text.isEmpty())) { |
234 | 0 | super.setText(watermarkText); |
235 | 0 | addStyleName("watermark-text"); |
236 | 0 | watermarkShowing = true; |
237 | |
} else { |
238 | 0 | super.setText(text); |
239 | 0 | removeStyleName("watermark-text"); |
240 | 0 | watermarkShowing = false; |
241 | |
} |
242 | |
} else { |
243 | 0 | super.setText(text); |
244 | |
} |
245 | 0 | ValueChangeEvent.fireIfNotEqual(this, oldValue, text); |
246 | 0 | } |
247 | |
|
248 | |
public void setLabel(final Label countLabel) { |
249 | 0 | left = this.maxLength - KSTextArea.this.getText().length(); |
250 | 0 | if (KSTextArea.this.getText().length() > this.maxLength) { |
251 | 0 | countLabel.setText("Please remove " + left * -1 + " characters"); |
252 | |
} else { |
253 | 0 | countLabel.setText(left + " characters left"); |
254 | |
} |
255 | 0 | if ((left <= (this.maxLength * 0.1)) || (left <= 10)) { |
256 | 0 | countLabel.getElement().setAttribute("style", "color: red;"); |
257 | |
} else { |
258 | 0 | countLabel.getElement().removeAttribute("style"); |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
public void setMaxLength(int maxLength) { |
263 | 0 | this.maxLength = maxLength; |
264 | 0 | } |
265 | |
|
266 | |
public int getMaxLength() { |
267 | 0 | return this.maxLength; |
268 | |
} |
269 | |
|
270 | |
} |