View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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.Style.Display;
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          // both only display when show all tab is active
71          horizontalRule.getStyle().setDisplay(Display.NONE);
72          legend.getStyle().setDisplay(Display.NONE);
73          fieldSet.appendChild(horizontalRule);
74      }
75  
76      @Override
77      public void add(Widget child) {
78          throw new UnsupportedOperationException();
79      }
80  
81      @Override
82      public boolean remove(Widget w) {
83          FormFieldWrapper fieldSection = (w instanceof FormFieldWrapper) ? (FormFieldWrapper) w : sections.get(w);
84          if (w != null) {
85              sections.remove(w);
86              super.remove(fieldSection);
87          }
88          return false;
89      }
90  
91      @Override
92      public void insert(Widget w, int beforeIndex) {
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         super.insert(fieldSection, fieldSet, beforeIndex, true);
102     }
103 
104     @Override
105     public FormTabWidget getParent() {
106         final Widget parent = super.getParent();
107         if (parent == null || !(parent instanceof FormTabWidget)) {
108             return null;
109         }
110         return (FormTabWidget) super.getParent();
111     }
112 
113     public void setDescriptionVisible(boolean isAccessible) {
114         for (final FormFieldWrapper fs : sections.values()) {
115             fs.setHelpEnabled(isAccessible);
116         }
117     }
118 
119     public List<FormFieldWrapper> getFields() {
120         return new ArrayList<FormFieldWrapper>(sections.values());
121     }
122 
123     public void setFieldCaption(Widget widget, String caption) {
124         final FormFieldWrapper wrapper = sections.get(widget);
125         if (wrapper != null) {
126             wrapper.setCaption(caption);
127         }
128     }
129 
130     public void setFieldDescription(Widget w, String description) {
131         FormFieldWrapper fieldSection = sections.get(w);
132         fieldSection.setHelpDescription(description);
133     }
134 
135     public void setCaption(String caption) {
136         legend.setInnerText(caption);
137         fieldSet.appendChild(legend);
138     }
139 
140     public void clearError(Widget widget) {
141         sections.get(widget).clearError();
142     }
143 
144     public void setFieldError(Widget widget, String errorMsg) {
145         sections.get(widget).showError(errorMsg);
146     }
147 
148     public void focus(Widget widget) {
149         scrollTo(sections.get(widget));
150     }
151 
152     private void scrollTo(final FormFieldWrapper field) {
153         final int top = JQueryWrapper.select(field).position().top();
154         JQueryWrapper.select((Element) getElement().getParentElement().cast())
155                 .animate(300, new AnimationSettings() {
156                     {
157                         setProperty("scrollTop", top - 30);
158                         addCallback(new JQueryCallback() {
159                             @Override
160                             public void execute(JQueryWrapper query) {
161                                 new Timer() {
162                                     @Override
163                                     public void run() {
164                                         field.focusField();
165                                     }
166                                 }.schedule(300);
167                             }
168                         });
169                     }
170                 });
171     }
172 }