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