View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.form.formsection.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.form.tab.widget.FormTabWidget;
37  import info.magnolia.ui.vaadin.gwt.client.form.widget.FormFieldWrapper;
38  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings;
39  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryCallback;
40  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
41  
42  import java.util.ArrayList;
43  import java.util.LinkedHashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  import com.google.gwt.dom.client.Element;
48  import com.google.gwt.user.client.DOM;
49  import com.google.gwt.user.client.Timer;
50  import com.google.gwt.user.client.ui.FlowPanel;
51  import com.google.gwt.user.client.ui.Widget;
52  
53  /**
54   * Layout for the {@link info.magnolia.ui.vaadin.gwt.client.form.widget.FormFieldWrapper} widgets.
55   */
56  public class FormSectionWidget extends FlowPanel {
57  
58      private final Map<Widget, FormFieldWrapper> sections = new LinkedHashMap<Widget, FormFieldWrapper>();
59  
60      private final Element legend = DOM.createElement("legend");
61  
62      private final Element fieldSet = DOM.createElement("fieldset");
63  
64      public FormSectionWidget() {
65          super();
66          getElement().appendChild(fieldSet);
67          fieldSet.appendChild(legend);
68          fieldSet.appendChild(DOM.createElement("hr"));
69      }
70  
71      @Override
72      public void add(Widget child) {
73          throw new UnsupportedOperationException();
74      }
75  
76      @Override
77      public boolean remove(Widget w) {
78          FormFieldWrapper./../../../../../../../info/magnolia/ui/vaadin/gwt/client/form/widget/FormFieldWrapper.html#FormFieldWrapper">FormFieldWrapperfo/magnolia/ui/vaadin/gwt/client/form/widget/FormFieldWrapper.html#FormFieldWrapper">FormFieldWrapper fieldSection = (w instanceof FormFieldWrapper./../../../../../../../info/magnolia/ui/vaadin/gwt/client/form/widget/FormFieldWrapper.html#FormFieldWrapper">FormFieldWrapper) ? (FormFieldWrapper) w : sections.get(w);
79          if (w != null) {
80              sections.remove(w);
81              super.remove(fieldSection);
82          }
83          return false;
84      }
85  
86      @Override
87      public void insert(Widget w, int beforeIndex) {
88          if (sections.containsKey(w)) {
89              return;
90          }
91          FormFieldWrapper fieldSection;
92          if (!(w instanceof FormFieldWrapper)) {
93              fieldSection = new FormFieldWrapper();
94              sections.put(w, fieldSection);
95              fieldSection.setField(w);
96          } else {
97              fieldSection = (FormFieldWrapper) w;
98          }
99  
100         // Patch ComplexPanel's insert logic to keep 0 index for the legend element (element is appended in the constructor).
101         // Although fieldset's legend is usually displayed on top of it regardless of its position in the DOM, this was not the case in older versions of Internet Explorer.
102         beforeIndex = adjustIndex(fieldSection, beforeIndex);
103         fieldSection.removeFromParent();
104         getChildren().insert(fieldSection, beforeIndex); // This widget's child collection remains 0-indexed...
105         DOM.insertChild(fieldSet, fieldSection.getElement(), beforeIndex + 1); // ... but DOM insertion starts with 1.
106         adopt(fieldSection);
107     }
108 
109     @Override
110     public FormTabWidget getParent() {
111         final Widget parent = super.getParent();
112         if (parent == null || !(parent instanceof FormTabWidget)) {
113             return null;
114         }
115         return (FormTabWidget) super.getParent();
116     }
117 
118     public void setDescriptionVisible(boolean isAccessible) {
119         if (!hasDialogDescriptionHeader()) {
120             boolean hasDisplayedFormFieldHelpSection = false;
121             for (final FormFieldWrapper fs : sections.values()) {
122                 if (fs.isDisplayingHelpSection()) {
123                     fs.setHelpEnabled(false);
124                     hasDisplayedFormFieldHelpSection = true;
125                 }
126             }
127             if (hasDisplayedFormFieldHelpSection) {
128                 return;
129             } else if (!isAccessible) {
130                 isAccessible = true;
131             }
132         }
133         for (final FormFieldWrapper fs : sections.values()) {
134             fs.setHelpEnabled(isAccessible);
135         }
136     }
137 
138     private boolean hasDialogDescriptionHeader() {
139         Element element = this.getElement();
140         while (element != null) {
141             if ("dialogDescriptionHeader".equals(element.getAttribute("role"))) {
142                 return true;
143             }
144             element = element.getParentElement();
145         }
146         return false;
147     }
148 
149     public List<FormFieldWrapper> getFields() {
150         return new ArrayList<>(sections.values());
151     }
152 
153     public void setFieldCaption(Widget widget, String caption) {
154         final FormFieldWrapper wrapper = sections.get(widget);
155         if (wrapper != null) {
156             wrapper.setCaption(caption);
157         }
158     }
159 
160     public void setFieldRequired(Widget fieldWidget, boolean isRequired) {
161         final FormFieldWrapper wrapper = sections.get(fieldWidget);
162         if (wrapper != null) {
163             wrapper.setRequired(isRequired);
164         }
165     }
166 
167     public void setFieldDescription(Widget w, String description) {
168         FormFieldWrapper fieldSection = sections.get(w);
169         fieldSection.setHelpDescription(description);
170     }
171 
172     public void setCaption(String caption) {
173         legend.setInnerText(caption);
174     }
175 
176     public void clearError(Widget widget) {
177         sections.get(widget).clearError();
178     }
179 
180     public void setFieldError(Widget widget, String errorMsg) {
181         sections.get(widget).showError(errorMsg);
182     }
183 
184     public void focus(Widget widget) {
185         scrollTo(sections.get(widget));
186     }
187 
188     private void scrollTo(final FormFieldWrapper field) {
189         final int top = JQueryWrapper.select(field).position().top();
190         JQueryWrapper.select((com.google.gwt.user.client.Element) getElement().getParentElement())
191                 .animate(300, new AnimationSettings() {
192                     {
193                         setProperty("scrollTop", top - 30);
194                         addCallback(new JQueryCallback() {
195                             @Override
196                             public void execute(JQueryWrapper query) {
197                                 new Timer() {
198                                     @Override
199                                     public void run() {
200                                         field.focusField();
201                                     }
202                                 }.schedule(300);
203                             }
204                         });
205                     }
206                 });
207     }
208 }