jdk/src/solaris/classes/sun/awt/X11/XTrayIconPeer.java
author dholmes
Fri, 25 Mar 2011 07:09:38 -0400
changeset 9002 6ee0abf0167f
parent 7668 d4a77089c587
child 16839 d0f2e97b7359
permissions -rw-r--r--
7030063: AWT support for SE-Embedded integration Summary: AWT support for SE-Embedded Reviewed-by: anthony, art, bobv, collins, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9002
6ee0abf0167f 7030063: AWT support for SE-Embedded integration
dholmes
parents: 7668
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.TrayIconPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.ArrayBlockingQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
    38
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    40
public class XTrayIconPeer implements TrayIconPeer,
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    41
       InfoWindow.Balloon.LiveArguments,
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    42
       InfoWindow.Tooltip.LiveArguments
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    43
{
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
    44
    private static final PlatformLogger ctrLog = PlatformLogger.getLogger("sun.awt.X11.XTrayIconPeer.centering");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    TrayIcon target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    TrayIconEventProxy eventProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    XTrayIconEmbeddedFrame eframe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    TrayIconCanvas canvas;
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    50
    InfoWindow.Balloon balloon;
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
    51
    InfoWindow.Tooltip tooltip;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    PopupMenu popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    String tooltipString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    boolean isTrayIconDisplayed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    long eframeParentID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final XEventDispatcher parentXED, eframeXED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final XEventDispatcher dummyXED = new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            public void dispatchEvent(XEvent ev) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    volatile boolean isDisposed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    boolean isParentWindowLocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int old_x, old_y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int ex_width, ex_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    final static int TRAY_ICON_WIDTH = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    final static int TRAY_ICON_HEIGHT = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    XTrayIconPeer(TrayIcon target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
      throws AWTException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        eventProxy = new TrayIconEventProxy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        canvas = new TrayIconCanvas(target, TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        eframe = new XTrayIconEmbeddedFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        eframe.setSize(TRAY_ICON_WIDTH, TRAY_ICON_HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        eframe.add(canvas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // Fix for 6317038: as EmbeddedFrame is instance of Frame, it is blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // by modal dialogs, but in the case of TrayIcon it shouldn't. So we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // set ModalExclusion property on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                eframe.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (XWM.getWMID() != XWM.METACITY_WM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            parentXED = dummyXED; // We don't like to leave it 'null'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            parentXED = new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // It's executed under AWTLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                public void dispatchEvent(XEvent ev) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 431
diff changeset
   103
                    if (isDisposed() || ev.get_type() != XConstants.ConfigureNotify) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    XConfigureEvent ce = ev.get_xconfigure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   109
                    ctrLog.fine("ConfigureNotify on parent of {0}: {1}x{2}+{3}+{4} (old: {5}+{6})",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   110
                                XTrayIconPeer.this, ce.get_width(), ce.get_height(),
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   111
                                ce.get_x(), ce.get_y(), old_x, old_y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    // A workaround for Gnome/Metacity (it doesn't affect the behaviour on KDE).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    // On Metacity the EmbeddedFrame's parent window bounds are larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    // than TrayIcon size required (that is we need a square but a rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    // is provided by the Panel Notification Area). The parent's background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    // differs from the Panel's one. To hide the background we resize parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    // window so that it fits the EmbeddedFrame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    // However due to resizing the parent window it loses centering in the Panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    // We center it when discovering that some of its side is of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    // than the fixed value. Centering is being done by "X" (when the parent's width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    // is greater) and by "Y" (when the parent's height is greater).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    // Actually we need this workaround until we could detect taskbar color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if (ce.get_height() != TRAY_ICON_HEIGHT && ce.get_width() != TRAY_ICON_WIDTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        // If both the height and the width differ from the fixed size then WM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        // must level at least one side to the fixed size. For some reason it may take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        // a few hops (even after reparenting) and we have to skip the intermediate ones.
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   131
                        ctrLog.fine("ConfigureNotify on parent of {0}. Skipping as intermediate resizing.",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   132
                                    XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    } else if (ce.get_height() > TRAY_ICON_HEIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   137
                        ctrLog.fine("ConfigureNotify on parent of {0}. Centering by \"Y\".",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   138
                                    XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), eframeParentID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                      ce.get_x(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                      ce.get_y()+ce.get_height()/2-TRAY_ICON_HEIGHT/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                      TRAY_ICON_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                      TRAY_ICON_HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        ex_height = ce.get_height();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        ex_width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    } else if (ce.get_width() > TRAY_ICON_WIDTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   150
                        ctrLog.fine("ConfigureNotify on parent of {0}. Centering by \"X\".",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   151
                                    XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), eframeParentID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                                      ce.get_x()+ce.get_width()/2 - TRAY_ICON_WIDTH/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                                      ce.get_y(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                                      TRAY_ICON_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                                      TRAY_ICON_HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        ex_width = ce.get_width();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        ex_height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    } else if (isParentWindowLocated && ce.get_x() != old_x && ce.get_y() != old_y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        // If moving by both "X" and "Y".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        // When some tray icon gets removed from the tray, a Java icon may be repositioned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        // In this case the parent window also lose centering. We have to restore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        if (ex_height != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   168
                            ctrLog.fine("ConfigureNotify on parent of {0}. Move detected. Centering by \"Y\".",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   169
                                        XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            XlibWrapper.XMoveWindow(XToolkit.getDisplay(), eframeParentID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                                    ce.get_x(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                                    ce.get_y() + ex_height/2 - TRAY_ICON_HEIGHT/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        } else if (ex_width != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   177
                            ctrLog.fine("ConfigureNotify on parent of {0}. Move detected. Centering by \"X\".",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   178
                                        XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            XlibWrapper.XMoveWindow(XToolkit.getDisplay(), eframeParentID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                                    ce.get_x() + ex_width/2 - TRAY_ICON_WIDTH/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                    ce.get_y());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        } else {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   184
                            ctrLog.fine("ConfigureNotify on parent of {0}. Move detected. Skipping.",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2472
diff changeset
   185
                                        XTrayIconPeer.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    old_x = ce.get_x();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    old_y = ce.get_y();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    isParentWindowLocated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        eframeXED = new XEventDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // It's executed under AWTLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                XTrayIconPeer xtiPeer = XTrayIconPeer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                public void dispatchEvent(XEvent ev) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 431
diff changeset
   199
                    if (isDisposed() || ev.get_type() != XConstants.ReparentNotify) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    XReparentEvent re = ev.get_xreparent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    eframeParentID = re.get_parent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    if (eframeParentID == XToolkit.getDefaultRootWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        if (isTrayIconDisplayed) { // most likely Notification Area was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            SunToolkit.executeOnEventHandlerThread(xtiPeer.target, new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                        SystemTray.getSystemTray().remove(xtiPeer.target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    if (!isTrayIconDisplayed) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 431
diff changeset
   219
                        addXED(eframeParentID, parentXED, XConstants.StructureNotifyMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        isTrayIconDisplayed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 431
diff changeset
   227
        addXED(getWindow(), eframeXED, XConstants.StructureNotifyMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        XSystemTrayPeer.getPeerInstance().addTrayIcon(this); // throws AWTException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // Wait till the EmbeddedFrame is reparented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        long start = System.currentTimeMillis();
9002
6ee0abf0167f 7030063: AWT support for SE-Embedded integration
dholmes
parents: 7668
diff changeset
   233
        final long PERIOD = XToolkit.getTrayIconDisplayTimeout();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            while (!isTrayIconDisplayed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    XToolkit.awtLockWait(PERIOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (System.currentTimeMillis() - start > PERIOD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // This is unlikely to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!isTrayIconDisplayed || eframeParentID == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            eframeParentID == XToolkit.getDefaultRootWindow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new AWTException("TrayIcon couldn't be displayed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        eframe.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        updateImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   260
        balloon = new InfoWindow.Balloon(eframe, target, this);
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   261
        tooltip = new InfoWindow.Tooltip(eframe, target, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        addListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (SunToolkit.isDispatchThreadForAppContext(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            disposeOnEDT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                SunToolkit.executeOnEDTAndWait(target, new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            disposeOnEDT();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } catch (InvocationTargetException ite) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private void disposeOnEDT() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // All actions that is to be synchronized with disposal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // should be executed either under AWTLock, or on EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // isDisposed value must be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        isDisposed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        removeXED(getWindow(), eframeXED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        removeXED(eframeParentID, parentXED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        eframe.realDispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        balloon.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        isTrayIconDisplayed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        XToolkit.targetDisposedPeer(target, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public static void suppressWarningString(Window w) {
4371
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4366
diff changeset
   298
        AWTAccessor.getWindowAccessor().setTrayIconWindow(w, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void setToolTip(String tooltip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        tooltipString = tooltip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   305
    public String getTooltipString() {
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   306
        return tooltipString;
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   307
    }
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   308
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void updateImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        Runnable r = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    canvas.updateImage(target.getImage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (!SunToolkit.isDispatchThreadForAppContext(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            SunToolkit.executeOnEventHandlerThread(target, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void displayMessage(String caption, String text, String messageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        Point loc = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        Rectangle screen = eframe.getGraphicsConfiguration().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // Check if the tray icon is in the bounds of a screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (!(loc.x < screen.x || loc.x >= screen.x + screen.width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
              loc.y < screen.y || loc.y >= screen.y + screen.height))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            balloon.display(caption, text, messageType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // It's synchronized with disposal by EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public void showPopupMenu(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (isDisposed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        assert SunToolkit.isDispatchThreadForAppContext(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        PopupMenu newPopup = target.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (popup != newPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                eframe.remove(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (newPopup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                eframe.add(newPopup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            popup = newPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            Point loc = ((XBaseWindow)eframe.getPeer()).toLocal(new Point(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            popup.show(eframe, loc.x, loc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    // ******************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    // ******************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private void addXED(long window, XEventDispatcher xed, long mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (window == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            XlibWrapper.XSelectInput(XToolkit.getDisplay(), window, mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        XToolkit.addEventDispatcher(window, xed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private void removeXED(long window, XEventDispatcher xed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (window == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            XToolkit.removeEventDispatcher(window, xed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // Private method for testing purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private Point getLocationOnScreen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return eframe.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   394
    public Rectangle getBounds() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        Point loc = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return new Rectangle(loc.x, loc.y, loc.x + TRAY_ICON_WIDTH, loc.y + TRAY_ICON_HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    void addListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        canvas.addMouseListener(eventProxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        canvas.addMouseMotionListener(eventProxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    long getWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return ((XEmbeddedFramePeer)eframe.getPeer()).getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   408
    public boolean isDisposed() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return isDisposed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   412
    public String getActionCommand() {
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   413
        return target.getActionCommand();
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   414
    }
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   415
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static class TrayIconEventProxy implements MouseListener, MouseMotionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        XTrayIconPeer xtiPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        TrayIconEventProxy(XTrayIconPeer xtiPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            this.xtiPeer = xtiPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        public void handleEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            //prevent DRAG events from being posted with TrayIcon source(CR 6565779)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // Event handling is synchronized with disposal by EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (xtiPeer.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            Point coord = XBaseWindow.toOtherWindow(xtiPeer.getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                                    XToolkit.getDefaultRootWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                    e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (e.isPopupTrigger()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                xtiPeer.showPopupMenu(coord.x, coord.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            e.translatePoint(coord.x - e.getX(), coord.y - e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // This is a hack in order to set non-Component source to MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // In some cases this could lead to unpredictable result (e.g. when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // other class tries to cast source field to Component).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // We already filter DRAG events out (CR 6565779).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            e.setSource(xtiPeer.target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if ((e.getClickCount() > 1 || xtiPeer.balloon.isVisible()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                e.getButton() == MouseEvent.BUTTON1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                ActionEvent aev = new ActionEvent(xtiPeer.target, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                                  xtiPeer.target.getActionCommand(), e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                                  e.getModifiers());
6826
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 5506
diff changeset
   457
                XToolkit.postEvent(XToolkit.targetToAppContext(aev.getSource()), aev);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (xtiPeer.balloon.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                xtiPeer.balloon.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            xtiPeer.tooltip.enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            xtiPeer.tooltip.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    // ***************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    // Special embedded frame for tray icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    // ***************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private static class XTrayIconEmbeddedFrame extends XEmbeddedFrame {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        public XTrayIconEmbeddedFrame(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            super(XToolkit.getDefaultRootWindow(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public boolean isUndecorated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public boolean isResizable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // embedded frame for tray icon shouldn't be disposed by anyone except tray icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        public void dispose(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        public void realDispose(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // ***************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    // Classes for painting an image on canvas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    // ***************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    static class TrayIconCanvas extends IconCanvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        TrayIcon target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        boolean autosize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        TrayIconCanvas(TrayIcon target, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            super(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // Invoke on EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        protected void repaintImage(boolean doClear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            boolean old_autosize = autosize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            autosize = target.isImageAutoSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            curW = autosize ? width : image.getWidth(observer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            curH = autosize ? height : image.getHeight(observer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            super.repaintImage(doClear || (old_autosize != autosize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
2472
b7aba00cabb6 6693253: Security Warning appearance requires enhancements
anthony
parents: 439
diff changeset
   537
    public static class IconCanvas extends Canvas {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        volatile Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        IconObserver observer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        int width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        int curW, curH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        IconCanvas(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            this.width = curW = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            this.height = curH = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // Invoke on EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        public void updateImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (observer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                observer = new IconObserver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            repaintImage(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        // Invoke on EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        protected void repaintImage(boolean doClear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    if (isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        if (doClear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            update(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                            paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        // Invoke on EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (g != null && curW > 0 && curH > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                BufferedImage bufImage = new BufferedImage(curW, curH, BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                Graphics2D gr = bufImage.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                if (gr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        gr.setColor(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        gr.fillRect(0, 0, curW, curH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        gr.drawImage(image, 0, 0, curW, curH, observer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        gr.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        g.drawImage(bufImage, 0, 0, curW, curH, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        gr.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        class IconObserver implements ImageObserver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            public boolean imageUpdate(final Image image, final int flags, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                if (image != IconCanvas.this.image || // if the image has been changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    !IconCanvas.this.isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                              ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    SunToolkit.executeOnEventHandlerThread(IconCanvas.this, new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                repaintImage(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                return (flags & ImageObserver.ALLBITS) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}