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