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.tab.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.ActiveTabChangedEvent;
37  import info.magnolia.ui.vaadin.gwt.client.tabsheet.event.TabCloseEvent;
38  
39  import com.google.gwt.dom.client.Style.Display;
40  import com.google.gwt.user.client.DOM;
41  import com.google.gwt.user.client.Element;
42  import com.google.gwt.user.client.Event;
43  import com.google.gwt.user.client.ui.SimplePanel;
44  import com.google.web.bindery.event.shared.EventBus;
45  import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
46  import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
47  import com.googlecode.mgwt.ui.client.widget.touch.TouchDelegate;
48  
49  /**
50   * Tab label in the tab bar.
51   */
52  public class MagnoliaTabLabel extends SimplePanel {
53  
54      private Element indicatorsWrapper = DOM.createDiv();
55  
56      private final Element notificationBox = DOM.createDiv();
57  
58      private final Element closeElement = DOM.createSpan();
59  
60      private final Element errorIndicator = DOM.createDiv();
61  
62      private final Element textWrapper = DOM.createSpan();
63  
64      private MagnoliaTabWidget tab;
65  
66      private final TouchDelegate touchDelegate = new TouchDelegate(this);
67  
68      private EventBus eventBus;
69  
70      public MagnoliaTabLabel() {
71          super(DOM.createElement("li"));
72  
73          indicatorsWrapper.addClassName("indicators-wrapper");
74          textWrapper.setClassName("tab-title");
75          getElement().appendChild(textWrapper);
76  
77          // TODO 20120816 mgeljic: implement subtitle
78  
79          indicatorsWrapper = getElement();
80  
81          closeElement.setClassName("v-shell-tab-close");
82          closeElement.addClassName("icon-close");
83          notificationBox.setClassName("v-shell-tab-notification");
84          errorIndicator.setClassName("v-shell-tab-error");
85  
86          getElement().appendChild(closeElement);
87          getElement().appendChild(notificationBox);
88          getElement().appendChild(errorIndicator);
89  
90          DOM.sinkEvents(getElement(), Event.MOUSEEVENTS | Event.TOUCHEVENTS);
91          hideNotification();
92          setHasError(false);
93          // MGNLUI-786: Fixes tab label sizing issue in Chrome.
94          setWidth("100px");
95      }
96  
97      @Override
98      protected void onLoad() {
99          super.onLoad();
100         bindHandlers();
101     }
102 
103     private void bindHandlers() {
104         touchDelegate.addTouchEndHandler(new TouchEndHandler() {
105             @Override
106             public void onTouchEnd(TouchEndEvent event) {
107                 final Element target = (Element) event.getNativeEvent().getEventTarget().cast();
108                 if (closeElement.isOrHasChild(target)) {
109                     eventBus.fireEvent(new TabCloseEvent(tab));
110                 } else {
111                     eventBus.fireEvent(new ActiveTabChangedEvent(tab));
112                 }
113             }
114         });
115     }
116 
117     public void setTab(final MagnoliaTabWidget tab) {
118         this.tab = tab;
119     }
120 
121     public void updateCaption(final String caption) {
122         textWrapper.setInnerText(caption);
123         setWidth("");
124     }
125 
126     public void setClosable(boolean isClosable) {
127         closeElement.getStyle().setDisplay(isClosable ? Display.INLINE : Display.NONE);
128     }
129 
130     public void updateNotification(final String text) {
131         if (text != null && !text.isEmpty()) {
132             notificationBox.getStyle().setDisplay(Display.INLINE_BLOCK);
133             notificationBox.setInnerText(text);
134         }
135     }
136 
137     public void hideNotification() {
138         notificationBox.getStyle().setDisplay(Display.NONE);
139     }
140 
141     public void setHasError(boolean hasError) {
142         errorIndicator.getStyle().setDisplay(hasError ? Display.INLINE_BLOCK : Display.NONE);
143     }
144 
145     /**
146      * @param eventBus
147      */
148     public void setEventBus(EventBus eventBus) {
149         this.eventBus = eventBus;
150     }
151 }