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.connector;
35  
36  import info.magnolia.ui.vaadin.form.FormSection;
37  import info.magnolia.ui.vaadin.gwt.client.form.formsection.widget.FormSectionWidget;
38  import info.magnolia.ui.vaadin.gwt.client.form.rpc.FormSectionClientRpc;
39  import info.magnolia.ui.vaadin.gwt.client.form.tab.connector.FormTabConnector;
40  
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Map;
44  
45  import com.vaadin.client.ComponentConnector;
46  import com.vaadin.client.ConnectorHierarchyChangeEvent;
47  import com.vaadin.client.communication.StateChangeEvent;
48  import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
49  import com.vaadin.client.ui.AbstractLayoutConnector;
50  import com.vaadin.shared.Connector;
51  import com.vaadin.shared.ui.Connect;
52  
53  /**
54   * FormSectionConnector.
55   */
56  @Connect(FormSection.class)
57  public class FormSectionConnector extends AbstractLayoutConnector {
58  
59      @Override
60      protected void init() {
61          super.init();
62          registerRpc(FormSectionClientRpc.class, new FormSectionClientRpc() {
63              @Override
64              public void focus(Connector component) {
65                  getWidget().focus(((ComponentConnector) component).getWidget());
66              }
67          });
68      }
69  
70      private final StateChangeHandler childErrorMessageHandler = new StateChangeHandler() {
71          @Override
72          public void onStateChanged(StateChangeEvent event) {
73              updateChildError((ComponentConnector) event.getConnector());
74          }
75      };
76  
77      private void updateChildError(ComponentConnector connector) {
78          final String errorMsg = connector.getState().errorMessage;
79          boolean errorOccurred = errorMsg != null && !errorMsg.isEmpty();
80          if (getState().isValidationVisible && errorOccurred) {
81              getWidget().setFieldError(connector.getWidget(), errorMsg);
82          } else {
83              getWidget().clearError(connector.getWidget());
84          }
85      }
86  
87      @Override
88      public void onStateChanged(StateChangeEvent stateChangeEvent) {
89          super.onStateChanged(stateChangeEvent);
90          getWidget().setCaption(getState().caption);
91          getWidget().setDescriptionVisible(getState().isDescriptionVisible);
92          for (final ComponentConnector cc : getChildComponents()) {
93              updateChildError(cc);
94          }
95  
96          if (stateChangeEvent.hasPropertyChanged("helpDescriptions")) {
97              final Iterator<Map.Entry<Connector, String>> entryIt = getState().helpDescriptions.entrySet().iterator();
98              while (entryIt.hasNext()) {
99                  Map.Entry<Connector, String> entry = entryIt.next();
100                 getWidget().setFieldDescription(((ComponentConnector)entry.getKey()).getWidget(), entry.getValue());
101             }
102         }
103     }
104 
105     @Override
106     public FormTabConnector getParent() {
107         return (FormTabConnector) super.getParent();
108     }
109 
110     @Override
111     public boolean delegateCaptionHandling() {
112         return false;
113     }
114 
115     @Override
116     public void updateCaption(ComponentConnector connector) {
117         getWidget().setFieldCaption(connector.getWidget(), connector.getState().caption);
118     }
119 
120     @Override
121     public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent e) {
122         final List<ComponentConnector> oldChildren = e.getOldChildren();
123         final List<ComponentConnector> newChildren = getChildComponents();
124 
125         oldChildren.removeAll(newChildren);
126         for (final ComponentConnector cc : oldChildren) {
127             getWidget().remove(cc.getWidget());
128         }
129 
130         int index = 0;
131         for (final ComponentConnector cc : newChildren) {
132             getWidget().insert(cc.getWidget(), index++);
133             cc.addStateChangeHandler("errorMessage", childErrorMessageHandler);
134         }
135     }
136 
137     @Override
138     protected FormSectionState createState() {
139         return new FormSectionState();
140     }
141 
142     @Override
143     public FormSectionState getState() {
144         return (FormSectionState) super.getState();
145     }
146 
147     @Override
148     public FormSectionWidget getWidget() {
149         return (FormSectionWidget) super.getWidget();
150     }
151 
152     @Override
153     public void onUnregister() {
154         super.onUnregister();
155     }
156 }