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