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