1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets; |
17 | |
|
18 | |
import com.google.gwt.dom.client.Element; |
19 | |
import com.google.gwt.event.dom.client.BlurEvent; |
20 | |
import com.google.gwt.event.dom.client.BlurHandler; |
21 | |
import com.google.gwt.event.dom.client.FocusEvent; |
22 | |
import com.google.gwt.event.dom.client.FocusHandler; |
23 | |
import com.google.gwt.event.dom.client.MouseOutEvent; |
24 | |
import com.google.gwt.event.dom.client.MouseOutHandler; |
25 | |
import com.google.gwt.event.dom.client.MouseOverEvent; |
26 | |
import com.google.gwt.event.dom.client.MouseOverHandler; |
27 | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
28 | |
import com.google.gwt.user.client.ui.TextBox; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class KSTextBox extends TextBox implements HasWatermark{ |
39 | 0 | private boolean hasWatermark = false; |
40 | 0 | private boolean watermarkShowing = false; |
41 | |
private String watermarkText; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public KSTextBox() { |
48 | 0 | super(); |
49 | 0 | setupDefaultStyle(); |
50 | 0 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public KSTextBox(Element element) { |
58 | 0 | super(element); |
59 | 0 | setupDefaultStyle(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private void setupDefaultStyle() { |
67 | 0 | addStyleName("KS-Textbox"); |
68 | |
|
69 | 0 | this.addBlurHandler(new BlurHandler(){ |
70 | |
public void onBlur(BlurEvent event) { |
71 | 0 | KSTextBox.this.removeStyleName("KS-Textbox-Focus"); |
72 | |
|
73 | 0 | } |
74 | |
}); |
75 | |
|
76 | 0 | this.addFocusHandler(new FocusHandler(){ |
77 | |
public void onFocus(FocusEvent event) { |
78 | 0 | KSTextBox.this.addStyleName("KS-Textbox-Focus"); |
79 | |
|
80 | 0 | } |
81 | |
}); |
82 | |
|
83 | 0 | this.addMouseOverHandler(new MouseOverHandler(){ |
84 | |
public void onMouseOver(MouseOverEvent event) { |
85 | 0 | KSTextBox.this.addStyleName("KS-Textbox-Hover"); |
86 | |
|
87 | 0 | } |
88 | |
}); |
89 | |
|
90 | 0 | this.addMouseOutHandler(new MouseOutHandler(){ |
91 | |
|
92 | |
public void onMouseOut(MouseOutEvent event) { |
93 | 0 | KSTextBox.this.removeStyleName("KS-Textbox-Hover"); |
94 | |
|
95 | 0 | } |
96 | |
|
97 | |
}); |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
@Override |
102 | |
public void setWatermarkText(String text) { |
103 | 0 | if(!hasWatermark){ |
104 | 0 | hasWatermark = true; |
105 | 0 | watermarkText = text; |
106 | 0 | if(getText() == null || getText().isEmpty()){ |
107 | 0 | addStyleName("watermark-text"); |
108 | 0 | super.setText(watermarkText); |
109 | 0 | watermarkShowing = true; |
110 | |
} |
111 | |
|
112 | 0 | this.addFocusHandler(new FocusHandler(){ |
113 | |
|
114 | |
@Override |
115 | |
public void onFocus(FocusEvent event) { |
116 | 0 | if(watermarkShowing){ |
117 | 0 | removeStyleName("watermark-text"); |
118 | 0 | KSTextBox.super.setText(""); |
119 | 0 | watermarkShowing = false; |
120 | |
} |
121 | 0 | } |
122 | |
}); |
123 | |
|
124 | 0 | this.addBlurHandler(new BlurHandler(){ |
125 | |
|
126 | |
@Override |
127 | |
public void onBlur(BlurEvent event) { |
128 | 0 | if(getText() == null || getText().isEmpty()){ |
129 | 0 | addStyleName("watermark-text"); |
130 | 0 | KSTextBox.super.setText(watermarkText); |
131 | 0 | watermarkShowing = true; |
132 | |
} |
133 | 0 | } |
134 | |
}); |
135 | |
} |
136 | |
else{ |
137 | 0 | watermarkText = text; |
138 | 0 | if(getText() == null || getText().isEmpty()){ |
139 | 0 | addStyleName("watermark-text"); |
140 | 0 | KSTextBox.super.setText(watermarkText); |
141 | 0 | watermarkShowing = true; |
142 | |
} |
143 | |
} |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
public boolean hasWatermark(){ |
148 | 0 | return hasWatermark; |
149 | |
} |
150 | |
|
151 | |
@Override |
152 | |
public boolean watermarkShowing() { |
153 | 0 | return watermarkShowing; |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public String getText() { |
158 | 0 | if(!watermarkShowing){ |
159 | 0 | return super.getText(); |
160 | |
} |
161 | 0 | return null; |
162 | |
} |
163 | |
|
164 | |
@Override |
165 | |
public String getValue() { |
166 | 0 | if(!watermarkShowing){ |
167 | 0 | return super.getValue(); |
168 | |
} |
169 | 0 | return null; |
170 | |
} |
171 | |
|
172 | |
@Override |
173 | |
public void setValue(String value) { |
174 | 0 | if(hasWatermark){ |
175 | 0 | if(value == null || (value != null && value.isEmpty())){ |
176 | 0 | super.setValue(watermarkText); |
177 | 0 | addStyleName("watermark-text"); |
178 | 0 | watermarkShowing = true; |
179 | |
} |
180 | |
else{ |
181 | 0 | super.setValue(value); |
182 | 0 | removeStyleName("watermark-text"); |
183 | 0 | watermarkShowing = false; |
184 | |
} |
185 | |
} |
186 | |
else{ |
187 | 0 | super.setValue(value); |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
@Override |
192 | |
public void setText(String text) { |
193 | 0 | String oldValue = super.getText(); |
194 | 0 | if(hasWatermark){ |
195 | 0 | if(text == null || (text != null && text.isEmpty())){ |
196 | 0 | super.setText(watermarkText); |
197 | 0 | addStyleName("watermark-text"); |
198 | 0 | watermarkShowing = true; |
199 | |
} |
200 | |
else{ |
201 | 0 | super.setText(text); |
202 | 0 | removeStyleName("watermark-text"); |
203 | 0 | watermarkShowing = false; |
204 | |
} |
205 | |
} |
206 | |
else{ |
207 | 0 | super.setText(text); |
208 | |
} |
209 | 0 | ValueChangeEvent.fireIfNotEqual(this, oldValue, text); |
210 | |
|
211 | 0 | } |
212 | |
} |