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.form;
35  
36  import info.magnolia.ui.vaadin.form.tab.MagnoliaFormTab;
37  import info.magnolia.ui.vaadin.gwt.client.form.connector.FormState;
38  import info.magnolia.ui.vaadin.gwt.client.form.rpc.FormServerRpc;
39  import info.magnolia.ui.vaadin.tabsheet.MagnoliaTab;
40  import info.magnolia.ui.vaadin.tabsheet.MagnoliaTabSheet;
41  
42  import java.util.Collection;
43  import java.util.Iterator;
44  import java.util.LinkedList;
45  import java.util.List;
46  
47  import com.vaadin.data.Item;
48  import com.vaadin.data.Property;
49  import com.vaadin.shared.Connector;
50  import com.vaadin.ui.AbstractSingleComponentContainer;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.Field;
53  
54  /**
55   * Server side implementation of the form view. Displays the form inside a {@link MagnoliaTabSheet}.
56   */
57  public class Form extends AbstractSingleComponentContainer implements FormViewReduced {
58  
59      private List<Field<?>> fields = new LinkedList<Field<?>>();
60  
61      private Item itemDataSource;
62  
63      private boolean isValidationVisible = false;
64  
65      private final MagnoliaTabSheet tabSheet = new MagnoliaTabSheet() {
66          @Override
67          public MagnoliaFormTab addTab(final String caption, final Component c) {
68              if (c instanceof FormSection) {
69                  final FormSection section = (FormSection) c;
70                  final MagnoliaFormTab tab = new MagnoliaFormTab(caption, section);
71                  tab.setClosable(false);
72                  doAddTab(tab);
73                  return tab;
74              }
75              throw new IllegalArgumentException("TabSheet inside a Form should only receive the FormSection objects as tab content."); //TODO-TRANSLATE-EXCEPTION
76          }
77      };
78  
79      public Form() {
80          super();
81          setStyleName("v-magnolia-form");
82          tabSheet.setSizeFull();
83          tabSheet.showAllTab(true, "");
84          setContent(tabSheet);
85          registerRpc(new FormServerRpc() {
86              @Override
87              public void focusNextProblematicField(Connector currentFocused) {
88                  doFocusNextProblematicField(currentFocused);
89              }
90          });
91      }
92  
93      public void focusFirstField() {
94          if (fields.isEmpty()) {
95              return;
96          }
97          fields.get(0).focus();
98      }
99  
100     private void doFocusNextProblematicField(Connector currentFocused) {
101         /**
102          * In case the remaining issues are in the current tab above current focus -
103          * we need to wrap the search to check the current tab once we've
104          * investigated all the others
105          */
106         int tabsToIterate = tabSheet.getComponentCount() + 1;
107         MagnoliaTab tab = tabSheet.getActiveTab();
108         FormSection section;
109         Component nextProblematic;
110         do {
111             section = (FormSection) tab.getContent();
112             nextProblematic = section.getNextProblematicField(currentFocused);
113             if (nextProblematic == null) {
114                 tab = tabSheet.getNextTab(tab);
115                 tabsToIterate--;
116                 // After testing the first section - we want to check ALL fields per section.
117                 currentFocused = null;
118             }
119 
120         } while (nextProblematic == null && tabsToIterate > 0);
121 
122         // focus next tab and field
123         if (nextProblematic != null) {
124             tabSheet.setActiveTab(tab);
125             section.focusField(nextProblematic);
126         }
127     }
128 
129     @Override
130     public void setDescriptionVisibility(boolean isVisible) {
131         getState().descriptionsVisible = isVisible;
132     }
133 
134     @Override
135     public void setItemDataSource(Item newDataSource) {
136         this.itemDataSource = newDataSource;
137     }
138 
139     @Override
140     public Item getItemDataSource() {
141         return itemDataSource;
142     }
143 
144     @Override
145     public void addFormSection(String tabName, FormSection inputFields) {
146         tabSheet.addTab(tabName, inputFields);
147     }
148 
149     @Override
150     public void addField(Field<?> field) {
151         fields.add(field);
152         field.addValueChangeListener(new Property.ValueChangeListener() {
153             @Override
154             public void valueChange(Property.ValueChangeEvent event) {
155                 invalidateErrorAmount();
156             }
157         });
158     }
159 
160     public void setErrorLabels(String errorsLabel, String nextErrorLabel){
161         getState().errorsLabel = errorsLabel;
162         getState().nextErrorLabel = nextErrorLabel;
163     }
164 
165     private void invalidateErrorAmount() {
166         getState().errorAmount = -1;
167     }
168 
169     @Override
170     public Collection<Field<?>> getFields() {
171         return fields;
172     }
173 
174     @Override
175     public boolean isValid() {
176         boolean res = true;
177         getState().errorAmount = 0;
178         for (Field<?> field : getFields()) {
179             boolean isFieldValid = field.isValid();
180             if(!isFieldValid){
181                 ++getState().errorAmount;
182             }
183             res &= isFieldValid;
184         }
185         return res;
186     }
187 
188     @Override
189     public void showValidation(boolean isVisible) {
190         isValidationVisible = isVisible;
191 
192         if (isVisible) {
193             invalidateErrorAmount();
194         }
195 
196         final Iterator<Component> it = tabSheet.iterator();
197         while (it.hasNext()) {
198             final Component c = it.next();
199             if (c instanceof MagnoliaFormTab) {
200                 ((MagnoliaFormTab) c).setValidationVisible(isVisible);
201             }
202         }
203     }
204 
205     /**
206      * @deprecated as of 5.1.1, please get the tabsheet through {@link #getContent()} and set the show all behavior there, along with its i18nized caption.
207      */
208     @Override
209     @Deprecated
210     public void setShowAllEnabled(boolean enabled) {
211         tabSheet.showAllTab(enabled, "");
212     }
213 
214     @Override
215     protected FormState getState() {
216         return (FormState) super.getState();
217     }
218 
219     @Override
220     protected FormState getState(boolean markAsDirty) {
221         return (FormState) super.getState(markAsDirty);
222     }
223 
224     @Override
225     public MagnoliaTabSheet getContent() {
226         return (MagnoliaTabSheet) super.getContent();
227     }
228 
229     @Override
230     public Form asVaadinComponent() {
231         return this;
232     }
233 }