View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * KSTextArea wraps gwt TextArea.  This class provides most of the same functionality, but sets KS css styles
31   * for its default look and a variety of TextArea events (for improved browser compatibility and customizability).
32   *
33   * @author Kuali Student Team
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       * Creates an empty text box.
44       *
45       */
46      public KSTextBox() {
47          super();
48          setupDefaultStyle();
49      }
50  
51      /**
52       *  Creates a new text box using the text box element specified.
53       *
54       * @param element a <input> element of type 'text'
55       */
56      public KSTextBox(Element element) {
57          super(element);
58          setupDefaultStyle();
59      }
60  
61      /**
62       * This method sets the default style for the text box and text box events.
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 }