View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.gwt.client.dialog.rpc.OverlayClientRpc;
37  import info.magnolia.ui.vaadin.gwt.client.dialog.rpc.OverlayServerRpc;
38  import info.magnolia.ui.vaadin.gwt.client.dialog.widget.OverlayWidget;
39  import info.magnolia.ui.vaadin.overlay.Overlay;
40  
41  import com.google.gwt.core.client.Scheduler;
42  import com.google.gwt.user.client.Timer;
43  import com.google.gwt.user.client.ui.Widget;
44  import com.vaadin.client.ComponentConnector;
45  import com.vaadin.client.ConnectorHierarchyChangeEvent;
46  import com.vaadin.client.communication.RpcProxy;
47  import com.vaadin.client.communication.StateChangeEvent;
48  import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
49  import com.vaadin.shared.ui.Connect;
50  
51  /**
52   * Client-side connector for Overlay component.
53   */
54  @Connect(Overlay.class)
55  public class OverlayConnector extends AbstractSingleComponentContainerConnector {
56  
57      public static final int OVERLAY_CLOSE_ANIMATION_DURATION_MSEC = 500;
58  
59      private OverlayServerRpc rpc = RpcProxy.create(OverlayServerRpc.class, this);
60  
61      private Timer automaticRemovalTimer = new Timer() {
62          @Override
63          public void run() {
64              removeSelf();
65          }
66      };
67  
68      @Override
69      protected void init() {
70          super.init();
71          registerRpc(OverlayClientRpc.class, new OverlayClientRpc() {
72              @Override
73              public void close() {
74                  removeSelf();
75              }
76          });
77      }
78  
79      public void removeSelf() {
80          final Object lock = new Object();
81          getWidget().addStyleName("close");
82          getConnection().getMessageHandler().suspendReponseHandling(lock);
83          final Widget w = getWidget();
84          Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
85              @Override
86              public boolean execute() {
87                  getConnection().getMessageHandler().resumeResponseHandling(lock);
88                  w.removeFromParent();
89                  rpc.onClosed();
90                  return false;
91              }
92          }, OVERLAY_CLOSE_ANIMATION_DURATION_MSEC);
93      }
94  
95      @Override
96      public void onStateChanged(StateChangeEvent event) {
97          super.onStateChanged(event);
98          if (event.hasPropertyChanged("closeTimeout")) {
99              int timeout = getState().closeTimeout;
100             if (timeout < 0) {
101                 automaticRemovalTimer.cancel();
102             } else {
103                 automaticRemovalTimer.schedule(timeout * 1000);
104             }
105         }
106     }
107 
108     @Override
109     public void updateCaption(ComponentConnector connector) {
110     }
111 
112     @Override
113     public OverlayWidget getWidget() {
114         return (OverlayWidget) super.getWidget();
115     }
116 
117     @Override
118     public void onConnectorHierarchyChange(final ConnectorHierarchyChangeEvent e) {
119         if (getContent() != null) {
120             getWidget().setWidget(getContent().getWidget());
121         }
122     }
123 
124     @Override
125     public OverlayState getState() {
126         return (OverlayState) super.getState();
127     }
128 }