Coverage Report - org.kuali.student.common.ui.client.widgets.KSTextArea
 
Classes in this File Line Coverage Branch Coverage Complexity
KSTextArea
0%
0/82
0%
0/34
2.304
KSTextArea$1
0%
0/3
N/A
2.304
KSTextArea$2
0%
0/3
N/A
2.304
KSTextArea$3
0%
0/3
N/A
2.304
KSTextArea$4
0%
0/3
N/A
2.304
KSTextArea$5
0%
0/3
N/A
2.304
KSTextArea$6
0%
0/3
N/A
2.304
KSTextArea$7
0%
0/10
0%
0/16
2.304
KSTextArea$8
0%
0/6
0%
0/2
2.304
KSTextArea$9
0%
0/6
0%
0/4
2.304
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
 3  
  * not use this file except in compliance with the License. You may obtain a copy of the License at
 4  
  * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
 5  
  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 6  
  * implied. See the License for the specific language governing permissions and limitations under the License.
 7  
  */
 8  
 
 9  
 package org.kuali.student.common.ui.client.widgets;
 10  
 
 11  
 import com.google.gwt.dom.client.Element;
 12  
 import com.google.gwt.event.dom.client.BlurEvent;
 13  
 import com.google.gwt.event.dom.client.BlurHandler;
 14  
 import com.google.gwt.event.dom.client.FocusEvent;
 15  
 import com.google.gwt.event.dom.client.FocusHandler;
 16  
 import com.google.gwt.event.dom.client.KeyDownEvent;
 17  
 import com.google.gwt.event.dom.client.KeyDownHandler;
 18  
 import com.google.gwt.event.dom.client.KeyUpEvent;
 19  
 import com.google.gwt.event.dom.client.KeyUpHandler;
 20  
 import com.google.gwt.event.dom.client.MouseOutEvent;
 21  
 import com.google.gwt.event.dom.client.MouseOutHandler;
 22  
 import com.google.gwt.event.dom.client.MouseOverEvent;
 23  
 import com.google.gwt.event.dom.client.MouseOverHandler;
 24  
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 25  
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 26  
 import com.google.gwt.user.client.ui.Label;
 27  
 import com.google.gwt.user.client.ui.TextArea;
 28  
 
 29  
 /**
 30  
  * KSTextArea wraps gwt TextArea. This class provides most of the same functionality, but sets KS css styles for its default
 31  
  * look and a variety of TextArea events (for improved browser compatibility and customizability).
 32  
  * 
 33  
  * @author Kuali Student Team
 34  
  */
 35  0
 public class KSTextArea extends TextArea implements HasWatermark {
 36  0
     private boolean hasWatermark = false;
 37  0
     private boolean watermarkShowing = false;
 38  
     private String watermarkText;
 39  0
     private int left = 0;
 40  0
     private int maxLength = 0;
 41  
 
 42  
     /**
 43  
      * Creates a new empty text area.
 44  
      */
 45  
     public KSTextArea() {
 46  0
         super();
 47  0
         setupDefaultStyle();
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Creates a new text area using the text area element specified.
 52  
      * 
 53  
      * @param element
 54  
      *            a <TextArea> element
 55  
      */
 56  
     public KSTextArea(Element element) {
 57  0
         super(element);
 58  0
         setupDefaultStyle();
 59  0
     }
 60  
 
 61  
     public KSTextArea(final Label countLabel, final int maximumChar) {
 62  0
         super();
 63  0
         setupDefaultStyle();
 64  0
         left = maximumChar;
 65  0
         countLabel.setText(left + " characters left");
 66  0
         this.addKeyUpHandler(new KeyUpHandler() {
 67  
 
 68  
             @Override
 69  
             public void onKeyUp(KeyUpEvent event) {
 70  0
                 setLabel(countLabel, maximumChar);
 71  0
             }
 72  
         });
 73  
 
 74  0
         this.addValueChangeHandler(new ValueChangeHandler<String>() {
 75  
 
 76  
             @Override
 77  
             public void onValueChange(ValueChangeEvent<String> event) {
 78  0
                 setLabel(countLabel, maximumChar);
 79  0
             }
 80  
         });
 81  
 
 82  0
     }
 83  
 
 84  
     /**
 85  
      * This method sets the default style for the text area and text area events.
 86  
      */
 87  
     private void setupDefaultStyle() {
 88  0
         addStyleName("KS-Textarea");
 89  
 
 90  0
         this.addBlurHandler(new BlurHandler() {
 91  
             public void onBlur(BlurEvent event) {
 92  0
                 KSTextArea.this.removeStyleName("KS-Textarea-Focus");
 93  
 
 94  0
             }
 95  
         });
 96  
 
 97  0
         this.addFocusHandler(new FocusHandler() {
 98  
             public void onFocus(FocusEvent event) {
 99  0
                 KSTextArea.this.addStyleName("KS-Textarea-Focus");
 100  
 
 101  0
             }
 102  
         });
 103  
 
 104  0
         this.addMouseOverHandler(new MouseOverHandler() {
 105  
             public void onMouseOver(MouseOverEvent event) {
 106  0
                 KSTextArea.this.addStyleName("KS-Textarea-Hover");
 107  
 
 108  0
             }
 109  
         });
 110  
 
 111  0
         this.addMouseOutHandler(new MouseOutHandler() {
 112  
 
 113  
             public void onMouseOut(MouseOutEvent event) {
 114  0
                 KSTextArea.this.removeStyleName("KS-Textarea-Hover");
 115  
 
 116  0
             }
 117  
 
 118  
         });
 119  
 
 120  0
         this.addKeyDownHandler(new KeyDownHandler() {
 121  
 
 122  
             @Override
 123  
             public void onKeyDown(KeyDownEvent event) {
 124  0
                 int keyPressed = (int) event.getNativeKeyCode();
 125  0
                 int textLength = getText().length();
 126  
 
 127  0
                 if ((textLength >= maxLength) && (maxLength > 0)) {
 128  0
                     setText(getText().trim().substring(0, maxLength));
 129  0
                     if (textLength + 1 > maxLength && (48 <= keyPressed && keyPressed <= 57) || (65 <= keyPressed && keyPressed <= 90)) {
 130  0
                         event.getNativeEvent().preventDefault();
 131  
                     }
 132  0
                     if (getCursorPos() == maxLength) {
 133  0
                         event.getRelativeElement().setScrollTop(getCursorPos());
 134  
                     }
 135  
                 }
 136  0
             }
 137  
 
 138  
         });
 139  0
     }
 140  
 
 141  
     @Override
 142  
     public void setWatermarkText(String text) {
 143  0
         if (!hasWatermark) {
 144  0
             hasWatermark = true;
 145  0
             watermarkText = text;
 146  0
             if (getText() == null || getText().isEmpty()) {
 147  0
                 addStyleName("watermark-text");
 148  0
                 KSTextArea.super.setText(watermarkText);
 149  0
                 watermarkShowing = true;
 150  
             }
 151  
 
 152  0
             this.addFocusHandler(new FocusHandler() {
 153  
 
 154  
                 @Override
 155  
                 public void onFocus(FocusEvent event) {
 156  0
                     if (watermarkShowing) {
 157  0
                         removeStyleName("watermark-text");
 158  0
                         KSTextArea.super.setText("");
 159  0
                         watermarkShowing = false;
 160  
                     }
 161  0
                 }
 162  
             });
 163  
 
 164  0
             this.addBlurHandler(new BlurHandler() {
 165  
 
 166  
                 @Override
 167  
                 public void onBlur(BlurEvent event) {
 168  0
                     if (getText() == null || getText().isEmpty()) {
 169  0
                         addStyleName("watermark-text");
 170  0
                         KSTextArea.super.setText(watermarkText);
 171  0
                         watermarkShowing = true;
 172  
                     }
 173  0
                 }
 174  
             });
 175  
         } else {
 176  0
             watermarkText = text;
 177  0
             if (getText() == null || getText().isEmpty()) {
 178  0
                 addStyleName("watermark-text");
 179  0
                 KSTextArea.super.setText(watermarkText);
 180  0
                 watermarkShowing = true;
 181  
             }
 182  
         }
 183  0
     }
 184  
 
 185  
     @Override
 186  
     public boolean hasWatermark() {
 187  0
         return hasWatermark;
 188  
     }
 189  
 
 190  
     @Override
 191  
     public boolean watermarkShowing() {
 192  0
         return watermarkShowing;
 193  
     }
 194  
 
 195  
     @Override
 196  
     public String getText() {
 197  0
         if (!watermarkShowing) {
 198  0
             return super.getText();
 199  
         }
 200  0
         return null;
 201  
     }
 202  
 
 203  
     @Override
 204  
     public String getValue() {
 205  0
         if (!watermarkShowing) {
 206  0
             return super.getValue();
 207  
         }
 208  0
         return null;
 209  
     }
 210  
 
 211  
     @Override
 212  
     public void setValue(String value) {
 213  0
         if (hasWatermark) {
 214  0
             if (value == null || (value != null && value.isEmpty())) {
 215  0
                 super.setValue(watermarkText);
 216  0
                 addStyleName("watermark-text");
 217  0
                 watermarkShowing = true;
 218  
             } else {
 219  0
                 super.setValue(value);
 220  0
                 removeStyleName("watermark-text");
 221  0
                 watermarkShowing = false;
 222  
             }
 223  
         } else {
 224  0
             super.setValue(value);
 225  
         }
 226  0
     }
 227  
 
 228  
     @Override
 229  
     public void setText(String text) {
 230  0
         String oldValue = super.getText();
 231  0
         if (hasWatermark) {
 232  0
             if (text == null || (text != null && text.isEmpty())) {
 233  0
                 super.setText(watermarkText);
 234  0
                 addStyleName("watermark-text");
 235  0
                 watermarkShowing = true;
 236  
             } else {
 237  0
                 super.setText(text);
 238  0
                 removeStyleName("watermark-text");
 239  0
                 watermarkShowing = false;
 240  
             }
 241  
         } else {
 242  0
             super.setText(text);
 243  
         }
 244  0
         ValueChangeEvent.fireIfNotEqual(this, oldValue, text);
 245  0
     }
 246  
 
 247  
     public void setLabel(final Label countLabel, int maximumChar) {
 248  0
         if (KSTextArea.this.getText().length() > maximumChar) {
 249  0
             KSTextArea.this.setText(KSTextArea.this.getText().substring(0, maximumChar));
 250  0
             left = 0;
 251  
         } else {
 252  0
             left = maximumChar - KSTextArea.this.getText().length();
 253  
         }
 254  0
         if (left <= 10) {
 255  0
             countLabel.getElement().setAttribute("style", "color: red;");
 256  
         } else {
 257  0
             countLabel.getElement().removeAttribute("style");
 258  
         }
 259  0
         countLabel.setText(left + " characters left");
 260  0
     }
 261  
 
 262  
     public void setMaxLength(int maxLength) {
 263  0
         this.maxLength = maxLength;
 264  0
     }
 265  
 
 266  
     public int getMaxLength() {
 267  0
         return this.maxLength;
 268  
     }
 269  
     
 270  
 }