View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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.dialog.connector;
35  
36  import info.magnolia.ui.vaadin.dialog.BaseDialog;
37  import info.magnolia.ui.vaadin.gwt.client.dialog.rpc.DialogServerRpc;
38  import info.magnolia.ui.vaadin.gwt.client.dialog.widget.BaseDialogView;
39  import info.magnolia.ui.vaadin.gwt.client.dialog.widget.BaseDialogViewImpl;
40  
41  import com.google.gwt.user.client.ui.Widget;
42  import com.vaadin.client.ComponentConnector;
43  import com.vaadin.client.ConnectorHierarchyChangeEvent;
44  import com.vaadin.client.communication.RpcProxy;
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.ui.Connect;
49  
50  /**
51   * BaseDialogConnector.
52   */
53  @Connect(BaseDialog.class)
54  public class BaseDialogConnector extends AbstractLayoutConnector implements BaseDialogView.Presenter {
55  
56      private final DialogServerRpc rpc = RpcProxy.create(DialogServerRpc.class, this);
57  
58      protected BaseDialogView createView() {
59          return new BaseDialogViewImpl();
60      }
61  
62      private BaseDialogView view;
63  
64      @Override
65      protected void init() {
66          super.init();
67          addStateChangeHandler("caption", new StateChangeHandler() {
68              @Override
69              public void onStateChanged(StateChangeEvent stateChangeEvent) {
70                  view.setCaption(getState().caption);
71              }
72          });
73  
74          addStateChangeHandler("hasCloseButton", new StateChangeHandler() {
75              @Override
76              public void onStateChanged(StateChangeEvent stateChangeEvent) {
77                  if (getState().hasCloseButton) {
78                      view.showCloseButton();
79                  }
80              }
81          });
82  
83          addStateChangeHandler("isWide", new StateChangeHandler() {
84              @Override
85              public void onStateChanged(StateChangeEvent stateChangeEvent) {
86                  view.setWide(getState().isWide);
87              }
88          });
89  
90          addStateChangeHandler("componentDescription", new StateChangeHandler() {
91              @Override
92              public void onStateChanged(StateChangeEvent stateChangeEvent) {
93                  view.setDescription(getState().componentDescription);
94              }
95          });
96      }
97  
98      @Override
99      protected BaseDialogState createState() {
100         return new BaseDialogState();
101     }
102 
103     @Override
104     public BaseDialogState getState() {
105         return (BaseDialogState) super.getState();
106     }
107 
108     @Override
109     public void updateCaption(ComponentConnector connector) {
110         if (this == connector) {
111             view.setCaption(connector.getState().caption);
112         }
113     }
114 
115     @Override
116     public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
117         updateContent();
118         updateHeaderToolbar();
119         updateFooterToolbar();
120     }
121 
122     @Override
123     public boolean delegateCaptionHandling() {
124         return false;
125     }
126 
127     protected ComponentConnector getContent() {
128         return (ComponentConnector) getState().content;
129     }
130 
131     protected ComponentConnector getHeader() {
132         return (ComponentConnector) getState().headerToolbar;
133     }
134 
135     protected ComponentConnector getFooter() {
136         return (ComponentConnector) getState().footerToolbar;
137     }
138 
139     protected void updateContent() {
140         final ComponentConnector content = getContent();
141         if (content != null) {
142             this.view.setContent(content.getWidget());
143         }
144     }
145 
146     protected void updateHeaderToolbar() {
147         final ComponentConnector header = getHeader();
148         if (header != null) {
149             this.view.setHeaderToolbar(header.getWidget());
150         }
151     }
152 
153     protected void updateFooterToolbar() {
154         final ComponentConnector footer = getFooter();
155         if (footer != null) {
156             this.view.setFooterToolbar(footer.getWidget());
157         }
158     }
159 
160     @Override
161     protected Widget createWidget() {
162         this.view = createView();
163         this.view.setPresenter(this);
164         return view.asWidget();
165     }
166 
167     // RPC methods to communicate back to the server.
168 
169     @Override
170     public void closeDialog() {
171         rpc.closeSelf();
172     }
173 
174     @Override
175     public void setWide(boolean isWide) {
176         rpc.setWide(isWide);
177     }
178 
179     @Override
180     public void setDescriptionVisibility(boolean isVisible) {
181         rpc.setDescriptionVisibility(isVisible);
182     }
183 }