jdk/src/share/classes/java/awt/TrayIcon.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8816 29f983feda95
child 13604 31089af1a447
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8816
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: 3288
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: 3288
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: 3288
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
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 java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.AWTEventMulticaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.EventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.PopupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.peer.TrayIconPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.SunToolkit;
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
    41
import sun.awt.HeadlessToolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.EventObject;
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
    43
import java.security.AccessControlContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
    44
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A <code>TrayIcon</code> object represents a tray icon that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * added to the {@link SystemTray system tray}. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>TrayIcon</code> can have a tooltip (text), an image, a popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * menu, and a set of listeners associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>A <code>TrayIcon</code> can generate various {@link MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * MouseEvents} and supports adding corresponding listeners to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * notification of these events.  <code>TrayIcon</code> processes some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * of the events by itself.  For example, by default, when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * right-mouse click is performed on the <code>TrayIcon</code> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * displays the specified popup menu.  When the mouse hovers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * over the <code>TrayIcon</code> the tooltip is displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p><strong>Note:</strong> When the <code>MouseEvent</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * dispatched to its registered listeners its <code>component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * property will be set to <code>null</code>.  (See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * java.awt.event.ComponentEvent#getComponent}) The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>source</code> property will be set to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>TrayIcon</code>. (See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * java.util.EventObject#getSource})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p><b>Note:</b> A well-behaved {@link TrayIcon} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * will assign different gestures to showing a popup menu and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * selecting a tray icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>A <code>TrayIcon</code> can generate an {@link ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * ActionEvent}.  On some platforms, this occurs when the user selects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * the tray icon using either the mouse or keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>If a SecurityManager is installed, the AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * {@code accessSystemTray} must be granted in order to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * a {@code TrayIcon}. Otherwise the constructor will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p> See the {@link SystemTray} class overview for an example on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * to use the <code>TrayIcon</code> API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see SystemTray#add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see java.awt.event.ComponentEvent#getComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see java.util.EventObject#getSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Bino George
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author Denis Mikhalkin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Sharon Zakhour
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @author Anton Tarasov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
public class TrayIcon {
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private String tooltip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private PopupMenu popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean autosize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private String actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    transient private TrayIconPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    transient MouseListener mouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    transient MouseMotionListener mouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    transient ActionListener actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   109
    /*
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   110
     * The tray icon's AccessControlContext.
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   111
     *
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   112
     * Unlike the acc in Component, this field is made final
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   113
     * because TrayIcon is not serializable.
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   114
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   115
    private final AccessControlContext acc = AccessController.getContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   116
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   117
    /*
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   118
     * Returns the acc this tray icon was constructed with.
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   119
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   120
    final AccessControlContext getAccessControlContext() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   121
        if (acc == null) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   122
            throw new SecurityException("TrayIcon is missing AccessControlContext");
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   123
        }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   124
        return acc;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   125
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 5506
diff changeset
   126
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private TrayIcon()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      throws UnsupportedOperationException, HeadlessException, SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        SystemTray.checkSystemTrayAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (!SystemTray.isSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        SunToolkit.insertTargetMapping(this, AppContext.getAppContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Creates a <code>TrayIcon</code> with the specified image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param image the <code>Image</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @throws IllegalArgumentException if <code>image</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws UnsupportedOperationException if the system tray isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * supported by the current platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * {@code GraphicsEnvironment.isHeadless()} returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws SecurityException if {@code accessSystemTray} permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * is not granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see SystemTray#add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @see TrayIcon#TrayIcon(Image, String, PopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see TrayIcon#TrayIcon(Image, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public TrayIcon(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this();
3233
abdab1f90b46 6759726: TrayIcon constructor throws NPE instead of documented IAE
dcherepanov
parents: 1191
diff changeset
   167
        if (image == null) {
abdab1f90b46 6759726: TrayIcon constructor throws NPE instead of documented IAE
dcherepanov
parents: 1191
diff changeset
   168
            throw new IllegalArgumentException("creating TrayIcon with null Image");
abdab1f90b46 6759726: TrayIcon constructor throws NPE instead of documented IAE
dcherepanov
parents: 1191
diff changeset
   169
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        setImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Creates a <code>TrayIcon</code> with the specified image and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * tooltip text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param image the <code>Image</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param tooltip the string to be used as tooltip text; if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * value is <code>null</code> no tooltip is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws IllegalArgumentException if <code>image</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws UnsupportedOperationException if the system tray isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * supported by the current platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * {@code GraphicsEnvironment.isHeadless()} returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws SecurityException if {@code accessSystemTray} permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * is not granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see SystemTray#add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @see TrayIcon#TrayIcon(Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see TrayIcon#TrayIcon(Image, String, PopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public TrayIcon(Image image, String tooltip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        setToolTip(tooltip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Creates a <code>TrayIcon</code> with the specified image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * tooltip and popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param image the <code>Image</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param tooltip the string to be used as tooltip text; if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * value is <code>null</code> no tooltip is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param popup the menu to be used for the tray icon's popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * menu; if the value is <code>null</code> no popup menu is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws IllegalArgumentException if <code>image</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws UnsupportedOperationException if the system tray isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * supported by the current platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @throws HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * {@code GraphicsEnvironment.isHeadless()} returns {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @throws SecurityException if {@code accessSystemTray} permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * is not granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see SystemTray#add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see TrayIcon#TrayIcon(Image, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see TrayIcon#TrayIcon(Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see PopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see #addMouseListener(MouseListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public TrayIcon(Image image, String tooltip, PopupMenu popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this(image, tooltip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        setPopupMenu(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Sets the image for this <code>TrayIcon</code>.  The previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * tray icon image is discarded without calling the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * java.awt.Image#flush} method &#151; you will need to call it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * manually.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p> If the image represents an animated image, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * animated automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <p> See the {@link #setImageAutoSize(boolean)} property for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * details on the size of the displayed image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <p> Calling this method with the same image that is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * being used has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws NullPointerException if <code>image</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param image the non-null <code>Image</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see #getImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see SystemTray#add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see TrayIcon#TrayIcon(Image, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void setImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new NullPointerException("setting null Image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        TrayIconPeer peer = this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            peer.updateImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the current image used for this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setImage(Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Sets the popup menu for this <code>TrayIcon</code>.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <code>popup</code> is <code>null</code>, no popup menu will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * associated with this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <p>Note that this <code>popup</code> must not be added to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * parent before or after it is set on the tray icon.  If you add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * it to some parent, the <code>popup</code> may be removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * that parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>The {@code popup} can be set on one {@code TrayIcon} only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Setting the same popup on multiple {@code TrayIcon}s will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * an {@code IllegalArgumentException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p><strong>Note:</strong> Some platforms may not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * showing the user-specified popup menu component when the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * right-clicks the tray icon.  In this situation, either no menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * will be displayed or, on some systems, a native version of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * menu may be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws IllegalArgumentException if the {@code popup} is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * set for another {@code TrayIcon}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param popup a <code>PopupMenu</code> or <code>null</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * remove any popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see #getPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public void setPopupMenu(PopupMenu popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (popup == this.popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (TrayIcon.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (popup.isTrayIconPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    throw new IllegalArgumentException("the PopupMenu is already set for another TrayIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                popup.isTrayIconPopup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (this.popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                this.popup.isTrayIconPopup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            this.popup = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns the popup menu associated with this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return the popup menu or <code>null</code> if none exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #setPopupMenu(PopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public PopupMenu getPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Sets the tooltip string for this <code>TrayIcon</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * tooltip is displayed automatically when the mouse hovers over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the icon.  Setting the tooltip to <code>null</code> removes any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * tooltip text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * When displayed, the tooltip string may be truncated on some platforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * the number of characters that may be displayed is platform-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param tooltip the string for the tooltip; if the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <code>null</code> no tooltip is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see #getToolTip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void setToolTip(String tooltip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        this.tooltip = tooltip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        TrayIconPeer peer = this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            peer.setToolTip(tooltip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns the tooltip string associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return the tooltip string or <code>null</code> if none exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see #setToolTip(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public String getToolTip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return tooltip;
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
     * Sets the auto-size property.  Auto-size determines whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * tray image is automatically sized to fit the space allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * for the image on the tray.  By default, the auto-size property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * is set to <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p> If auto-size is <code>false</code>, and the image size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * doesn't match the tray icon space, the image is painted as-is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * inside that space &#151; if larger than the allocated space, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * be cropped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p> If auto-size is <code>true</code>, the image is stretched or shrunk to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * fit the tray icon space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param autosize <code>true</code> to auto-size the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @see #isImageAutoSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public void setImageAutoSize(boolean autosize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.autosize = autosize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        TrayIconPeer peer = this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            peer.updateImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
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
     * Returns the value of the auto-size property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return <code>true</code> if the image will be auto-sized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see #setImageAutoSize(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public boolean isImageAutoSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return autosize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Adds the specified mouse listener to receive mouse events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * this <code>TrayIcon</code>.  Calling this method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>null</code> value has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p><b>Note</b>: The {@code MouseEvent}'s coordinates (received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * from the {@code TrayIcon}) are relative to the screen, not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * {@code TrayIcon}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p> <b>Note: </b>The <code>MOUSE_ENTERED</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>MOUSE_EXITED</code> mouse events are not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param    listener the mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see      java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see      java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see      #removeMouseListener(MouseListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see      #getMouseListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public synchronized void addMouseListener(MouseListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        mouseListener = AWTEventMulticaster.add(mouseListener, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Removes the specified mouse listener.  Calling this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <code>null</code> or an invalid value has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param    listener   the mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @see      java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @see      java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see      #addMouseListener(MouseListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see      #getMouseListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public synchronized void removeMouseListener(MouseListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        mouseListener = AWTEventMulticaster.remove(mouseListener, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns an array of all the mouse listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * registered on this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return all of the <code>MouseListeners</code> registered on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * this <code>TrayIcon</code> or an empty array if no mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see      #addMouseListener(MouseListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see      #removeMouseListener(MouseListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @see      java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public synchronized MouseListener[] getMouseListeners() {
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   458
        return AWTEventMulticaster.getListeners(mouseListener, MouseListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Adds the specified mouse listener to receive mouse-motion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * events from this <code>TrayIcon</code>.  Calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * with a <code>null</code> value has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p><b>Note</b>: The {@code MouseEvent}'s coordinates (received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * from the {@code TrayIcon}) are relative to the screen, not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * {@code TrayIcon}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <p> <b>Note: </b>The <code>MOUSE_DRAGGED</code> mouse event is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param    listener   the mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see      java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @see      java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see      #removeMouseMotionListener(MouseMotionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @see      #getMouseMotionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public synchronized void addMouseMotionListener(MouseMotionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Removes the specified mouse-motion listener.  Calling this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <code>null</code> or an invalid value has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param    listener   the mouse listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see      java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see      java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see      #addMouseMotionListener(MouseMotionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @see      #getMouseMotionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public synchronized void removeMouseMotionListener(MouseMotionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Returns an array of all the mouse-motion listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * registered on this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return all of the <code>MouseInputListeners</code> registered on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * this <code>TrayIcon</code> or an empty array if no mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see      #addMouseMotionListener(MouseMotionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @see      #removeMouseMotionListener(MouseMotionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @see      java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public synchronized MouseMotionListener[] getMouseMotionListeners() {
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   519
        return AWTEventMulticaster.getListeners(mouseMotionListener, MouseMotionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Returns the command name of the action event fired by this tray icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return the action command name, or <code>null</code> if none exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @see #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public String getActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Sets the command name for the action event fired by this tray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * icon.  By default, this action command is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param command  a string used to set the tray icon's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *                 action command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see #getActionCommand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public void setActionCommand(String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        actionCommand = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Adds the specified action listener to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <code>ActionEvent</code>s from this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Action events usually occur when a user selects the tray icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * using either the mouse or keyboard.  The conditions in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * action events are generated are platform-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <p>Calling this method with a <code>null</code> value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param         listener the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see           #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see           #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see           java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public synchronized void addActionListener(ActionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        actionListener = AWTEventMulticaster.add(actionListener, listener);
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
     * Removes the specified action listener.  Calling this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <code>null</code> or an invalid value has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param    listener   the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see      java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @see      java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @see      #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see      #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public synchronized void removeActionListener(ActionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        actionListener = AWTEventMulticaster.remove(actionListener, listener);
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
     * Returns an array of all the action listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * registered on this <code>TrayIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @return all of the <code>ActionListeners</code> registered on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * this <code>TrayIcon</code> or an empty array if no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see      #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @see      #removeActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see      java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public synchronized ActionListener[] getActionListeners() {
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   606
        return AWTEventMulticaster.getListeners(actionListener, ActionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * The message type determines which icon will be displayed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * caption of the message, and a possible system sound a message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * may generate upon showing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see TrayIcon#displayMessage(String, String, MessageType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public enum MessageType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        /** An error message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        /** A warning message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        WARNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        /** An information message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /** Simple message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        NONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Displays a popup message near the tray icon.  The message will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * disappear after a time or if the user clicks on it.  Clicking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * on the message may trigger an {@code ActionEvent}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <p>Either the caption or the text may be <code>null</code>, but an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <code>NullPointerException</code> is thrown if both are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * When displayed, the caption or text strings may be truncated on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * some platforms; the number of characters that may be displayed is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * platform-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <p><strong>Note:</strong> Some platforms may not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * showing a message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param caption the caption displayed above the text, usually in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * bold; may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param text the text displayed for the particular message; may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @param messageType an enum indicating the message type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @throws NullPointerException if both <code>caption</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * and <code>text</code> are <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public void displayMessage(String caption, String text, MessageType messageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (caption == null && text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            throw new NullPointerException("displaying the message with both caption and text being null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        TrayIconPeer peer = this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (peer != null) {
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   660
            peer.displayMessage(caption, text, messageType.name());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Returns the size, in pixels, of the space that the tray icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * occupies in the system tray.  For the tray icon that is not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * added to the system tray, the returned size is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * result of the {@link SystemTray#getTrayIconSize}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @return the size of the tray icon, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @see TrayIcon#setImageAutoSize(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see java.awt.Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @see TrayIcon#getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public Dimension getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return SystemTray.getSystemTray().getTrayIconSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    // ****************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    // ****************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    void addNotify()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
      throws AWTException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (peer == null) {
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   687
                Toolkit toolkit = Toolkit.getDefaultToolkit();
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   688
                if (toolkit instanceof SunToolkit) {
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   689
                    peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this);
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   690
                } else if (toolkit instanceof HeadlessToolkit) {
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   691
                    peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this);
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   692
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        peer.setToolTip(tooltip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    void removeNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        TrayIconPeer p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            p = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            peer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            p.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    void setID(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    int getID(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    void dispatchEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        EventQueue.setCurrentEventAndMostRecentTime(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        Toolkit.getDefaultToolkit().notifyAWTEventListeners(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (e instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            case MouseEvent.MOUSE_CLICKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                processMouseEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                processMouseMotionEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } else if (e instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            processActionEvent((ActionEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    void processMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        MouseListener listener = mouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                listener.mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                listener.mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            case MouseEvent.MOUSE_CLICKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                listener.mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    void processMouseMotionEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        MouseMotionListener listener = mouseMotionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if (listener != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            e.getID() == MouseEvent.MOUSE_MOVED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            listener.mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    void processActionEvent(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        ActionListener listener = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            listener.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
}