1
2
3
4
5
6
7
8
9
10
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 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) && (this.getText().length() <= maxLength)) {
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
105 if ((rem <= (this.maxLength * 0.1)) || (rem <= 10)) {
106 countingLabel.getElement().setAttribute("style", "color: red;");
107 } else {
108 countingLabel.getElement().removeAttribute("style");
109 }
110
111 return (rem + " " + Application.getApplicationContext().getUILabel("common", "remainingChars"));
112 }
113
114 @Override
115 public String getText() {
116 return ((TextBoxBase) (this.inputWidget)).getText();
117 }
118
119 @Override
120 public void setText(String text) {
121 ((TextBoxBase) (this.inputWidget)).setText(text);
122
123 }
124
125 @Override
126 public Widget getInputWidget() {
127 return this.inputWidget;
128 }
129
130 @Override
131 public HandlerRegistration addBlurHandler(BlurHandler handler) {
132 return ((TextBoxBase) (this.inputWidget)).addBlurHandler(handler);
133 }
134
135 }