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