View Javadoc

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