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.tabsheet.widget;
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.tab.widget.MagnoliaTabLabel;
42  import info.magnolia.ui.vaadin.gwt.client.tabsheet.tab.widget.MagnoliaTabWidget;
43  import info.magnolia.ui.vaadin.gwt.client.tabsheet.util.CollectionUtil;
44  
45  import java.util.LinkedList;
46  import java.util.List;
47  
48  import com.google.gwt.dom.client.NativeEvent;
49  import com.google.gwt.event.dom.client.ClickEvent;
50  import com.google.gwt.event.dom.client.ClickHandler;
51  import com.google.gwt.user.client.DOM;
52  import com.google.gwt.user.client.Element;
53  import com.google.gwt.user.client.ui.ComplexPanel;
54  import com.google.gwt.user.client.ui.SimplePanel;
55  import com.google.web.bindery.event.shared.EventBus;
56  import com.vaadin.client.ui.VButton;
57  
58  /**
59   * A bar that contains the tab labels and controls the switching between tabs.
60   */
61  public class TabBarWidget extends ComplexPanel {
62  
63      private static final String SINGLE_TAB_CLASS_NAME = "single-tab";
64  
65      private final List<MagnoliaTabLabel> tabLabels = new LinkedList<MagnoliaTabLabel>();
66  
67      private final Element tabContainer = DOM.createElement("ul");
68  
69      private EventBus eventBus;
70  
71      private VShellShowAllTabLabel showAllTab;
72  
73      public TabBarWidget(EventBus eventBus) {
74          this.eventBus = eventBus;
75          setElement(tabContainer);
76          setStyleName("nav");
77          addStyleDependentName("tabs");
78      }
79  
80      @Override
81      protected void onLoad() {
82          super.onLoad();
83          bindHandlers();
84      }
85  
86      private void bindHandlers() {
87          eventBus.addHandler(ActiveTabChangedEvent.TYPE, new ActiveTabChangedEvent.Handler() {
88              @Override
89              public void onActiveTabChanged(final ActiveTabChangedEvent event) {
90                  final MagnoliaTabWidget tab = event.getTab();
91                  final MagnoliaTabLabel label = tab.getLabel();
92                  if (label != null) {
93                      for (final MagnoliaTabLabel tabLabel : tabLabels) {
94                          tabLabel.removeStyleName("active");
95                      }
96                      label.addStyleName("active");
97                      showAll(false);
98                  }
99              }
100         });
101 
102         eventBus.addHandler(TabCloseEvent.TYPE, new TabCloseEventHandler() {
103             @Override
104             public void onTabClosed(TabCloseEvent event) {
105                 final MagnoliaTabLabel tabLabel = event.getTab().getLabel();
106                 boolean wasActive = tabLabel.getStyleName().contains("active");
107                 if (wasActive) {
108                     final MagnoliaTabLabel nextLabel = getNextLabel(tabLabel);
109                     if (nextLabel != null) {
110                         nextLabel.addStyleName("active");
111                     }
112                 }
113                 tabLabels.remove(tabLabel);
114                 remove(tabLabel);
115                 updateSingleTabStyle();
116             }
117         });
118 
119         eventBus.addHandler(ShowAllTabsEvent.TYPE, new ShowAllTabsHandler() {
120 
121             @Override
122             public void onShowAllTabs(ShowAllTabsEvent event) {
123                 for (final MagnoliaTabLabel tabLabel : tabLabels) {
124                     tabLabel.removeStyleName("active");
125                 }
126                 showAll(true);
127             }
128         });
129     }
130 
131     protected MagnoliaTabLabel getNextLabel(final MagnoliaTabLabel label) {
132         return CollectionUtil.getNext(tabLabels, label);
133     }
134 
135     public void addTabLabel(MagnoliaTabLabel label) {
136         label.setEventBus(eventBus);
137         if (!tabLabels.contains(label)) {
138             tabLabels.add(label);
139             add(label, getElement());
140             updateSingleTabStyle();
141         }
142     }
143 
144     public void updateSingleTabStyle() {
145         if (tabLabels.size() <= 1) {
146             tabContainer.addClassName(SINGLE_TAB_CLASS_NAME);
147         } else {
148             tabContainer.removeClassName(SINGLE_TAB_CLASS_NAME);
149         }
150     }
151 
152     public void addShowAllTab(boolean showAll, String label) {
153         if (showAll && showAllTab == null) {
154             showAllTab = new VShellShowAllTabLabel(label);
155             add(showAllTab, getElement());
156         } else if (!showAll && showAllTab != null) {
157             remove(showAllTab);
158             showAllTab = null;
159         }
160     }
161 
162     private class VShellShowAllTabLabel extends SimplePanel {
163 
164         private final VButton textWrapper = new VButton();
165 
166         public VShellShowAllTabLabel(String label) {
167             super(DOM.createElement("li"));
168             addStyleName("show-all");
169             textWrapper.getElement().setInnerHTML(label);
170             textWrapper.getElement().setClassName("tab-title");
171             this.add(textWrapper);
172         }
173 
174         @Override
175         protected void onLoad() {
176             super.onLoad();
177             bindHandlers();
178         }
179 
180         private void bindHandlers() {
181             addDomHandler(new ClickHandler() {
182                 @Override
183                 public void onClick(ClickEvent event) {
184                     onClickGeneric(event.getNativeEvent());
185                 }
186             }, ClickEvent.getType());
187 
188             textWrapper.addClickHandler(new ClickHandler(){
189                 @Override
190                 public void onClick(ClickEvent event) {
191                     textWrapper.setFocus(false);
192                     onClickGeneric(event.getNativeEvent());
193                 }
194             });
195         }
196 
197         private void onClickGeneric(NativeEvent nativeEvent){
198             eventBus.fireEvent(new ShowAllTabsEvent());
199             nativeEvent.stopPropagation();
200         }
201 
202     }
203 
204     public void showAll(boolean showAll) {
205         if (showAllTab != null) {
206             if (showAll) {
207                 showAllTab.addStyleName("active");
208             } else {
209                 if (showAllTab.getStyleName().contains("active")) {
210                     showAllTab.removeStyleName("active");
211                 }
212             }
213         }
214     }
215 
216 }