author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 439 | 3488710b02f8 |
child 2472 | b7aba00cabb6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.awt.X11; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import java.awt.peer.TrayIconPeer; |
|
31 |
import sun.awt.*; |
|
32 |
import java.awt.image.*; |
|
33 |
import java.text.BreakIterator; |
|
34 |
import java.util.logging.Logger; |
|
35 |
import java.util.logging.Level; |
|
36 |
import java.util.concurrent.ArrayBlockingQueue; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.security.PrivilegedAction; |
|
39 |
import java.lang.reflect.InvocationTargetException; |
|
40 |
||
41 |
public class XTrayIconPeer implements TrayIconPeer { |
|
42 |
private static final Logger ctrLog = Logger.getLogger("sun.awt.X11.XTrayIconPeer.centering"); |
|
43 |
||
44 |
TrayIcon target; |
|
45 |
TrayIconEventProxy eventProxy; |
|
46 |
XTrayIconEmbeddedFrame eframe; |
|
47 |
TrayIconCanvas canvas; |
|
48 |
Balloon balloon; |
|
49 |
Tooltip tooltip; |
|
50 |
PopupMenu popup; |
|
51 |
String tooltipString; |
|
52 |
boolean isTrayIconDisplayed; |
|
53 |
long eframeParentID; |
|
54 |
final XEventDispatcher parentXED, eframeXED; |
|
55 |
||
56 |
static final XEventDispatcher dummyXED = new XEventDispatcher() { |
|
57 |
public void dispatchEvent(XEvent ev) {} |
|
58 |
}; |
|
59 |
||
60 |
volatile boolean isDisposed; |
|
61 |
||
62 |
boolean isParentWindowLocated; |
|
63 |
int old_x, old_y; |
|
64 |
int ex_width, ex_height; |
|
65 |
||
66 |
final static int TRAY_ICON_WIDTH = 24; |
|
67 |
final static int TRAY_ICON_HEIGHT = 24; |
|
68 |
||
69 |
XTrayIconPeer(TrayIcon target) |
|
70 |
throws AWTException |
|
71 |
{ |
|
72 |
this.target = target; |
|
73 |
||
74 |
eventProxy = new TrayIconEventProxy(this); |
|
75 |
||
76 |
canvas = new TrayIconCanvas(target, TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT); |
|
77 |
||
78 |
eframe = new XTrayIconEmbeddedFrame(); |
|
79 |
||
80 |
eframe.setSize(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT); |
|
81 |
eframe.add(canvas); |
|
82 |
||
83 |
// Fix for 6317038: as EmbeddedFrame is instance of Frame, it is blocked |
|
84 |
// by modal dialogs, but in the case of TrayIcon it shouldn't. So we |
|
85 |
// set ModalExclusion property on it. |
|
86 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
87 |
public Object run() { |
|
88 |
eframe.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE); |
|
89 |
return null; |
|
90 |
} |
|
91 |
}); |
|
92 |
||
93 |
||
94 |
if (XWM.getWMID() != XWM.METACITY_WM) { |
|
95 |
parentXED = dummyXED; // We don't like to leave it 'null'. |
|
96 |
||
97 |
} else { |
|
98 |
parentXED = new XEventDispatcher() { |
|
99 |
// It's executed under AWTLock. |
|
100 |
public void dispatchEvent(XEvent ev) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
431
diff
changeset
|
101 |
if (isDisposed() || ev.get_type() != XConstants.ConfigureNotify) { |
2 | 102 |
return; |
103 |
} |
|
104 |
||
105 |
XConfigureEvent ce = ev.get_xconfigure(); |
|
106 |
||
107 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}: {1}x{2}+{3}+{4} (old: {5}+{6})", |
|
108 |
new Object[] { XTrayIconPeer.this, ce.get_width(), ce.get_height(), |
|
109 |
ce.get_x(), ce.get_y(), old_x, old_y }); |
|
110 |
||
111 |
// A workaround for Gnome/Metacity (it doesn't affect the behaviour on KDE). |
|
112 |
// On Metacity the EmbeddedFrame's parent window bounds are larger |
|
113 |
// than TrayIcon size required (that is we need a square but a rectangle |
|
114 |
// is provided by the Panel Notification Area). The parent's background color |
|
115 |
// differs from the Panel's one. To hide the background we resize parent |
|
116 |
// window so that it fits the EmbeddedFrame. |
|
117 |
// However due to resizing the parent window it loses centering in the Panel. |
|
118 |
// We center it when discovering that some of its side is of size greater |
|
119 |
// than the fixed value. Centering is being done by "X" (when the parent's width |
|
120 |
// is greater) and by "Y" (when the parent's height is greater). |
|
121 |
||
122 |
// Actually we need this workaround until we could detect taskbar color. |
|
123 |
||
124 |
if (ce.get_height() != TRAY_ICON_HEIGHT && ce.get_width() != TRAY_ICON_WIDTH) { |
|
125 |
||
126 |
// If both the height and the width differ from the fixed size then WM |
|
127 |
// must level at least one side to the fixed size. For some reason it may take |
|
128 |
// a few hops (even after reparenting) and we have to skip the intermediate ones. |
|
129 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Skipping as intermediate resizing.", |
|
130 |
XTrayIconPeer.this); |
|
131 |
return; |
|
132 |
||
133 |
} else if (ce.get_height() > TRAY_ICON_HEIGHT) { |
|
134 |
||
135 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Centering by \"Y\".", |
|
136 |
XTrayIconPeer.this); |
|
137 |
||
138 |
XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), eframeParentID, |
|
139 |
ce.get_x(), |
|
140 |
ce.get_y()+ce.get_height()/2-TRAY_ICON_HEIGHT/2, |
|
141 |
TRAY_ICON_WIDTH, |
|
142 |
TRAY_ICON_HEIGHT); |
|
143 |
ex_height = ce.get_height(); |
|
144 |
ex_width = 0; |
|
145 |
||
146 |
} else if (ce.get_width() > TRAY_ICON_WIDTH) { |
|
147 |
||
148 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Centering by \"X\".", |
|
149 |
XTrayIconPeer.this); |
|
150 |
||
151 |
XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), eframeParentID, |
|
152 |
ce.get_x()+ce.get_width()/2 - TRAY_ICON_WIDTH/2, |
|
153 |
ce.get_y(), |
|
154 |
TRAY_ICON_WIDTH, |
|
155 |
TRAY_ICON_HEIGHT); |
|
156 |
ex_width = ce.get_width(); |
|
157 |
ex_height = 0; |
|
158 |
||
159 |
} else if (isParentWindowLocated && ce.get_x() != old_x && ce.get_y() != old_y) { |
|
160 |
// If moving by both "X" and "Y". |
|
161 |
// When some tray icon gets removed from the tray, a Java icon may be repositioned. |
|
162 |
// In this case the parent window also lose centering. We have to restore it. |
|
163 |
||
164 |
if (ex_height != 0) { |
|
165 |
||
166 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Move detected. Centering by \"Y\".", |
|
167 |
XTrayIconPeer.this); |
|
168 |
||
169 |
XlibWrapper.XMoveWindow(XToolkit.getDisplay(), eframeParentID, |
|
170 |
ce.get_x(), |
|
171 |
ce.get_y() + ex_height/2 - TRAY_ICON_HEIGHT/2); |
|
172 |
||
173 |
} else if (ex_width != 0) { |
|
174 |
||
175 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Move detected. Centering by \"X\".", |
|
176 |
XTrayIconPeer.this); |
|
177 |
||
178 |
XlibWrapper.XMoveWindow(XToolkit.getDisplay(), eframeParentID, |
|
179 |
ce.get_x() + ex_width/2 - TRAY_ICON_WIDTH/2, |
|
180 |
ce.get_y()); |
|
181 |
} else { |
|
182 |
ctrLog.log(Level.FINE, "ConfigureNotify on parent of {0}. Move detected. Skipping.", |
|
183 |
XTrayIconPeer.this); |
|
184 |
} |
|
185 |
} |
|
186 |
old_x = ce.get_x(); |
|
187 |
old_y = ce.get_y(); |
|
188 |
isParentWindowLocated = true; |
|
189 |
} |
|
190 |
}; |
|
191 |
} |
|
192 |
eframeXED = new XEventDispatcher() { |
|
193 |
// It's executed under AWTLock. |
|
194 |
XTrayIconPeer xtiPeer = XTrayIconPeer.this; |
|
195 |
||
196 |
public void dispatchEvent(XEvent ev) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
431
diff
changeset
|
197 |
if (isDisposed() || ev.get_type() != XConstants.ReparentNotify) { |
2 | 198 |
return; |
199 |
} |
|
200 |
||
201 |
XReparentEvent re = ev.get_xreparent(); |
|
202 |
eframeParentID = re.get_parent(); |
|
203 |
||
204 |
if (eframeParentID == XToolkit.getDefaultRootWindow()) { |
|
205 |
||
206 |
if (isTrayIconDisplayed) { // most likely Notification Area was removed |
|
207 |
SunToolkit.executeOnEventHandlerThread(xtiPeer.target, new Runnable() { |
|
208 |
public void run() { |
|
209 |
SystemTray.getSystemTray().remove(xtiPeer.target); |
|
210 |
} |
|
211 |
}); |
|
212 |
} |
|
213 |
return; |
|
214 |
} |
|
215 |
||
216 |
if (!isTrayIconDisplayed) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
431
diff
changeset
|
217 |
addXED(eframeParentID, parentXED, XConstants.StructureNotifyMask); |
2 | 218 |
|
219 |
isTrayIconDisplayed = true; |
|
220 |
XToolkit.awtLockNotifyAll(); |
|
221 |
} |
|
222 |
} |
|
223 |
}; |
|
224 |
||
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
431
diff
changeset
|
225 |
addXED(getWindow(), eframeXED, XConstants.StructureNotifyMask); |
2 | 226 |
|
227 |
XSystemTrayPeer.getPeerInstance().addTrayIcon(this); // throws AWTException |
|
228 |
||
229 |
// Wait till the EmbeddedFrame is reparented |
|
230 |
long start = System.currentTimeMillis(); |
|
231 |
final long PERIOD = 2000L; |
|
232 |
XToolkit.awtLock(); |
|
233 |
try { |
|
234 |
while (!isTrayIconDisplayed) { |
|
235 |
try { |
|
236 |
XToolkit.awtLockWait(PERIOD); |
|
237 |
} catch (InterruptedException e) { |
|
238 |
break; |
|
239 |
} |
|
240 |
if (System.currentTimeMillis() - start > PERIOD) { |
|
241 |
break; |
|
242 |
} |
|
243 |
} |
|
244 |
} finally { |
|
245 |
XToolkit.awtUnlock(); |
|
246 |
} |
|
247 |
||
248 |
// This is unlikely to happen. |
|
249 |
if (!isTrayIconDisplayed || eframeParentID == 0 || |
|
250 |
eframeParentID == XToolkit.getDefaultRootWindow()) |
|
251 |
{ |
|
252 |
throw new AWTException("TrayIcon couldn't be displayed."); |
|
253 |
} |
|
254 |
||
255 |
eframe.setVisible(true); |
|
256 |
updateImage(); |
|
257 |
||
258 |
balloon = new Balloon(this, eframe); |
|
259 |
tooltip = new Tooltip(this, eframe); |
|
260 |
||
261 |
addListeners(); |
|
262 |
} |
|
263 |
||
264 |
public void dispose() { |
|
265 |
if (SunToolkit.isDispatchThreadForAppContext(target)) { |
|
266 |
disposeOnEDT(); |
|
267 |
} else { |
|
268 |
try { |
|
269 |
SunToolkit.executeOnEDTAndWait(target, new Runnable() { |
|
270 |
public void run() { |
|
271 |
disposeOnEDT(); |
|
272 |
} |
|
273 |
}); |
|
274 |
} catch (InterruptedException ie) { |
|
275 |
} catch (InvocationTargetException ite) {} |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
private void disposeOnEDT() { |
|
280 |
// All actions that is to be synchronized with disposal |
|
281 |
// should be executed either under AWTLock, or on EDT. |
|
282 |
// isDisposed value must be checked. |
|
283 |
XToolkit.awtLock(); |
|
284 |
isDisposed = true; |
|
285 |
XToolkit.awtUnlock(); |
|
286 |
||
287 |
removeXED(getWindow(), eframeXED); |
|
288 |
removeXED(eframeParentID, parentXED); |
|
289 |
eframe.realDispose(); |
|
290 |
balloon.dispose(); |
|
291 |
isTrayIconDisplayed = false; |
|
292 |
XToolkit.targetDisposedPeer(target, this); |
|
293 |
} |
|
294 |
||
295 |
public static void suppressWarningString(Window w) { |
|
296 |
WindowAccessor.setTrayIconWindow(w, true); |
|
297 |
} |
|
298 |
||
299 |
public void setToolTip(String tooltip) { |
|
300 |
tooltipString = tooltip; |
|
301 |
} |
|
302 |
||
303 |
public void updateImage() { |
|
304 |
Runnable r = new Runnable() { |
|
305 |
public void run() { |
|
306 |
canvas.updateImage(target.getImage()); |
|
307 |
} |
|
308 |
}; |
|
309 |
||
310 |
if (!SunToolkit.isDispatchThreadForAppContext(target)) { |
|
311 |
SunToolkit.executeOnEventHandlerThread(target, r); |
|
312 |
} else { |
|
313 |
r.run(); |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
public void displayMessage(String caption, String text, String messageType) { |
|
318 |
Point loc = getLocationOnScreen(); |
|
319 |
Rectangle screen = eframe.getGraphicsConfiguration().getBounds(); |
|
320 |
||
321 |
// Check if the tray icon is in the bounds of a screen. |
|
322 |
if (!(loc.x < screen.x || loc.x >= screen.x + screen.width || |
|
323 |
loc.y < screen.y || loc.y >= screen.y + screen.height)) |
|
324 |
{ |
|
325 |
balloon.display(caption, text, messageType); |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
// It's synchronized with disposal by EDT. |
|
330 |
public void showPopupMenu(int x, int y) { |
|
331 |
if (isDisposed()) |
|
332 |
return; |
|
333 |
||
334 |
assert SunToolkit.isDispatchThreadForAppContext(target); |
|
335 |
||
336 |
PopupMenu newPopup = target.getPopupMenu(); |
|
337 |
if (popup != newPopup) { |
|
338 |
if (popup != null) { |
|
339 |
eframe.remove(popup); |
|
340 |
} |
|
341 |
if (newPopup != null) { |
|
342 |
eframe.add(newPopup); |
|
343 |
} |
|
344 |
popup = newPopup; |
|
345 |
} |
|
346 |
||
347 |
if (popup != null) { |
|
348 |
Point loc = ((XBaseWindow)eframe.getPeer()).toLocal(new Point(x, y)); |
|
349 |
popup.show(eframe, loc.x, loc.y); |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
||
354 |
// ****************************************************************** |
|
355 |
// ****************************************************************** |
|
356 |
||
357 |
||
358 |
private void addXED(long window, XEventDispatcher xed, long mask) { |
|
359 |
if (window == 0) { |
|
360 |
return; |
|
361 |
} |
|
362 |
XToolkit.awtLock(); |
|
363 |
try { |
|
364 |
XlibWrapper.XSelectInput(XToolkit.getDisplay(), window, mask); |
|
365 |
} finally { |
|
366 |
XToolkit.awtUnlock(); |
|
367 |
} |
|
368 |
XToolkit.addEventDispatcher(window, xed); |
|
369 |
} |
|
370 |
||
371 |
private void removeXED(long window, XEventDispatcher xed) { |
|
372 |
if (window == 0) { |
|
373 |
return; |
|
374 |
} |
|
375 |
XToolkit.awtLock(); |
|
376 |
try { |
|
377 |
XToolkit.removeEventDispatcher(window, xed); |
|
378 |
} finally { |
|
379 |
XToolkit.awtUnlock(); |
|
380 |
} |
|
381 |
} |
|
382 |
||
383 |
// Private method for testing purposes. |
|
384 |
private Point getLocationOnScreen() { |
|
385 |
return eframe.getLocationOnScreen(); |
|
386 |
} |
|
387 |
||
388 |
private Rectangle getBounds() { |
|
389 |
Point loc = getLocationOnScreen(); |
|
390 |
return new Rectangle(loc.x, loc.y, loc.x + TRAY_ICON_WIDTH, loc.y + TRAY_ICON_HEIGHT); |
|
391 |
} |
|
392 |
||
393 |
void addListeners() { |
|
394 |
canvas.addMouseListener(eventProxy); |
|
395 |
canvas.addMouseMotionListener(eventProxy); |
|
396 |
} |
|
397 |
||
398 |
long getWindow() { |
|
399 |
return ((XEmbeddedFramePeer)eframe.getPeer()).getWindow(); |
|
400 |
} |
|
401 |
||
402 |
boolean isDisposed() { |
|
403 |
return isDisposed; |
|
404 |
} |
|
405 |
||
406 |
static class TrayIconEventProxy implements MouseListener, MouseMotionListener { |
|
407 |
XTrayIconPeer xtiPeer; |
|
408 |
||
409 |
TrayIconEventProxy(XTrayIconPeer xtiPeer) { |
|
410 |
this.xtiPeer = xtiPeer; |
|
411 |
} |
|
412 |
||
413 |
public void handleEvent(MouseEvent e) { |
|
414 |
//prevent DRAG events from being posted with TrayIcon source(CR 6565779) |
|
415 |
if (e.getID() == MouseEvent.MOUSE_DRAGGED) { |
|
416 |
return; |
|
417 |
} |
|
418 |
||
419 |
// Event handling is synchronized with disposal by EDT. |
|
420 |
if (xtiPeer.isDisposed()) { |
|
421 |
return; |
|
422 |
} |
|
423 |
Point coord = XBaseWindow.toOtherWindow(xtiPeer.getWindow(), |
|
424 |
XToolkit.getDefaultRootWindow(), |
|
425 |
e.getX(), e.getY()); |
|
426 |
||
427 |
if (e.isPopupTrigger()) { |
|
428 |
xtiPeer.showPopupMenu(coord.x, coord.y); |
|
429 |
} |
|
430 |
||
431 |
e.translatePoint(coord.x - e.getX(), coord.y - e.getY()); |
|
432 |
// This is a hack in order to set non-Component source to MouseEvent |
|
433 |
// instance. |
|
434 |
// In some cases this could lead to unpredictable result (e.g. when |
|
435 |
// other class tries to cast source field to Component). |
|
436 |
// We already filter DRAG events out (CR 6565779). |
|
437 |
e.setSource(xtiPeer.target); |
|
438 |
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e); |
|
439 |
} |
|
440 |
public void mouseClicked(MouseEvent e) { |
|
441 |
if ((e.getClickCount() > 1 || xtiPeer.balloon.isVisible()) && |
|
442 |
e.getButton() == MouseEvent.BUTTON1) |
|
443 |
{ |
|
444 |
ActionEvent aev = new ActionEvent(xtiPeer.target, ActionEvent.ACTION_PERFORMED, |
|
445 |
xtiPeer.target.getActionCommand(), e.getWhen(), |
|
446 |
e.getModifiers()); |
|
447 |
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(aev); |
|
448 |
} |
|
449 |
if (xtiPeer.balloon.isVisible()) { |
|
450 |
xtiPeer.balloon.hide(); |
|
451 |
} |
|
452 |
handleEvent(e); |
|
453 |
} |
|
454 |
public void mouseEntered(MouseEvent e) { |
|
455 |
xtiPeer.tooltip.enter(); |
|
456 |
handleEvent(e); |
|
457 |
} |
|
458 |
public void mouseExited(MouseEvent e) { |
|
459 |
xtiPeer.tooltip.exit(); |
|
460 |
handleEvent(e); |
|
461 |
} |
|
462 |
public void mousePressed(MouseEvent e) { |
|
463 |
handleEvent(e); |
|
464 |
} |
|
465 |
public void mouseReleased(MouseEvent e) { |
|
466 |
handleEvent(e); |
|
467 |
} |
|
468 |
public void mouseDragged(MouseEvent e) { |
|
469 |
handleEvent(e); |
|
470 |
} |
|
471 |
public void mouseMoved(MouseEvent e) { |
|
472 |
handleEvent(e); |
|
473 |
} |
|
474 |
} |
|
475 |
||
476 |
static boolean isTrayIconStuffWindow(Window w) { |
|
477 |
return (w instanceof Tooltip) || |
|
478 |
(w instanceof Balloon) || |
|
479 |
(w instanceof XTrayIconEmbeddedFrame); |
|
480 |
} |
|
481 |
||
482 |
// *************************************** |
|
483 |
// Special embedded frame for tray icon |
|
484 |
// *************************************** |
|
485 |
||
486 |
private static class XTrayIconEmbeddedFrame extends XEmbeddedFrame { |
|
487 |
public XTrayIconEmbeddedFrame(){ |
|
488 |
super(XToolkit.getDefaultRootWindow(), true, true); |
|
489 |
} |
|
490 |
||
491 |
public boolean isUndecorated() { |
|
492 |
return true; |
|
493 |
} |
|
494 |
||
495 |
public boolean isResizable() { |
|
496 |
return false; |
|
497 |
} |
|
498 |
||
499 |
// embedded frame for tray icon shouldn't be disposed by anyone except tray icon |
|
500 |
public void dispose(){ |
|
501 |
} |
|
502 |
||
503 |
public void realDispose(){ |
|
504 |
super.dispose(); |
|
505 |
} |
|
506 |
}; |
|
507 |
||
508 |
// *************************************** |
|
509 |
// Classes for painting an image on canvas |
|
510 |
// *************************************** |
|
511 |
||
512 |
static class TrayIconCanvas extends IconCanvas { |
|
513 |
TrayIcon target; |
|
514 |
boolean autosize; |
|
515 |
||
516 |
TrayIconCanvas(TrayIcon target, int width, int height) { |
|
517 |
super(width, height); |
|
518 |
this.target = target; |
|
519 |
} |
|
520 |
||
521 |
// Invoke on EDT. |
|
522 |
protected void repaintImage(boolean doClear) { |
|
523 |
boolean old_autosize = autosize; |
|
524 |
autosize = target.isImageAutoSize(); |
|
525 |
||
526 |
curW = autosize ? width : image.getWidth(observer); |
|
527 |
curH = autosize ? height : image.getHeight(observer); |
|
528 |
||
529 |
super.repaintImage(doClear || (old_autosize != autosize)); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
static class IconCanvas extends Canvas { |
|
534 |
volatile Image image; |
|
535 |
IconObserver observer; |
|
536 |
int width, height; |
|
537 |
int curW, curH; |
|
538 |
||
539 |
IconCanvas(int width, int height) { |
|
540 |
this.width = curW = width; |
|
541 |
this.height = curH = height; |
|
542 |
} |
|
543 |
||
544 |
// Invoke on EDT. |
|
545 |
public void updateImage(Image image) { |
|
546 |
this.image = image; |
|
547 |
if (observer == null) { |
|
548 |
observer = new IconObserver(); |
|
549 |
} |
|
550 |
repaintImage(true); |
|
551 |
} |
|
552 |
||
553 |
// Invoke on EDT. |
|
554 |
protected void repaintImage(boolean doClear) { |
|
555 |
Graphics g = getGraphics(); |
|
556 |
if (g != null) { |
|
557 |
try { |
|
558 |
if (isVisible()) { |
|
559 |
if (doClear) { |
|
560 |
update(g); |
|
561 |
} else { |
|
562 |
paint(g); |
|
563 |
} |
|
564 |
} |
|
565 |
} finally { |
|
566 |
g.dispose(); |
|
567 |
} |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
// Invoke on EDT. |
|
572 |
public void paint(Graphics g) { |
|
573 |
if (g != null && curW > 0 && curH > 0) { |
|
574 |
BufferedImage bufImage = new BufferedImage(curW, curH, BufferedImage.TYPE_INT_ARGB); |
|
575 |
Graphics2D gr = bufImage.createGraphics(); |
|
576 |
if (gr != null) { |
|
577 |
try { |
|
578 |
gr.setColor(getBackground()); |
|
579 |
gr.fillRect(0, 0, curW, curH); |
|
580 |
gr.drawImage(image, 0, 0, curW, curH, observer); |
|
581 |
gr.dispose(); |
|
582 |
||
583 |
g.drawImage(bufImage, 0, 0, curW, curH, null); |
|
584 |
} finally { |
|
585 |
gr.dispose(); |
|
586 |
} |
|
587 |
} |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
class IconObserver implements ImageObserver { |
|
592 |
public boolean imageUpdate(final Image image, final int flags, int x, int y, int width, int height) { |
|
593 |
if (image != IconCanvas.this.image || // if the image has been changed |
|
594 |
!IconCanvas.this.isVisible()) |
|
595 |
{ |
|
596 |
return false; |
|
597 |
} |
|
598 |
if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS | |
|
599 |
ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0) |
|
600 |
{ |
|
601 |
SunToolkit.executeOnEventHandlerThread(IconCanvas.this, new Runnable() { |
|
602 |
public void run() { |
|
603 |
repaintImage(false); |
|
604 |
} |
|
605 |
}); |
|
606 |
} |
|
607 |
return (flags & ImageObserver.ALLBITS) == 0; |
|
608 |
} |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
// *************************************** |
|
613 |
// Classes for toolitp and balloon windows |
|
614 |
// *************************************** |
|
615 |
||
616 |
static class Tooltip extends InfoWindow { |
|
617 |
XTrayIconPeer xtiPeer; |
|
618 |
Label textLabel = new Label(""); |
|
619 |
Runnable starter = new Runnable() { |
|
620 |
public void run() { |
|
621 |
display(); |
|
622 |
}}; |
|
623 |
||
624 |
final static int TOOLTIP_SHOW_TIME = 10000; |
|
625 |
final static int TOOLTIP_START_DELAY_TIME = 1000; |
|
626 |
final static int TOOLTIP_MAX_LENGTH = 64; |
|
627 |
final static int TOOLTIP_MOUSE_CURSOR_INDENT = 5; |
|
628 |
final static Color TOOLTIP_BACKGROUND_COLOR = new Color(255, 255, 220); |
|
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
629 |
final static Font TOOLTIP_TEXT_FONT = XWindow.getDefaultFont(); |
2 | 630 |
|
631 |
Tooltip(XTrayIconPeer xtiPeer, Frame parent) { |
|
632 |
super(parent, Color.black); |
|
633 |
this.xtiPeer = xtiPeer; |
|
634 |
||
635 |
suppressWarningString(this); |
|
636 |
||
637 |
setCloser(null, TOOLTIP_SHOW_TIME); |
|
638 |
textLabel.setBackground(TOOLTIP_BACKGROUND_COLOR); |
|
639 |
textLabel.setFont(TOOLTIP_TEXT_FONT); |
|
640 |
add(textLabel); |
|
641 |
} |
|
642 |
||
643 |
/* |
|
644 |
* WARNING: this method is executed on Toolkit thread! |
|
645 |
*/ |
|
646 |
void display() { |
|
647 |
String tip = xtiPeer.tooltipString; |
|
648 |
if (tip == null) { |
|
649 |
return; |
|
650 |
} else if (tip.length() > TOOLTIP_MAX_LENGTH) { |
|
651 |
textLabel.setText(tip.substring(0, TOOLTIP_MAX_LENGTH)); |
|
652 |
} else { |
|
653 |
textLabel.setText(tip); |
|
654 |
} |
|
655 |
||
656 |
// Execute on EDT to avoid deadlock (see 6280857). |
|
657 |
SunToolkit.executeOnEventHandlerThread(xtiPeer.target, new Runnable() { |
|
658 |
public void run() { |
|
659 |
if (xtiPeer.isDisposed()) { |
|
660 |
return; |
|
661 |
} |
|
662 |
Point pointer = (Point)AccessController.doPrivileged(new PrivilegedAction() { |
|
663 |
public Object run() { |
|
664 |
if (!isPointerOverTrayIcon(xtiPeer.getBounds())) { |
|
665 |
return null; |
|
666 |
} |
|
667 |
return MouseInfo.getPointerInfo().getLocation(); |
|
668 |
} |
|
669 |
}); |
|
670 |
if (pointer == null) { |
|
671 |
return; |
|
672 |
} |
|
673 |
show(new Point(pointer.x, pointer.y), TOOLTIP_MOUSE_CURSOR_INDENT); |
|
674 |
} |
|
675 |
}); |
|
676 |
} |
|
677 |
||
678 |
void enter() { |
|
679 |
XToolkit.schedule(starter, TOOLTIP_START_DELAY_TIME); |
|
680 |
} |
|
681 |
||
682 |
void exit() { |
|
683 |
XToolkit.remove(starter); |
|
684 |
if (isVisible()) { |
|
685 |
hide(); |
|
686 |
} |
|
687 |
} |
|
688 |
||
689 |
boolean isPointerOverTrayIcon(Rectangle trayRect) { |
|
690 |
Point p = MouseInfo.getPointerInfo().getLocation(); |
|
691 |
return !(p.x < trayRect.x || p.x > (trayRect.x + trayRect.width) || |
|
692 |
p.y < trayRect.y || p.y > (trayRect.y + trayRect.height)); |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
static class Balloon extends InfoWindow { |
|
697 |
final static int BALLOON_SHOW_TIME = 10000; |
|
698 |
final static int BALLOON_TEXT_MAX_LENGTH = 256; |
|
699 |
final static int BALLOON_WORD_LINE_MAX_LENGTH = 16; |
|
700 |
final static int BALLOON_WORD_LINE_MAX_COUNT = 4; |
|
701 |
final static int BALLOON_ICON_WIDTH = 32; |
|
702 |
final static int BALLOON_ICON_HEIGHT = 32; |
|
703 |
final static int BALLOON_TRAY_ICON_INDENT = 0; |
|
704 |
final static Color BALLOON_CAPTION_BACKGROUND_COLOR = new Color(200, 200 ,255); |
|
705 |
final static Font BALLOON_CAPTION_FONT = new Font(Font.DIALOG, Font.BOLD, 12); |
|
706 |
||
707 |
XTrayIconPeer xtiPeer; |
|
708 |
Panel mainPanel = new Panel(); |
|
709 |
Panel captionPanel = new Panel(); |
|
710 |
Label captionLabel = new Label(""); |
|
711 |
Button closeButton = new Button("X"); |
|
712 |
Panel textPanel = new Panel(); |
|
713 |
IconCanvas iconCanvas = new IconCanvas(BALLOON_ICON_WIDTH, BALLOON_ICON_HEIGHT); |
|
714 |
Label[] lineLabels = new Label[BALLOON_WORD_LINE_MAX_COUNT]; |
|
715 |
ActionPerformer ap = new ActionPerformer(); |
|
716 |
||
717 |
Image iconImage; |
|
718 |
Image errorImage; |
|
719 |
Image warnImage; |
|
720 |
Image infoImage; |
|
721 |
boolean gtkImagesLoaded; |
|
722 |
||
723 |
Displayer displayer = new Displayer(); |
|
724 |
||
725 |
Balloon(final XTrayIconPeer xtiPeer, Frame parent) { |
|
726 |
super(parent, new Color(90, 80 ,190)); |
|
727 |
this.xtiPeer = xtiPeer; |
|
728 |
||
729 |
suppressWarningString(this); |
|
730 |
||
731 |
setCloser(new Runnable() { |
|
732 |
public void run() { |
|
733 |
if (textPanel != null) { |
|
734 |
textPanel.removeAll(); |
|
735 |
textPanel.setSize(0, 0); |
|
736 |
iconCanvas.setSize(0, 0); |
|
737 |
XToolkit.awtLock(); |
|
738 |
try { |
|
739 |
displayer.isDisplayed = false; |
|
740 |
XToolkit.awtLockNotifyAll(); |
|
741 |
} finally { |
|
742 |
XToolkit.awtUnlock(); |
|
743 |
} |
|
744 |
} |
|
745 |
} |
|
746 |
}, BALLOON_SHOW_TIME); |
|
747 |
||
748 |
add(mainPanel); |
|
749 |
||
750 |
captionLabel.setFont(BALLOON_CAPTION_FONT); |
|
751 |
captionLabel.addMouseListener(ap); |
|
752 |
||
753 |
captionPanel.setLayout(new BorderLayout()); |
|
754 |
captionPanel.add(captionLabel, BorderLayout.WEST); |
|
755 |
captionPanel.add(closeButton, BorderLayout.EAST); |
|
756 |
captionPanel.setBackground(BALLOON_CAPTION_BACKGROUND_COLOR); |
|
757 |
captionPanel.addMouseListener(ap); |
|
758 |
||
759 |
closeButton.addActionListener(new ActionListener() { |
|
760 |
public void actionPerformed(ActionEvent e) { |
|
761 |
hide(); |
|
762 |
} |
|
763 |
}); |
|
764 |
||
765 |
mainPanel.setLayout(new BorderLayout()); |
|
766 |
mainPanel.setBackground(Color.white); |
|
767 |
mainPanel.add(captionPanel, BorderLayout.NORTH); |
|
768 |
mainPanel.add(iconCanvas, BorderLayout.WEST); |
|
769 |
mainPanel.add(textPanel, BorderLayout.CENTER); |
|
770 |
||
771 |
iconCanvas.addMouseListener(ap); |
|
772 |
||
773 |
for (int i = 0; i < BALLOON_WORD_LINE_MAX_COUNT; i++) { |
|
774 |
lineLabels[i] = new Label(); |
|
775 |
lineLabels[i].addMouseListener(ap); |
|
776 |
lineLabels[i].setBackground(Color.white); |
|
777 |
} |
|
778 |
||
779 |
displayer.start(); |
|
780 |
} |
|
781 |
||
782 |
void display(String caption, String text, String messageType) { |
|
783 |
if (!gtkImagesLoaded) { |
|
784 |
loadGtkImages(); |
|
785 |
} |
|
786 |
displayer.display(caption, text, messageType); |
|
787 |
} |
|
788 |
||
789 |
private void _display(String caption, String text, String messageType) { |
|
790 |
captionLabel.setText(caption); |
|
791 |
||
792 |
BreakIterator iter = BreakIterator.getWordInstance(); |
|
793 |
if (text != null) { |
|
794 |
iter.setText(text); |
|
795 |
int start = iter.first(), end; |
|
796 |
int nLines = 0; |
|
797 |
||
798 |
do { |
|
799 |
end = iter.next(); |
|
800 |
||
801 |
if (end == BreakIterator.DONE || |
|
802 |
text.substring(start, end).length() >= 50) |
|
803 |
{ |
|
804 |
lineLabels[nLines].setText(text.substring(start, end == BreakIterator.DONE ? |
|
805 |
iter.last() : end)); |
|
806 |
textPanel.add(lineLabels[nLines++]); |
|
807 |
start = end; |
|
808 |
} |
|
809 |
if (nLines == BALLOON_WORD_LINE_MAX_COUNT) { |
|
810 |
if (end != BreakIterator.DONE) { |
|
811 |
lineLabels[nLines - 1].setText( |
|
812 |
new String(lineLabels[nLines - 1].getText() + " ...")); |
|
813 |
} |
|
814 |
break; |
|
815 |
} |
|
816 |
} while (end != BreakIterator.DONE); |
|
817 |
||
818 |
||
819 |
textPanel.setLayout(new GridLayout(nLines, 1)); |
|
820 |
} |
|
821 |
||
822 |
if ("ERROR".equals(messageType)) { |
|
823 |
iconImage = errorImage; |
|
824 |
} else if ("WARNING".equals(messageType)) { |
|
825 |
iconImage = warnImage; |
|
826 |
} else if ("INFO".equals(messageType)) { |
|
827 |
iconImage = infoImage; |
|
828 |
} else { |
|
829 |
iconImage = null; |
|
830 |
} |
|
831 |
||
832 |
if (iconImage != null) { |
|
833 |
Dimension tpSize = textPanel.getSize(); |
|
834 |
iconCanvas.setSize(BALLOON_ICON_WIDTH, (BALLOON_ICON_HEIGHT > tpSize.height ? |
|
835 |
BALLOON_ICON_HEIGHT : tpSize.height)); |
|
431
6aea7c26a8dc
6637204: TrayIcon.displayMessage fails to show icon twice
dcherepanov
parents:
116
diff
changeset
|
836 |
iconCanvas.validate(); |
2 | 837 |
} |
838 |
||
839 |
SunToolkit.executeOnEventHandlerThread(xtiPeer.target, new Runnable() { |
|
840 |
public void run() { |
|
841 |
if (xtiPeer.isDisposed()) { |
|
842 |
return; |
|
843 |
} |
|
844 |
Point parLoc = getParent().getLocationOnScreen(); |
|
845 |
Dimension parSize = getParent().getSize(); |
|
846 |
show(new Point(parLoc.x + parSize.width/2, parLoc.y + parSize.height/2), |
|
847 |
BALLOON_TRAY_ICON_INDENT); |
|
848 |
if (iconImage != null) { |
|
849 |
iconCanvas.updateImage(iconImage); // call it after the show(..) above |
|
850 |
} |
|
851 |
} |
|
852 |
}); |
|
853 |
} |
|
854 |
||
855 |
public void dispose() { |
|
856 |
displayer.interrupt(); |
|
857 |
super.dispose(); |
|
858 |
} |
|
859 |
||
860 |
void loadGtkImages() { |
|
861 |
if (!gtkImagesLoaded) { |
|
862 |
errorImage = (Image)Toolkit.getDefaultToolkit().getDesktopProperty( |
|
863 |
"gtk.icon.gtk-dialog-error.6.rtl"); |
|
864 |
warnImage = (Image)Toolkit.getDefaultToolkit().getDesktopProperty( |
|
865 |
"gtk.icon.gtk-dialog-warning.6.rtl"); |
|
866 |
infoImage = (Image)Toolkit.getDefaultToolkit().getDesktopProperty( |
|
867 |
"gtk.icon.gtk-dialog-info.6.rtl"); |
|
868 |
gtkImagesLoaded = true; |
|
869 |
} |
|
870 |
} |
|
871 |
||
872 |
class ActionPerformer extends MouseAdapter { |
|
873 |
public void mouseClicked(MouseEvent e) { |
|
874 |
// hide the balloon by any click |
|
875 |
hide(); |
|
876 |
if (e.getButton() == MouseEvent.BUTTON1) { |
|
877 |
ActionEvent aev = new ActionEvent(xtiPeer.target, ActionEvent.ACTION_PERFORMED, |
|
878 |
xtiPeer.target.getActionCommand(), |
|
879 |
e.getWhen(), e.getModifiers()); |
|
880 |
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(aev); |
|
881 |
} |
|
882 |
} |
|
883 |
} |
|
884 |
||
885 |
class Displayer extends Thread { |
|
886 |
final int MAX_CONCURRENT_MSGS = 10; |
|
887 |
||
888 |
ArrayBlockingQueue<Message> messageQueue = new ArrayBlockingQueue<Message>(MAX_CONCURRENT_MSGS); |
|
889 |
boolean isDisplayed; |
|
890 |
||
891 |
Displayer() { |
|
892 |
setDaemon(true); |
|
893 |
} |
|
894 |
||
895 |
public void run() { |
|
896 |
while (true) { |
|
897 |
Message msg = null; |
|
898 |
try { |
|
899 |
msg = (Message)messageQueue.take(); |
|
900 |
} catch (InterruptedException e) { |
|
901 |
return; |
|
902 |
} |
|
903 |
||
904 |
/* |
|
905 |
* Wait till the previous message is displayed if any |
|
906 |
*/ |
|
907 |
XToolkit.awtLock(); |
|
908 |
try { |
|
909 |
while (isDisplayed) { |
|
910 |
try { |
|
911 |
XToolkit.awtLockWait(); |
|
912 |
} catch (InterruptedException e) { |
|
913 |
return; |
|
914 |
} |
|
915 |
} |
|
916 |
isDisplayed = true; |
|
917 |
} finally { |
|
918 |
XToolkit.awtUnlock(); |
|
919 |
} |
|
920 |
_display(msg.caption, msg.text, msg.messageType); |
|
921 |
} |
|
922 |
} |
|
923 |
||
924 |
void display(String caption, String text, String messageType) { |
|
925 |
messageQueue.offer(new Message(caption, text, messageType)); |
|
926 |
} |
|
927 |
} |
|
928 |
||
929 |
class Message { |
|
930 |
String caption, text, messageType; |
|
931 |
||
932 |
Message(String caption, String text, String messageType) { |
|
933 |
this.caption = caption; |
|
934 |
this.text = text; |
|
935 |
this.messageType = messageType; |
|
936 |
} |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
static class InfoWindow extends Window { |
|
941 |
Container container; |
|
942 |
Closer closer; |
|
943 |
||
944 |
InfoWindow(Frame parent, Color borderColor) { |
|
945 |
super(parent); |
|
946 |
container = new Container() { |
|
947 |
public Insets getInsets() { |
|
948 |
return new Insets(1, 1, 1, 1); |
|
949 |
} |
|
950 |
}; |
|
951 |
setLayout(new BorderLayout()); |
|
952 |
setBackground(borderColor); |
|
953 |
add(container, BorderLayout.CENTER); |
|
954 |
container.setLayout(new BorderLayout()); |
|
955 |
||
956 |
closer = new Closer(); |
|
957 |
} |
|
958 |
||
959 |
public Component add(Component c) { |
|
960 |
container.add(c, BorderLayout.CENTER); |
|
961 |
return c; |
|
962 |
} |
|
963 |
||
964 |
void setCloser(Runnable action, int time) { |
|
965 |
closer.set(action, time); |
|
966 |
} |
|
967 |
||
968 |
// Must be executed on EDT. |
|
969 |
protected void show(Point corner, int indent) { |
|
970 |
assert SunToolkit.isDispatchThreadForAppContext(InfoWindow.this); |
|
971 |
||
972 |
pack(); |
|
973 |
||
974 |
Dimension size = getSize(); |
|
975 |
// TODO: When 6356322 is fixed we should get screen bounds in |
|
976 |
// this way: eframe.getGraphicsConfiguration().getBounds(). |
|
977 |
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|
978 |
||
979 |
if (corner.x < scrSize.width/2 && corner.y < scrSize.height/2) { // 1st square |
|
980 |
setLocation(corner.x + indent, corner.y + indent); |
|
981 |
||
982 |
} else if (corner.x >= scrSize.width/2 && corner.y < scrSize.height/2) { // 2nd square |
|
983 |
setLocation(corner.x - indent - size.width, corner.y + indent); |
|
984 |
||
985 |
} else if (corner.x < scrSize.width/2 && corner.y >= scrSize.height/2) { // 3rd square |
|
986 |
setLocation(corner.x + indent, corner.y - indent - size.height); |
|
987 |
||
988 |
} else if (corner.x >= scrSize.width/2 && corner.y >= scrSize.height/2) { // 4th square |
|
989 |
setLocation(corner.x - indent - size.width, corner.y - indent - size.height); |
|
990 |
} |
|
991 |
||
992 |
InfoWindow.super.show(); |
|
993 |
InfoWindow.this.closer.schedule(); |
|
994 |
} |
|
995 |
||
996 |
public void hide() { |
|
997 |
closer.close(); |
|
998 |
} |
|
999 |
||
1000 |
class Closer implements Runnable { |
|
1001 |
Runnable action; |
|
1002 |
int time; |
|
1003 |
||
1004 |
public void run() { |
|
1005 |
doClose(); |
|
1006 |
} |
|
1007 |
||
1008 |
void set(Runnable action, int time) { |
|
1009 |
this.action = action; |
|
1010 |
this.time = time; |
|
1011 |
} |
|
1012 |
||
1013 |
void schedule() { |
|
1014 |
XToolkit.schedule(this, time); |
|
1015 |
} |
|
1016 |
||
1017 |
void close() { |
|
1018 |
XToolkit.remove(this); |
|
1019 |
doClose(); |
|
1020 |
} |
|
1021 |
||
1022 |
// WARNING: this method may be executed on Toolkit thread. |
|
1023 |
private void doClose() { |
|
1024 |
SunToolkit.executeOnEventHandlerThread(InfoWindow.this, new Runnable() { |
|
1025 |
public void run() { |
|
1026 |
InfoWindow.super.hide(); |
|
1027 |
invalidate(); |
|
1028 |
if (action != null) { |
|
1029 |
action.run(); |
|
1030 |
} |
|
1031 |
} |
|
1032 |
}); |
|
1033 |
} |
|
1034 |
} |
|
1035 |
} |
|
1036 |
} |