View Javadoc
1   /**
2    * This file Copyright (c) 2020 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 com.google.gwt.dom.client.Element;
37  import com.google.gwt.dom.client.Style;
38  import com.google.gwt.event.dom.client.KeyUpEvent;
39  import com.google.gwt.event.shared.HandlerRegistration;
40  import com.google.gwt.user.client.DOM;
41  import com.google.gwt.user.client.ui.HasValue;
42  import com.google.gwt.user.client.ui.Widget;
43  import com.vaadin.client.ServerConnector;
44  import com.vaadin.client.communication.StateChangeEvent;
45  import com.vaadin.client.extensions.AbstractExtensionConnector;
46  import com.vaadin.client.ui.AbstractComponentConnector;
47  
48  /**
49   * Abstract client-side connector for adding counter of characters to text component.
50   *
51   * @param <T> component connector
52   */
53  public abstract class AbstractMaxLengthIndicatorExtensionConnector<T extends AbstractComponentConnector> extends AbstractExtensionConnector {
54  
55      public static final String MAXLENGTH_INDICATOR_STYLE_NAME = "maxlength-indicator";
56  
57      protected T textConnector;
58      private Widget textWidget;
59      private final Element indicatorElem = DOM.createDiv();
60      private int maxLength = 0;
61      private HandlerRegistration keyUpHandlerRegistration;
62      private com.google.web.bindery.event.shared.HandlerRegistration parentStateChangeHandlerRegistration;
63  
64      abstract protected int getConnectorStateMaxLength();
65      abstract protected String getConnectorStateText();
66  
67      @Override
68      protected void extend(ServerConnector target) {
69          textConnector = (T) target;
70          textWidget = textConnector.getWidget();
71  
72          addHandlers();
73          updateIndicatorFromParentState();
74  
75          textWidget.addAttachHandler(event -> {
76              if (event.isAttached()) {
77                  assembleAndAttach();
78              }
79          });
80      }
81  
82      @Override
83      public void onUnregister() {
84          super.onUnregister();
85          cleanUp();
86      }
87  
88      private void cleanUp() {
89          if (keyUpHandlerRegistration != null) {
90              keyUpHandlerRegistration.removeHandler();
91          }
92          if (parentStateChangeHandlerRegistration != null) {
93              parentStateChangeHandlerRegistration.removeHandler();
94          }
95          if (indicatorElem.hasParentElement()) {
96              indicatorElem.removeFromParent();
97          }
98      }
99  
100     private void assembleAndAttach() {
101         // We append indicator element to the text widget's parent element
102         indicatorElem.addClassName(MAXLENGTH_INDICATOR_STYLE_NAME);
103         textWidget.getElement().getParentElement().appendChild(indicatorElem);
104     }
105 
106     private void addHandlers() {
107         // One might think KeyPressEvent should be the "better" choice here.
108         // However, when using KeyPressEvent, text field's value is not updated
109         // and hence the displayed number of entered characters is always 1 too small.
110         keyUpHandlerRegistration = textWidget.addDomHandler(event -> updateIndicatorValue(getInputValueLength()), KeyUpEvent.getType());
111 
112         // make sure to update maxLength when the property gets changed
113         parentStateChangeHandlerRegistration = textConnector.addStateChangeHandler((StateChangeEvent.StateChangeHandler) stateChangeEvent -> {
114             if (stateChangeEvent.hasPropertyChanged("maxLength") || stateChangeEvent.hasPropertyChanged("text")) {
115                 updateIndicatorFromParentState();
116             }
117         });
118     }
119 
120     private void updateIndicatorFromParentState() {
121         maxLength = getConnectorStateMaxLength();
122         String text = getConnectorStateText();
123         updateIndicatorValue(text == null ? 0 : text.length());
124         setIndicatorVisible(maxLength >= 0);
125     }
126 
127     private void setIndicatorVisible(boolean isVisible) {
128         if (isVisible) {
129             indicatorElem.getStyle().clearDisplay();
130         } else {
131             indicatorElem.getStyle().setDisplay(Style.Display.NONE);
132         }
133     }
134 
135     private void updateIndicatorValue(int currentlyDisplayedCharactersAmount) {
136         indicatorElem.setInnerHTML(currentlyDisplayedCharactersAmount + "/" + maxLength);
137     }
138 
139     private int getInputValueLength() {
140         return ((String)((HasValue)textWidget).getValue()).length();
141     }
142 
143 
144 }