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