View Javadoc
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.ui.client.application.Application;
16  import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
17  import org.kuali.student.r1.common.assembly.data.Metadata;
18  import org.kuali.student.r1.common.assembly.data.MetadataInterrogator;
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      protected VerticalPanel countingPanel;
35      protected Widget inputWidget;
36      protected KSLabel countingLabel;
37  
38      private int maxLength;
39  
40      public KSCharCount() {}
41  
42      public KSCharCount(Metadata metadata) {
43          countingPanel = new VerticalPanel();
44          countingLabel = new KSLabel();
45          super.initWidget(countingPanel);
46          this.setWidget(DefaultWidgetFactory.getInstance().getWidget(metadata));
47          this.setMaxLength(metadata);
48  
49          if (this.inputWidget instanceof TextBoxBase) {
50              ((TextBoxBase) (this.inputWidget)).addKeyUpHandler(new KeyUpHandler() {
51  
52                  @Override
53                  public void onKeyUp(KeyUpEvent event) {
54                      countingLabel.setText(setLabel());
55  
56                  }
57              });
58  
59              ((TextBoxBase) (this.inputWidget)).addValueChangeHandler(new ValueChangeHandler<String>() {
60  
61                  @Override
62                  public void onValueChange(ValueChangeEvent<String> event) {
63                      countingLabel.setText(setLabel());
64  
65                  }
66  
67              });
68          }
69  
70          countingLabel.setStyleName("ks-form-module-elements-help-text");
71          countingLabel.setText(this.setLabel());
72          countingPanel.add(inputWidget);
73          countingPanel.add(countingLabel);
74      }
75  
76      public void setMaxLength(Metadata metadata) {
77          this.maxLength = MetadataInterrogator.getSmallestMaxLength(metadata);
78      }
79  
80      public void setWidget(Widget widget) {
81          if (widget instanceof TextBoxBase) {
82              this.inputWidget = widget;
83          } else {
84              this.inputWidget = new KSTextBox();
85          }
86  
87      }
88  
89      public int getRemCount() {
90          int rem = 0;
91  
92          if ((this.getText() != null)) {
93              rem = this.maxLength - ((TextBoxBase) (this.inputWidget)).getText().length();
94          }
95          if (this.getText() == null) {
96              rem = this.maxLength;
97          }
98  
99          return rem;
100     }
101 
102     public String setLabel() {
103         int rem = getRemCount();
104         String message = "";
105 
106         if (this.getText().length() > this.maxLength) {
107             message = "Please remove " + rem * -1 + " characters";
108         } else {
109             message = rem + " " + Application.getApplicationContext().getUILabel("common", "remainingChars");
110         }
111         
112         if ((rem <= (this.maxLength * 0.1)) || (rem <= 10)) {
113             countingLabel.getElement().setAttribute("style", "color: red;");
114         } else {
115             countingLabel.getElement().removeAttribute("style");
116         }
117 
118         return message;
119     }
120 
121     @Override
122     public String getText() {
123         return ((TextBoxBase) (this.inputWidget)).getText();
124     }
125 
126     @Override
127     public void setText(String text) {
128         ((TextBoxBase) (this.inputWidget)).setText(text);
129 
130     }
131 
132     @Override
133     public Widget getInputWidget() {
134         return this.inputWidget;
135     }
136 
137     @Override
138     public HandlerRegistration addBlurHandler(BlurHandler handler) {
139         return ((TextBoxBase) (this.inputWidget)).addBlurHandler(handler);
140     }
141 
142 }