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