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