View Javadoc

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