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.form;
35  
36  import info.magnolia.ui.vaadin.gwt.client.form.formsection.connector.FormSectionState;
37  import info.magnolia.ui.vaadin.gwt.client.form.rpc.FormSectionClientRpc;
38  
39  import java.util.Iterator;
40  import java.util.LinkedList;
41  import java.util.List;
42  
43  import com.vaadin.shared.Connector;
44  import com.vaadin.ui.AbstractLayout;
45  import com.vaadin.ui.Component;
46  import com.vaadin.v7.ui.AbstractField;
47  
48  /**
49   * Form layout server side implementation.
50   */
51  public class FormSection extends AbstractLayout {
52  
53      private final List<Component> components = new LinkedList<Component>();
54  
55      private String name;
56  
57      public FormSection() {
58          addStyleName("v-form-layout");
59      }
60  
61      @Override
62      protected FormSectionState getState() {
63          return (FormSectionState) super.getState();
64      }
65  
66      @Override
67      protected FormSectionState getState(boolean markAsDirty) {
68          return (FormSectionState) super.getState(markAsDirty);
69      }
70  
71      public void setComponentHelpDescription(Component c, String description) {
72          if (components.contains(c)) {
73              getState().helpDescriptions.put(c, description);
74          } else {
75              throw new IllegalArgumentException("Layout doesn't contain this component.");
76          }
77      }
78  
79      @Override
80      public void addComponent(Component c) {
81          super.addComponent(c);
82          components.add(c);
83          c.addStyleName("v-form-field");
84          markAsDirty();
85      }
86  
87      @Override
88      public void removeComponent(Component c) {
89          super.removeComponent(c);
90          components.remove(c);
91          markAsDirty();
92      }
93  
94      @Override
95      public void replaceComponent(Component oldComponent, Component newComponent) {
96      }
97  
98      @Override
99      public Iterator<Component> iterator() {
100         return components.iterator();
101     }
102 
103     public void setValidationVisible(boolean isVisible) {
104         getState().isValidationVisible = isVisible;
105     }
106 
107     @Override
108     public int getComponentCount() {
109         return components.size();
110     }
111 
112     public Component getNextProblematicField(Connector currentFocused) {
113         int startIndex = components.indexOf(currentFocused) + 1;
114         while (startIndex < components.size()) {
115             Component c = components.get(startIndex++);
116             if (c instanceof AbstractField && !((AbstractField<?>) c).isValid()) {
117                 return c;
118             }
119         }
120         return null;
121     }
122 
123     public void focusField(Component field) {
124         getRpcProxy(FormSectionClientRpc.class).focus(field);
125     }
126 
127     public String getName() {
128         return name;
129     }
130 
131     public void setName(String name) {
132         this.name = name;
133     }
134 
135     public List<Component> getComponents() {
136         return this.components;
137     }
138 }