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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
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.VToolkitOverlay;
/**
* TODO open for extension
*/
public class VTooltip extends VToolkitOverlay {
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 Paintable 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;
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);
}
private void show(TooltipInfo info) {
boolean hasContent = false;
if (info.getErrorUidl() != null) {
em.setVisible(true);
em.updateFromUIDL(info.getErrorUidl());
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) {
setPopupPositionAndShow(new PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
if (offsetWidth > MAX_WIDTH) {
setWidth(MAX_WIDTH + "px");
}
offsetWidth = getOffsetWidth();
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;
}
setPopupPosition(x, y);
sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
}
});
} else {
hide();
}
}
public void showTooltip(Paintable owner, Event event) {
if (closing && tooltipOwner == owner) {
// 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;
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.getTitleInfo(tooltipOwner);
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, Paintable owner) {
final int type = DOM.eventGetType(event);
if ((VTooltip.TOOLTIP_EVENTS & type) == type) {
if (type == Event.ONMOUSEOVER) {
showTooltip(owner, event);
} 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
}
}
}
|