View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.tabsheet.connector;
35  
36  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.ActiveTabChangedEvent;
37  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.ShowAllTabsEvent;
38  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.ShowAllTabsHandler;
39  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.TabCloseEvent;
40  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.TabCloseEventHandler;
41  import info.magnolia.ui.vaadin.gwt.client.tabsheet.rpc.MagnoliaTabSheetClientRpc;
42  import info.magnolia.ui.vaadin.gwt.client.tabsheet.rpc.MagnoliaTabSheetServerRpc;
43  import info.magnolia.ui.vaadin.gwt.client.tabsheet.tab.connector.MagnoliaTabConnector;
44  import info.magnolia.ui.vaadin.gwt.client.tabsheet.tab.widget.MagnoliaTabWidget;
45  import info.magnolia.ui.vaadin.gwt.client.tabsheet.widget.MagnoliaTabSheetView;
46  import info.magnolia.ui.vaadin.gwt.client.tabsheet.widget.MagnoliaTabSheetViewImpl;
47  import info.magnolia.ui.vaadin.tabsheet.MagnoliaTabSheet;
48  
49  import java.util.List;
50  
51  import com.google.gwt.dom.client.Style;
52  import com.google.gwt.user.client.ui.Widget;
53  import com.google.web.bindery.event.shared.EventBus;
54  import com.google.web.bindery.event.shared.SimpleEventBus;
55  import com.vaadin.client.ComponentConnector;
56  import com.vaadin.client.ConnectorHierarchyChangeEvent;
57  import com.vaadin.client.Util;
58  import com.vaadin.client.communication.RpcProxy;
59  import com.vaadin.client.communication.StateChangeEvent;
60  import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
61  import com.vaadin.client.ui.AbstractComponentContainerConnector;
62  import com.vaadin.client.ui.PostLayoutListener;
63  import com.vaadin.client.ui.layout.ElementResizeEvent;
64  import com.vaadin.client.ui.layout.ElementResizeListener;
65  import com.vaadin.shared.Connector;
66  import com.vaadin.shared.ui.Connect;
67  
68  /**
69   * Client-side connector, counter-part of {@link MagnoliaTabSheet}.
70   *
71   * @deprecated Since 6.1, Use Vaadin TabSheet component
72   */
73  @Deprecated
74  
75  @Connect(MagnoliaTabSheet.class)
76  public class MagnoliaTabSheetConnector extends AbstractComponentContainerConnector implements PostLayoutListener {
77  
78      private final MagnoliaTabSheetServerRpc rpc = RpcProxy.create(MagnoliaTabSheetServerRpc.class, this);
79  
80      private MagnoliaTabSheetView view;
81  
82      private final EventBus eventBus = new SimpleEventBus();
83  
84      @Override
85      public MagnoliaTabSheetState getState() {
86          return (MagnoliaTabSheetState) super.getState();
87      }
88  
89      @Override
90      protected Widget createWidget() {
91          this.view = new MagnoliaTabSheetViewImpl(eventBus);
92          return view.asWidget();
93      }
94  
95      @Override
96      protected void init() {
97          super.init();
98  
99          addStateChangeHandler("activeTab", new StateChangeHandler() {
100             @Override
101             public void onStateChanged(StateChangeEvent event) {
102                 final MagnoliaTabConnector/../info/magnolia/ui/vaadin/gwt/client/tabsheet/tab/connector/MagnoliaTabConnector.html#MagnoliaTabConnector">MagnoliaTabConnector tabConnector = (MagnoliaTabConnector) getState().activeTab;
103                 if (tabConnector != null) {
104                     view.setActiveTab(tabConnector.getWidget());
105                     eventBus.fireEvent(new ActiveTabChangedEvent(tabConnector.getWidget(), false));
106                 }
107             }
108         });
109 
110         addStateChangeHandler(new StateChangeHandler() {
111             @Override
112             public void onStateChanged(StateChangeEvent event) {
113                 if (event.hasPropertyChanged("showAllEnabled")) {
114                     view.getTabContainer().addShowAllTab(getState().showAllEnabled, getState().showAllLabel);
115                 }
116 
117                 if (getState().logo != null && getState().logoBgColor != null) {
118                     view.setLogo(getState().logo, getState().logoBgColor);
119                 }
120             }
121         });
122 
123         registerRpc(MagnoliaTabSheetClientRpc.class, new MagnoliaTabSheetClientRpc() {
124 
125             @Override
126             public void closeTab(Connector tabConnector) {
127                 eventBus.fireEvent(new TabCloseEvent(((MagnoliaTabConnector) tabConnector).getWidget()));
128             }
129         });
130 
131         eventBus.addHandler(TabCloseEvent.TYPE, new TabCloseEventHandler() {
132             @Override
133             public void onTabClosed(TabCloseEvent event) {
134                 MagnoliaTabWidget tab = event.getTab();
135                 if (tab == view.getActiveTab()) {
136                     view.showPreloader();
137                     tab.getWidget().getElement().getStyle().setDisplay(Style.Display.NONE);
138                 }
139                 rpc.closeTab(Util.findConnectorFor(tab));
140             }
141         });
142 
143         eventBus.addHandler(ActiveTabChangedEvent.TYPE, new ActiveTabChangedEvent.Handler() {
144             @Override
145             public void onActiveTabChanged(final ActiveTabChangedEvent event) {
146                 MagnoliaTabWidget tab = event.getTab();
147                 if (view.isShowingAllTabs()) {
148                     view.setActiveTab(tab);
149                     tab = null; // force preloader and animation when returning to same tab for consistency reasons
150                 }
151                 if (tab != view.getActiveTab() && event.isNotifyServer()) {
152                     view.showPreloader();
153                     view.clearTabs();
154                     rpc.setActiveTab(Util.findConnectorFor(event.getTab()));
155                 }
156             }
157         });
158 
159         eventBus.addHandler(ShowAllTabsEvent.TYPE, new ShowAllTabsHandler() {
160             @Override
161             public void onShowAllTabs(ShowAllTabsEvent event) {
162                 rpc.setShowAll();
163                 view.showAllTabContents(true);
164                 view.getTabContainer().showAll(true);
165                 fireEvent(new ActiveTabChangedEvent(true, false));
166             }
167 
168         });
169 
170         getLayoutManager().addElementResizeListener(view.getTabContainer().getElement(), listener);
171     }
172 
173     private final ElementResizeListener listener = new ElementResizeListener() {
174         @Override
175         public void onElementResize(ElementResizeEvent e) {
176             view.onResize();
177         }
178     };
179 
180     @Override
181     public void updateCaption(ComponentConnector connector) {
182         final String caption = connector.getState().caption;
183         if (connector.getWidget() instanceof MagnoliaTabWidget) {
184             MagnoliaTabWidget/../../../../../info/magnolia/ui/vaadin/gwt/client/tabsheet/tab/widget/MagnoliaTabWidget.html#MagnoliaTabWidget">MagnoliaTabWidget tab = (MagnoliaTabWidget) connector.getWidget();
185             tab.getLabel().updateCaption(caption);
186             // rearrange tabs after caption update (here we know exact width of the tab)
187             view.getTabContainer().reArrangeTabVisibility();
188         }
189     }
190 
191     @Override
192     public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
193         final List<ComponentConnector> childConnectors = getChildComponents();
194         final List<ComponentConnector> oldChildren = event.getOldChildren();
195 
196         oldChildren.removeAll(childConnectors);
197         for (final ComponentConnector cc : oldChildren) {
198             view.removeTab((MagnoliaTabWidget) cc.getWidget());
199         }
200 
201         for (final ComponentConnector cc : childConnectors) {
202             view.updateTab((MagnoliaTabWidget) cc.getWidget());
203         }
204     }
205 
206     @Override
207     public void onUnregister() {
208         getLayoutManager().removeElementResizeListener(view.getTabContainer().getElement(), listener);
209     }
210 
211     @Override
212     public void postLayout() {
213         view.removePreloader();
214     }
215 }