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.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
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
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
173
174 public void setEventBus(EventBus eventBus) {
175 this.eventBus = eventBus;
176 }
177 }