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