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.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          final Iterator<Map.Entry<Connector, String>> entryIt = getState().helpDescriptions.entrySet().iterator();
71          while (entryIt.hasNext()) {
72              Map.Entry<Connector, String> entry = entryIt.next();
73              getWidget().setFieldDescription(((ComponentConnector)entry.getKey()).getWidget(), entry.getValue());
74          }
75  
76      }
77  
78      private final StateChangeHandler childErrorMessageHandler = new StateChangeHandler() {
79          @Override
80          public void onStateChanged(StateChangeEvent event) {
81              updateChildError((ComponentConnector) event.getConnector());
82          }
83      };
84  
85      private void updateChildError(ComponentConnector connector) {
86          final String errorMsg = connector.getState().errorMessage;
87          boolean errorOccurred = errorMsg != null && !errorMsg.isEmpty();
88          if (getState().isValidationVisible && errorOccurred) {
89              getWidget().setFieldError(connector.getWidget(), errorMsg);
90          } else {
91              getWidget().clearError(connector.getWidget());
92          }
93      }
94  
95      @Override
96      public void onStateChanged(StateChangeEvent stateChangeEvent) {
97          super.onStateChanged(stateChangeEvent);
98          getWidget().setCaption(getState().caption);
99          for (final ComponentConnector cc : getChildComponents()) {
100             updateChildError(cc);
101         }
102 
103         if (stateChangeEvent.hasPropertyChanged("helpDescriptions")) {
104             final Iterator<Map.Entry<Connector, String>> entryIt = getState().helpDescriptions.entrySet().iterator();
105             while (entryIt.hasNext()) {
106                 Map.Entry<Connector, String> entry = entryIt.next();
107                 getWidget().setFieldDescription(((ComponentConnector)entry.getKey()).getWidget(), entry.getValue());
108             }
109         }
110     }
111 
112     @Override
113     public FormTabConnector getParent() {
114         return (FormTabConnector) super.getParent();
115     }
116 
117     @Override
118     public boolean delegateCaptionHandling() {
119         return false;
120     }
121 
122     @Override
123     public void updateCaption(ComponentConnector connector) {
124         getWidget().setFieldCaption(connector.getWidget(), connector.getState().caption);
125     }
126 
127     @Override
128     public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent e) {
129         final List<ComponentConnector> oldChildren = e.getOldChildren();
130         final List<ComponentConnector> newChildren = getChildComponents();
131 
132         oldChildren.removeAll(newChildren);
133         for (final ComponentConnector cc : oldChildren) {
134             getWidget().remove(cc.getWidget());
135         }
136 
137         int index = 0;
138         for (final ComponentConnector cc : newChildren) {
139             getWidget().insert(cc.getWidget(), index++);
140             cc.addStateChangeHandler("errorMessage", childErrorMessageHandler);
141         }
142     }
143 
144     @Override
145     protected FormSectionState createState() {
146         return new FormSectionState();
147     }
148 
149     @Override
150     public FormSectionState getState() {
151         return (FormSectionState) super.getState();
152     }
153 
154     @Override
155     public FormSectionWidget getWidget() {
156         return (FormSectionWidget) super.getWidget();
157     }
158 
159     @Override
160     public void onUnregister() {
161         super.onUnregister();
162     }
163 }