Coverage Report - org.kuali.student.common.ui.client.widgets.KSCharCount
 
Classes in this File Line Coverage Branch Coverage Complexity
KSCharCount
0%
0/37
0%
0/14
1.583
KSCharCount$1
0%
0/3
N/A
1.583
KSCharCount$2
0%
0/3
N/A
1.583
 
 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  
 /**
 10  
  * This widget adds a label with remaining character count for KSTextBox, KSTextArea
 11  
  */
 12  
 
 13  
 package org.kuali.student.common.ui.client.widgets;
 14  
 
 15  
 import org.kuali.student.common.assembly.data.Metadata;
 16  
 import org.kuali.student.common.assembly.data.MetadataInterrogator;
 17  
 import org.kuali.student.common.ui.client.application.Application;
 18  
 import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
 19  
 
 20  
 import com.google.gwt.event.dom.client.BlurHandler;
 21  
 import com.google.gwt.event.dom.client.HasBlurHandlers;
 22  
 import com.google.gwt.event.dom.client.KeyUpEvent;
 23  
 import com.google.gwt.event.dom.client.KeyUpHandler;
 24  
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 25  
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 26  
 import com.google.gwt.event.shared.HandlerRegistration;
 27  
 import com.google.gwt.user.client.ui.Composite;
 28  
 import com.google.gwt.user.client.ui.HasText;
 29  
 import com.google.gwt.user.client.ui.TextBoxBase;
 30  
 import com.google.gwt.user.client.ui.VerticalPanel;
 31  
 import com.google.gwt.user.client.ui.Widget;
 32  
 
 33  
 public class KSCharCount extends Composite implements HasText, HasInputWidget, HasBlurHandlers {
 34  
     VerticalPanel countingPanel;
 35  
     Widget inputWidget;
 36  
     KSLabel countingLabel;
 37  
 
 38  
     private int maxLength;
 39  
 
 40  0
     public KSCharCount() {}
 41  
 
 42  0
     public KSCharCount(Metadata metadata) {
 43  0
         countingPanel = new VerticalPanel();
 44  0
         countingLabel = new KSLabel();
 45  0
         super.initWidget(countingPanel);
 46  0
         this.setWidget(DefaultWidgetFactory.getInstance().getWidget(metadata));
 47  0
         this.setMaxLength(metadata);
 48  
 
 49  0
         if (this.inputWidget instanceof TextBoxBase) {
 50  0
             ((TextBoxBase) (this.inputWidget)).addKeyUpHandler(new KeyUpHandler() {
 51  
 
 52  
                 @Override
 53  
                 public void onKeyUp(KeyUpEvent event) {
 54  0
                     countingLabel.setText(setLabel());
 55  
 
 56  0
                 }
 57  
             });
 58  
 
 59  0
             ((TextBoxBase) (this.inputWidget)).addValueChangeHandler(new ValueChangeHandler<String>() {
 60  
 
 61  
                 @Override
 62  
                 public void onValueChange(ValueChangeEvent<String> event) {
 63  0
                     countingLabel.setText(setLabel());
 64  
 
 65  0
                 }
 66  
 
 67  
             });
 68  
         }
 69  
 
 70  0
         countingLabel.setStyleName("ks-form-module-elements-help-text");
 71  0
         countingLabel.setText(this.setLabel());
 72  0
         countingPanel.add(inputWidget);
 73  0
         countingPanel.add(countingLabel);
 74  0
     }
 75  
 
 76  
     public void setMaxLength(Metadata metadata) {
 77  0
         this.maxLength = MetadataInterrogator.getSmallestMaxLength(metadata);
 78  0
     }
 79  
 
 80  
     public void setWidget(Widget widget) {
 81  0
         if (widget instanceof TextBoxBase) {
 82  0
             this.inputWidget = widget;
 83  
         } else {
 84  0
             this.inputWidget = new KSTextBox();
 85  
         }
 86  
 
 87  0
     }
 88  
 
 89  
     public int getRemCount() {
 90  0
         int rem = 0;
 91  
 
 92  0
         if ((this.getText() != null) && (this.getText().length() <= maxLength)) {
 93  0
             rem = this.maxLength - ((TextBoxBase) (this.inputWidget)).getText().length();
 94  
         }
 95  0
         if (this.getText() == null) {
 96  0
             rem = this.maxLength;
 97  
         }
 98  
 
 99  0
         return rem;
 100  
     }
 101  
 
 102  
     public String setLabel() {
 103  0
         int rem = getRemCount();
 104  
 
 105  0
         if ((rem <= (this.maxLength * 0.1)) || (rem <= 10)) {
 106  0
             countingLabel.getElement().setAttribute("style", "color: red;");
 107  
         } else {
 108  0
             countingLabel.getElement().removeAttribute("style");
 109  
         }
 110  
 
 111  0
         return (rem + " " + Application.getApplicationContext().getUILabel("common", "remainingChars"));
 112  
     }
 113  
 
 114  
     @Override
 115  
     public String getText() {
 116  0
         return ((TextBoxBase) (this.inputWidget)).getText();
 117  
     }
 118  
 
 119  
     @Override
 120  
     public void setText(String text) {
 121  0
         ((TextBoxBase) (this.inputWidget)).setText(text);
 122  
 
 123  0
     }
 124  
 
 125  
     @Override
 126  
     public Widget getInputWidget() {
 127  0
         return this.inputWidget;
 128  
     }
 129  
 
 130  
     @Override
 131  
     public HandlerRegistration addBlurHandler(BlurHandler handler) {
 132  0
         return ((TextBoxBase) (this.inputWidget)).addBlurHandler(handler);
 133  
     }
 134  
 
 135  
 }