1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.gwt.client.extension;
35
36 import info.magnolia.ui.vaadin.extension.MaxLengthIndicator;
37
38 import com.google.gwt.dom.client.Element;
39 import com.google.gwt.dom.client.Style;
40 import com.google.gwt.event.dom.client.KeyUpEvent;
41 import com.google.gwt.event.dom.client.KeyUpHandler;
42 import com.google.gwt.event.logical.shared.AttachEvent;
43 import com.google.gwt.event.shared.HandlerRegistration;
44 import com.google.gwt.user.client.DOM;
45 import com.google.gwt.user.client.ui.HasValue;
46 import com.google.gwt.user.client.ui.Widget;
47 import com.vaadin.client.ServerConnector;
48 import com.vaadin.client.communication.StateChangeEvent;
49 import com.vaadin.client.extensions.AbstractExtensionConnector;
50 import com.vaadin.shared.ui.Connect;
51 import com.vaadin.v7.client.ui.textfield.TextFieldConnector;
52
53
54
55
56 @Connect(MaxLengthIndicator.class)
57 public class MaxLengthIndicatorConnector extends AbstractExtensionConnector {
58
59 public static final String MAXLENGTH_INDICATOR_STYLE_NAME = "maxlength-indicator";
60
61 private TextFieldConnector textConnector;
62 private Widget textWidget;
63 private Element indicatorElem = DOM.createDiv();
64 private int maxLength = 0;
65 private HandlerRegistration keyUpHandlerRegistration;
66 private com.google.web.bindery.event.shared.HandlerRegistration parentStateChangeHandlerRegistration;
67
68
69 @Override
70 protected void extend(ServerConnector target) {
71 textConnector = (TextFieldConnector) target;
72 textWidget = textConnector.getWidget();
73
74 addHandlers();
75 updateIndicatorFromParentState();
76
77 textWidget.addAttachHandler(new AttachEvent.Handler() {
78 @Override
79 public void onAttachOrDetach(AttachEvent event) {
80 if (event.isAttached()) {
81 assembleAndAttach();
82 }
83 }
84 });
85 }
86
87 @Override
88 public void onUnregister() {
89 super.onUnregister();
90 cleanUp();
91 }
92
93 private void cleanUp() {
94 if (keyUpHandlerRegistration != null) {
95 keyUpHandlerRegistration.removeHandler();
96 }
97 if (parentStateChangeHandlerRegistration != null) {
98 parentStateChangeHandlerRegistration.removeHandler();
99 }
100 if (indicatorElem.hasParentElement()) {
101 indicatorElem.removeFromParent();
102 }
103 }
104
105 private void assembleAndAttach() {
106
107 indicatorElem.addClassName(MAXLENGTH_INDICATOR_STYLE_NAME);
108 textWidget.getElement().getParentElement().appendChild(indicatorElem);
109 }
110
111 private void addHandlers() {
112
113
114
115 keyUpHandlerRegistration = textWidget.addDomHandler(new KeyUpHandler() {
116 @Override
117 public void onKeyUp(KeyUpEvent event) {
118 updateIndicatorValue(getInputValueLength());
119 }
120 }, KeyUpEvent.getType());
121
122
123 parentStateChangeHandlerRegistration = textConnector.addStateChangeHandler(new StateChangeEvent.StateChangeHandler() {
124 @Override
125 public void onStateChanged(StateChangeEvent stateChangeEvent) {
126 if (stateChangeEvent.hasPropertyChanged("maxLength") || stateChangeEvent.hasPropertyChanged("text")) {
127 updateIndicatorFromParentState();
128 }
129 }
130 });
131 }
132
133 private void updateIndicatorFromParentState() {
134 maxLength = textConnector.getState().maxLength;
135 String text = textConnector.getState().text;
136 updateIndicatorValue(text == null ? 0 : text.length());
137 setIndicatorVisible(maxLength >= 0);
138 }
139
140 private void setIndicatorVisible(boolean isVisible) {
141 if (isVisible) {
142 indicatorElem.getStyle().clearDisplay();
143 } else {
144 indicatorElem.getStyle().setDisplay(Style.Display.NONE);
145 }
146 }
147
148 private void updateIndicatorValue(int currentlyDisplayedCharactersAmount) {
149 indicatorElem.setInnerHTML(currentlyDisplayedCharactersAmount + "/" + maxLength);
150 }
151
152 private int getInputValueLength() {
153 return ((String)((HasValue)textWidget).getValue()).length();
154 }
155
156
157 }