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