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