View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Client-side connector for {@link MaxLengthIndicator}.
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         // We append indicator element to the text widget's parent element
107         indicatorElem.addClassName(MAXLENGTH_INDICATOR_STYLE_NAME);
108         textWidget.getElement().getParentElement().appendChild(indicatorElem);
109     }
110 
111     private void addHandlers() {
112         // One might think KeyPressEvent should be the "better" choice here.
113         // However, when using KeyPressEvent, text field's value is not updated
114         // and hence the displayed number of entered characters is always 1 too small.
115         keyUpHandlerRegistration = textWidget.addDomHandler(new KeyUpHandler() {
116             @Override
117             public void onKeyUp(KeyUpEvent event) {
118                 updateIndicatorValue(getInputValueLength());
119             }
120         }, KeyUpEvent.getType());
121 
122         // make sure to update maxLength when the property gets changed
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 }