View Javadoc

1   /**
2    * This file Copyright (c) 2010-2012 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.VFormSection;
37  
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.LinkedList;
41  import java.util.List;
42  import java.util.Map;
43  
44  import com.vaadin.terminal.ErrorMessage;
45  import com.vaadin.terminal.PaintException;
46  import com.vaadin.terminal.PaintTarget;
47  import com.vaadin.ui.AbstractComponent;
48  import com.vaadin.ui.AbstractLayout;
49  import com.vaadin.ui.ClientWidget;
50  import com.vaadin.ui.ClientWidget.LoadStyle;
51  import com.vaadin.ui.Component;
52  
53  /**
54   * Form layout server side implementation.
55   */
56  @ClientWidget(value = VFormSection.class, loadStyle = LoadStyle.EAGER)
57  public class FormSection extends AbstractLayout {
58  
59      private boolean isValidationVisible = false;
60  
61      private final List<Component> components = new LinkedList<Component>();
62  
63      private final Map<Component, String> helpDescriptions = new HashMap<Component, String>();
64  
65      public FormSection() {
66          addStyleName("v-form-layout");
67      }
68  
69      @Override
70      public void paintContent(PaintTarget target) throws PaintException {
71          super.paintContent(target);
72          final Iterator<Component> it = getComponentIterator();
73          target.addAttribute("validationVisible", isValidationVisible);
74          while (it.hasNext()) {
75              final Component c = it.next();
76              target.startTag("component");
77              c.addStyleName("v-form-field");
78              c.setSizeUndefined();
79              c.paint(target);
80              if (helpDescriptions.containsKey(c)) {
81                  target.addAttribute("helpDescription", helpDescriptions.get(c));
82              }
83              target.endTag("component");
84          }
85      }
86  
87      public void setComponentHelpDescription(Component c, String description) {
88          if (components.contains(c)) {
89              helpDescriptions.put(c, description);
90              requestRepaint();
91          } else {
92              throw new IllegalArgumentException("Layout doesn't contain this component.");
93          }
94      }
95  
96      @Override
97      public void addComponent(Component c) {
98          super.addComponent(c);
99          components.add(c);
100         requestRepaint();
101     }
102 
103     @Override
104     public void replaceComponent(Component oldComponent, Component newComponent) {
105     }
106 
107     @Override
108     public Iterator<Component> getComponentIterator() {
109         return components.iterator();
110     }
111 
112     public void setValidationVisible(boolean isVisible) {
113         this.isValidationVisible = isVisible;
114         requestRepaint();
115     }
116 
117     @Override
118     public ErrorMessage getErrorMessage() {
119         final Iterator<Component> it = getComponentIterator();
120         while (it.hasNext()) {
121             final Component c = it.next();
122             if (c instanceof AbstractComponent) {
123                 final ErrorMessage errMsg = ((AbstractComponent) c).getErrorMessage();
124                 if (errMsg != null) {
125                     return errMsg;
126                 }
127             }
128         }
129         return super.getErrorMessage();
130     }
131 
132     public boolean hasError() {
133         return getErrorMessage() != null && isValidationVisible;
134     }
135 
136 }