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