1 /*
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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.concurrent.ArrayBlockingQueue;
35 import java.security.AccessController;
36 import java.security.PrivilegedAction;
37 import java.lang.reflect.InvocationTargetException;
38 import sun.util.logging.PlatformLogger;
39
40 public class XTrayIconPeer implements TrayIconPeer,
41 InfoWindow.Balloon.LiveArguments,
42 InfoWindow.Tooltip.LiveArguments
43 {
44 private static final PlatformLogger ctrLog = PlatformLogger.getLogger("sun.awt.X11.XTrayIconPeer.centering");
45
46 TrayIcon target;
47 TrayIconEventProxy eventProxy;
48 XTrayIconEmbeddedFrame eframe;
49 TrayIconCanvas canvas;
50 InfoWindow.Balloon balloon;
51 InfoWindow.Tooltip tooltip;
52 PopupMenu popup;
53 String tooltipString;
54 boolean isTrayIconDisplayed;
330 if (!SunToolkit.isDispatchThreadForAppContext(target)) {
331 SunToolkit.executeOnEventHandlerThread(target, r);
332 } else {
333 r.run();
334 }
335 }
336
337 public void displayMessage(String caption, String text, String messageType) {
338 Point loc = getLocationOnScreen();
339 Rectangle screen = eframe.getGraphicsConfiguration().getBounds();
340
341 // Check if the tray icon is in the bounds of a screen.
342 if (!(loc.x < screen.x || loc.x >= screen.x + screen.width ||
343 loc.y < screen.y || loc.y >= screen.y + screen.height))
344 {
345 balloon.display(caption, text, messageType);
346 }
347 }
348
349 // It's synchronized with disposal by EDT.
350 @SuppressWarnings("deprecation")
351 public void showPopupMenu(int x, int y) {
352 if (isDisposed())
353 return;
354
355 assert SunToolkit.isDispatchThreadForAppContext(target);
356
357 PopupMenu newPopup = target.getPopupMenu();
358 if (popup != newPopup) {
359 if (popup != null) {
360 eframe.remove(popup);
361 }
362 if (newPopup != null) {
363 eframe.add(newPopup);
364 }
365 popup = newPopup;
366 }
367
368 if (popup != null) {
369 Point loc = ((XBaseWindow)eframe.getPeer()).toLocal(new Point(x, y));
370 popup.show(eframe, loc.x, loc.y);
371 }
372 }
373
374
375 // ******************************************************************
376 // ******************************************************************
377
378
379 private void addXED(long window, XEventDispatcher xed, long mask) {
380 if (window == 0) {
381 return;
382 }
383 XToolkit.awtLock();
384 try {
385 XlibWrapper.XSelectInput(XToolkit.getDisplay(), window, mask);
386 } finally {
387 XToolkit.awtUnlock();
388 }
389 XToolkit.addEventDispatcher(window, xed);
399 } finally {
400 XToolkit.awtUnlock();
401 }
402 }
403
404 // Private method for testing purposes.
405 private Point getLocationOnScreen() {
406 return eframe.getLocationOnScreen();
407 }
408
409 public Rectangle getBounds() {
410 Point loc = getLocationOnScreen();
411 return new Rectangle(loc.x, loc.y, loc.x + TRAY_ICON_WIDTH, loc.y + TRAY_ICON_HEIGHT);
412 }
413
414 void addListeners() {
415 canvas.addMouseListener(eventProxy);
416 canvas.addMouseMotionListener(eventProxy);
417 }
418
419 @SuppressWarnings("deprecation")
420 long getWindow() {
421 return ((XEmbeddedFramePeer)eframe.getPeer()).getWindow();
422 }
423
424 public boolean isDisposed() {
425 return isDisposed;
426 }
427
428 public String getActionCommand() {
429 return target.getActionCommand();
430 }
431
432 static class TrayIconEventProxy implements MouseListener, MouseMotionListener {
433 XTrayIconPeer xtiPeer;
434
435 TrayIconEventProxy(XTrayIconPeer xtiPeer) {
436 this.xtiPeer = xtiPeer;
437 }
438
439 public void handleEvent(MouseEvent e) {
440 //prevent DRAG events from being posted with TrayIcon source(CR 6565779)
441 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
|
1 /*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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.security.AccessController;
34 import java.security.PrivilegedAction;
35 import java.lang.reflect.InvocationTargetException;
36 import sun.util.logging.PlatformLogger;
37
38 public class XTrayIconPeer implements TrayIconPeer,
39 InfoWindow.Balloon.LiveArguments,
40 InfoWindow.Tooltip.LiveArguments
41 {
42 private static final PlatformLogger ctrLog = PlatformLogger.getLogger("sun.awt.X11.XTrayIconPeer.centering");
43
44 TrayIcon target;
45 TrayIconEventProxy eventProxy;
46 XTrayIconEmbeddedFrame eframe;
47 TrayIconCanvas canvas;
48 InfoWindow.Balloon balloon;
49 InfoWindow.Tooltip tooltip;
50 PopupMenu popup;
51 String tooltipString;
52 boolean isTrayIconDisplayed;
328 if (!SunToolkit.isDispatchThreadForAppContext(target)) {
329 SunToolkit.executeOnEventHandlerThread(target, r);
330 } else {
331 r.run();
332 }
333 }
334
335 public void displayMessage(String caption, String text, String messageType) {
336 Point loc = getLocationOnScreen();
337 Rectangle screen = eframe.getGraphicsConfiguration().getBounds();
338
339 // Check if the tray icon is in the bounds of a screen.
340 if (!(loc.x < screen.x || loc.x >= screen.x + screen.width ||
341 loc.y < screen.y || loc.y >= screen.y + screen.height))
342 {
343 balloon.display(caption, text, messageType);
344 }
345 }
346
347 // It's synchronized with disposal by EDT.
348 public void showPopupMenu(int x, int y) {
349 if (isDisposed())
350 return;
351
352 assert SunToolkit.isDispatchThreadForAppContext(target);
353
354 PopupMenu newPopup = target.getPopupMenu();
355 if (popup != newPopup) {
356 if (popup != null) {
357 eframe.remove(popup);
358 }
359 if (newPopup != null) {
360 eframe.add(newPopup);
361 }
362 popup = newPopup;
363 }
364
365 if (popup != null) {
366 final XBaseWindow peer = AWTAccessor.getComponentAccessor()
367 .getPeer(eframe);
368 Point loc = peer.toLocal(new Point(x, y));
369 popup.show(eframe, loc.x, loc.y);
370 }
371 }
372
373
374 // ******************************************************************
375 // ******************************************************************
376
377
378 private void addXED(long window, XEventDispatcher xed, long mask) {
379 if (window == 0) {
380 return;
381 }
382 XToolkit.awtLock();
383 try {
384 XlibWrapper.XSelectInput(XToolkit.getDisplay(), window, mask);
385 } finally {
386 XToolkit.awtUnlock();
387 }
388 XToolkit.addEventDispatcher(window, xed);
398 } finally {
399 XToolkit.awtUnlock();
400 }
401 }
402
403 // Private method for testing purposes.
404 private Point getLocationOnScreen() {
405 return eframe.getLocationOnScreen();
406 }
407
408 public Rectangle getBounds() {
409 Point loc = getLocationOnScreen();
410 return new Rectangle(loc.x, loc.y, loc.x + TRAY_ICON_WIDTH, loc.y + TRAY_ICON_HEIGHT);
411 }
412
413 void addListeners() {
414 canvas.addMouseListener(eventProxy);
415 canvas.addMouseMotionListener(eventProxy);
416 }
417
418 long getWindow() {
419 return AWTAccessor.getComponentAccessor()
420 .<XEmbeddedFramePeer>getPeer(eframe).getWindow();
421 }
422
423 public boolean isDisposed() {
424 return isDisposed;
425 }
426
427 public String getActionCommand() {
428 return target.getActionCommand();
429 }
430
431 static class TrayIconEventProxy implements MouseListener, MouseMotionListener {
432 XTrayIconPeer xtiPeer;
433
434 TrayIconEventProxy(XTrayIconPeer xtiPeer) {
435 this.xtiPeer = xtiPeer;
436 }
437
438 public void handleEvent(MouseEvent e) {
439 //prevent DRAG events from being posted with TrayIcon source(CR 6565779)
440 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
|