View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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.form.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.form.formsection.widget.InlineMessageWidget;
37  
38  import com.google.gwt.event.dom.client.BlurEvent;
39  import com.google.gwt.event.dom.client.BlurHandler;
40  import com.google.gwt.event.dom.client.ClickEvent;
41  import com.google.gwt.event.dom.client.ClickHandler;
42  import com.google.gwt.event.dom.client.FocusEvent;
43  import com.google.gwt.event.dom.client.FocusHandler;
44  import com.google.gwt.event.dom.client.HasBlurHandlers;
45  import com.google.gwt.event.dom.client.HasFocusHandlers;
46  import com.google.gwt.event.shared.HandlerRegistration;
47  import com.google.gwt.user.client.DOM;
48  import com.google.gwt.user.client.Element;
49  import com.google.gwt.user.client.ui.Button;
50  import com.google.gwt.user.client.ui.FlowPanel;
51  import com.google.gwt.user.client.ui.Widget;
52  import com.google.gwt.user.client.ui.impl.FocusImpl;
53  import com.vaadin.client.ui.aria.AriaHelper;
54  
55  /**
56   * Wrapper widget that provides help and error indication.
57   */
58  public class FormFieldWrapper extends FlowPanel implements HasFocusHandlers, HasBlurHandlers {
59  
60      private Element label = DOM.createDiv();
61  
62      private Element fieldWrapper = DOM.createDiv();
63  
64      private Element root;
65  
66      private final HelpIconWidget helpButton = new HelpIconWidget();
67  
68      private Button errorAction = new Button();
69  
70      private InlineMessageWidget errorSection = null;
71  
72      private InlineMessageWidget helpSection = null;
73  
74      private String helpDescription = null;
75  
76      private Widget field = null;
77  
78      public FormFieldWrapper() {
79          super();
80          addStyleName("v-form-field-section");
81          root = super.getElement();
82          construct();
83          setHelpEnabled(false);
84  
85          helpButton.addDomHandler(new ClickHandler() {
86              @Override
87              public void onClick(ClickEvent event) {
88                  if (helpSection == null) {
89                      showHelp();
90                  } else {
91                      hideHelp();
92                  }
93              }
94          }, ClickEvent.getType());
95  
96      }
97  
98      public void hideHelp() {
99          if (helpSection != null) {
100             remove(helpSection);
101         }
102         helpSection = null;
103         helpButton.setHighlighted(false);
104     }
105 
106     public void showHelp() {
107         if (helpDescription == null || "".equals(helpDescription)) {
108             return;
109         }
110         helpSection = InlineMessageWidget.createHelpMessage();
111         helpSection.setMessage(helpDescription);
112         add(helpSection, root);
113         helpButton.setHighlighted(true);
114     }
115 
116     private void construct() {
117         label.addClassName("v-form-field-label");
118         fieldWrapper.addClassName("v-form-field-container");
119         errorAction.addStyleName("action-validation");
120 
121         root.appendChild(label);
122         root.appendChild(fieldWrapper);
123         add(helpButton, fieldWrapper);
124         add(errorAction, fieldWrapper);
125     }
126 
127     public void showError(final String errorDescription) {
128         helpButton.setVisible(false);
129         errorAction.setVisible(true);
130         fieldWrapper.addClassName("validation-highlight");
131         if (errorSection == null) {
132             errorSection = InlineMessageWidget.createErrorMessage();
133         }
134         errorSection.setMessage(errorDescription);
135         add(errorSection, root);
136     }
137 
138     public void setCaption(String caption) {
139         label.setInnerHTML(caption);
140         if (caption != null) {
141             // let's show the caption as tooltip, too. This helps if the label is too long. it may contain <span class="requiredfield">*</span>
142             String toolTip = caption.replaceAll("\\<.*?\\>", "");
143             label.setTitle(toolTip);
144         }
145     }
146 
147     @Override
148     public void add(Widget child) {
149         add(child, fieldWrapper);
150     }
151 
152     public void setField(Widget child) {
153         if (this.field != null) {
154             remove(field);
155         }
156         this.field = child;
157         if (child != null) {
158             child.removeFromParent();
159             AriaHelper.bindCaption(child, label);
160             getChildren().add(child);
161             fieldWrapper.insertBefore(child.getElement(), helpButton.getElement());
162             adopt(child);
163         }
164     }
165 
166     public boolean isDisplayingHelpSection() {
167         return helpSection != null;
168     }
169 
170     public void clearError() {
171         if (errorSection != null) {
172             remove(errorSection);
173             errorSection = null;
174         }
175         fieldWrapper.removeClassName("validation-hilight");
176         errorAction.setVisible(false);
177         if (helpDescription != null && !"".equals(helpDescription)) {
178             helpButton.setVisible(true);
179         }
180     }
181 
182     public void setHelpEnabled(boolean isHelpEnabled) {
183         helpButton.setVisible(helpDescription != null && !"".equals(helpDescription) && !errorAction.isVisible());
184         if (!isHelpEnabled && helpSection != null) {
185             hideHelp();
186             return;
187         }
188         if (isHelpEnabled && helpButton.isVisible() && helpSection == null) {
189             showHelp();
190         }
191     }
192 
193     public void setHelpDescription(String description) {
194         this.helpDescription = description;
195         if (helpSection != null && getWidgetIndex(helpSection) >= 0) {
196             helpSection.setMessage(helpDescription);
197         }
198         if (description != null && !"".equals(description)) {
199             helpButton.setVisible(true);
200         } else {
201             helpButton.setVisible(false);
202         }
203     }
204 
205     public void focusField() {
206         if (field != null) {
207             FocusImpl.getFocusImplForWidget().focus(field.getElement());
208         }
209     }
210 
211     public Widget getField() {
212         return field;
213     }
214 
215     @Override
216     public HandlerRegistration addFocusHandler(FocusHandler handler) {
217         return field.addDomHandler(handler, FocusEvent.getType());
218     }
219 
220     @Override
221     public HandlerRegistration addBlurHandler(BlurHandler handler) {
222         return field.addDomHandler(handler, BlurEvent.getType());
223     }
224 }