Coverage Report - org.kuali.student.common.ui.client.widgets.KSTextBox
 
Classes in this File Line Coverage Branch Coverage Complexity
KSTextBox
0%
0/60
0%
0/30
2.25
KSTextBox$1
0%
0/3
N/A
2.25
KSTextBox$2
0%
0/3
N/A
2.25
KSTextBox$3
0%
0/3
N/A
2.25
KSTextBox$4
0%
0/3
N/A
2.25
KSTextBox$5
0%
0/6
0%
0/2
2.25
KSTextBox$6
0%
0/6
0%
0/4
2.25
 
 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  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  
      * Creates an empty text box.
 45  
      *
 46  
      */
 47  
     public KSTextBox() {
 48  0
         super();
 49  0
         setupDefaultStyle();
 50  0
     }
 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  0
         super(element);
 59  0
         setupDefaultStyle();
 60  0
     }
 61  
 
 62  
     /**
 63  
      * This method sets the default style for the text box and text box events.
 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  
 }