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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.vaadin.terminal.gwt.client.ui.VOverlay;
/**
* TODO open for extension
*/
public class VTooltip extends VOverlay {
private static final String CLASSNAME = "v-tooltip";
private static final int MARGIN = 4;
public static final int TOOLTIP_EVENTS = Event.ONKEYDOWN
| Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONMOUSEMOVE
| Event.ONCLICK;
protected static final int MAX_WIDTH = 500;
private static final int QUICK_OPEN_TIMEOUT = 1000;
private static final int CLOSE_TIMEOUT = 300;
private static final int OPEN_DELAY = 750;
private static final int QUICK_OPEN_DELAY = 100;
VErrorMessage em = new VErrorMessage();
Element description = DOM.createDiv();
private ComponentConnector tooltipOwner;
private boolean closing = false;
private boolean opening = false;
private ApplicationConnection ac;
// Open next tooltip faster. Disabled after 2 sec of showTooltip-silence.
private boolean justClosed = false;
// If this is "additional" tooltip, this field contains the key for it
private Object tooltipKey;
public VTooltip(ApplicationConnection client) {
super(false, false, true);
ac = client;
setStyleName(CLASSNAME);
FlowPanel layout = new FlowPanel();
setWidget(layout);
layout.add(em);
DOM.setElementProperty(description, "className", CLASSNAME + "-text");
DOM.appendChild(layout.getElement(), description);
setSinkShadowEvents(true);
}
/**
* Show a popup containing the information in the "info" tooltip
*
* @param info
*/
private void show(TooltipInfo info) {
boolean hasContent = false;
if (info.getErrorMessage() != null) {
em.setVisible(true);
em.updateMessage(info.getErrorMessage());
hasContent = true;
} else {
em.setVisible(false);
}
if (info.getTitle() != null && !"".equals(info.getTitle())) {
DOM.setInnerHTML(description, info.getTitle());
DOM.setStyleAttribute(description, "display", "");
hasContent = true;
} else {
DOM.setInnerHTML(description, "");
DOM.setStyleAttribute(description, "display", "none");
}
if (hasContent) {
// Issue #8454: With IE7 the tooltips size is calculated based on
// the last tooltip's position, causing problems if the last one was
// in the right or bottom edge. For this reason the tooltip is moved
// first to 0,0 position so that the calculation goes correctly.
setPopupPosition(0, 0);
setPopupPositionAndShow(new PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
if (offsetWidth > MAX_WIDTH) {
setWidth(MAX_WIDTH + "px");
// Check new height and width with reflowed content
offsetWidth = getOffsetWidth();
offsetHeight = getOffsetHeight();
}
int x = tooltipEventMouseX + 10 + Window.getScrollLeft();
int y = tooltipEventMouseY + 10 + Window.getScrollTop();
if (x + offsetWidth + MARGIN - Window.getScrollLeft() > Window
.getClientWidth()) {
x = Window.getClientWidth() - offsetWidth - MARGIN;
}
if (y + offsetHeight + MARGIN - Window.getScrollTop() > Window
.getClientHeight()) {
y = tooltipEventMouseY - 5 - offsetHeight;
if (y - Window.getScrollTop() < 0) {
// tooltip does not fit on top of the mouse either,
// put it at the top of the screen
y = Window.getScrollTop();
}
}
setPopupPosition(x, y);
sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
}
});
} else {
hide();
}
}
public void showTooltip(ComponentConnector owner, Event event, Object key) {
if (closing && tooltipOwner == owner && tooltipKey == key) {
// return to same tooltip, cancel closing
closeTimer.cancel();
closing = false;
justClosedTimer.cancel();
justClosed = false;
return;
}
if (closing) {
closeNow();
}
updatePosition(event);
if (opening) {
showTimer.cancel();
}
tooltipOwner = owner;
tooltipKey = key;
// Schedule timer for showing the tooltip according to if it was
// recently closed or not.
if (justClosed) {
showTimer.schedule(QUICK_OPEN_DELAY);
} else {
showTimer.schedule(OPEN_DELAY);
}
opening = true;
}
private void closeNow() {
if (closing) {
hide();
tooltipOwner = null;
setWidth("");
closing = false;
}
}
private Timer showTimer = new Timer() {
@Override
public void run() {
TooltipInfo info = ac.getTooltipTitleInfo(tooltipOwner, tooltipKey);
if (null != info) {
show(info);
}
opening = false;
}
};
private Timer closeTimer = new Timer() {
@Override
public void run() {
closeNow();
justClosedTimer.schedule(2000);
justClosed = true;
}
};
private Timer justClosedTimer = new Timer() {
@Override
public void run() {
justClosed = false;
}
};
public void hideTooltip() {
if (opening) {
showTimer.cancel();
opening = false;
tooltipOwner = null;
}
if (!isAttached()) {
return;
}
if (closing) {
// already about to close
return;
}
closeTimer.schedule(CLOSE_TIMEOUT);
closing = true;
justClosed = true;
justClosedTimer.schedule(QUICK_OPEN_TIMEOUT);
}
private int tooltipEventMouseX;
private int tooltipEventMouseY;
public void updatePosition(Event event) {
tooltipEventMouseX = DOM.eventGetClientX(event);
tooltipEventMouseY = DOM.eventGetClientY(event);
}
public void handleTooltipEvent(Event event, ComponentConnector owner,
Object key) {
final int type = DOM.eventGetType(event);
if ((VTooltip.TOOLTIP_EVENTS & type) == type) {
if (type == Event.ONMOUSEOVER) {
showTooltip(owner, event, key);
} else if (type == Event.ONMOUSEMOVE) {
updatePosition(event);
} else {
hideTooltip();
}
} else {
// non-tooltip event, hide tooltip
hideTooltip();
}
}
@Override
public void onBrowserEvent(Event event) {
final int type = DOM.eventGetType(event);
// cancel closing event if tooltip is mouseovered; the user might want
// to scroll of cut&paste
switch (type) {
case Event.ONMOUSEOVER:
closeTimer.cancel();
closing = false;
break;
case Event.ONMOUSEOUT:
hideTooltip();
break;
default:
// NOP
}
}
}
|