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