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;
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
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
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
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 }