1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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
147
148 public void setEventBus(EventBus eventBus) {
149 this.eventBus = eventBus;
150 }
151 }