jdk/src/share/classes/java/awt/SystemTray.java
author chegar
Mon, 17 Jun 2013 14:09:25 +0100
changeset 18178 ee71c923891d
parent 13604 31089af1a447
child 20172 f48935a247ec
permissions -rw-r--r--
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
     2
 * Copyright (c) 2005, 2008, 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: 3956
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: 3956
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: 3956
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
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.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.peer.SystemTrayPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.awt.HeadlessToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.SecurityConstants;
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
    36
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The <code>SystemTray</code> class represents the system tray for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * desktop.  On Microsoft Windows it is referred to as the "Taskbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Status Area", on Gnome it is referred to as the "Notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Area", on KDE it is referred to as the "System Tray".  The system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * tray is shared by all applications running on the desktop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> On some platforms the system tray may not be present or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * be supported, in this case {@link SystemTray#getSystemTray()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * throws {@link UnsupportedOperationException}.  To detect whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * system tray is supported, use {@link SystemTray#isSupported}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>The <code>SystemTray</code> may contain one or more {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * TrayIcon TrayIcons}, which are added to the tray using the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * #add} method, and removed when no longer needed, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #remove}.  <code>TrayIcon</code> consists of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * image, a popup menu and a set of associated listeners.  Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the {@link TrayIcon} class for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>Every Java application has a single <code>SystemTray</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * instance that allows the app to interface with the system tray of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the desktop while the app is running.  The <code>SystemTray</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * instance can be obtained from the {@link #getSystemTray} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * An application may not create its own instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>SystemTray</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>The following code snippet demonstrates how to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * and customize the system tray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     {@link TrayIcon} trayIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     if (SystemTray.isSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         // get the SystemTray instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         SystemTray tray = SystemTray.{@link #getSystemTray};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         // load an image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         {@link java.awt.Image} image = {@link java.awt.Toolkit#getImage(String) Toolkit.getDefaultToolkit().getImage}(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         // create a action listener to listen for default action executed on the tray icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         {@link java.awt.event.ActionListener} listener = new {@link java.awt.event.ActionListener ActionListener}() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *             public void {@link java.awt.event.ActionListener#actionPerformed actionPerformed}({@link java.awt.event.ActionEvent} e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *                 // execute default action of the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *                 // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         // create a popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *         {@link java.awt.PopupMenu} popup = new {@link java.awt.PopupMenu#PopupMenu PopupMenu}();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         // create menu item for the default action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *         MenuItem defaultItem = new MenuItem(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         defaultItem.addActionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *         popup.add(defaultItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *         /// ... add other items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         // construct a TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         trayIcon = new {@link TrayIcon#TrayIcon(java.awt.Image, String, java.awt.PopupMenu) TrayIcon}(image, "Tray Demo", popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *         // set the TrayIcon properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         trayIcon.{@link TrayIcon#addActionListener(java.awt.event.ActionListener) addActionListener}(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         // add the tray image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *             tray.{@link SystemTray#add(TrayIcon) add}(trayIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *         } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *             System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         }
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
 *         // disable tray option in your application or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         // perform other actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *     // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *     // some time later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     // the application state has changed - update the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *     if (trayIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *         trayIcon.{@link TrayIcon#setImage(java.awt.Image) setImage}(updatedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *     // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author Bino George
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @author Denis Mikhalkin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @author Sharon Zakhour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @author Anton Tarasov
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
public class SystemTray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static SystemTray systemTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private int currentIconID = 0; // each TrayIcon added gets a unique ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    transient private SystemTrayPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   129
    private static final TrayIcon[] EMPTY_TRAY_ARRAY = new TrayIcon[0];
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   130
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   131
    static {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   132
        AWTAccessor.setSystemTrayAccessor(
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   133
            new AWTAccessor.SystemTrayAccessor() {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   134
                public void firePropertyChange(SystemTray tray,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   135
                                               String propertyName,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   136
                                               Object oldValue,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   137
                                               Object newValue) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   138
                    tray.firePropertyChange(propertyName, oldValue, newValue);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   139
                }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   140
            });
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   141
    }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 5506
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Private <code>SystemTray</code> constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private SystemTray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Gets the <code>SystemTray</code> instance that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * desktop's tray area.  This always returns the same instance per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * application.  On some platforms the system tray may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * supported.  You may use the {@link #isSupported} method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * check if the system tray is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <p>If a SecurityManager is installed, the AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * {@code accessSystemTray} must be granted in order to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * {@code SystemTray} instance. Otherwise this method will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return the <code>SystemTray</code> instance that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the desktop's tray area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws UnsupportedOperationException if the system tray isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * supported by the current platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws SecurityException if {@code accessSystemTray} permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * is not granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see #add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see #isSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static SystemTray getSystemTray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        checkSystemTrayAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        initializeSystemTrayIfNeeded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!isSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                "The system tray is not supported on the current platform.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return systemTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns whether the system tray is supported on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * platform.  In addition to displaying the tray icon, minimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * system tray support includes either a popup menu (see {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * TrayIcon#setPopupMenu(PopupMenu)}) or an action event (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * {@link TrayIcon#addActionListener(ActionListener)}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>Developers should not assume that all of the system tray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * functionality is supported.  To guarantee that the tray icon's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * default action is always accessible, add the default action to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * both the action listener and the popup menu.  See the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * SystemTray example} for an example of how to do this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <p><b>Note</b>: When implementing <code>SystemTray</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>TrayIcon</code> it is <em>strongly recommended</em> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * you assign different gestures to the popup menu and an action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * event.  Overloading a gesture for both purposes is confusing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * and may prevent the user from accessing one or the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see #getSystemTray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return <code>false</code> if no system tray access is supported; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * method returns <code>true</code> if the minimal system tray access is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * supported but does not guarantee that all system tray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * functionality is supported for the current platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public static boolean isSupported() {
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   219
        Toolkit toolkit = Toolkit.getDefaultToolkit();
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   220
        if (toolkit instanceof SunToolkit) {
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   221
            // connecting tray to native resource
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   222
            initializeSystemTrayIfNeeded();
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   223
            return ((SunToolkit)toolkit).isTraySupported();
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   224
        } else if (toolkit instanceof HeadlessToolkit) {
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   225
            // skip initialization as the init routine
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   226
            // throws HeadlessException
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   227
            return ((HeadlessToolkit)toolkit).isTraySupported();
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   228
        } else {
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   229
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Adds a <code>TrayIcon</code> to the <code>SystemTray</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * The tray icon becomes visible in the system tray once it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * added.  The order in which icons are displayed in a tray is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * specified - it is platform and implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <p> All icons added by the application are automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * removed from the <code>SystemTray</code> upon application exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * and also when the desktop system tray becomes unavailable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param trayIcon the <code>TrayIcon</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws NullPointerException if <code>trayIcon</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws IllegalArgumentException if the same instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * a <code>TrayIcon</code> is added more than once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws AWTException if the desktop system tray is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see #remove(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see #getSystemTray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see java.awt.Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void add(TrayIcon trayIcon) throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (trayIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new NullPointerException("adding null TrayIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        TrayIcon[] oldArray = null, newArray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Vector<TrayIcon> icons = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            oldArray = systemTray.getTrayIcons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (icons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                icons = new Vector<TrayIcon>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                AppContext.getAppContext().put(TrayIcon.class, icons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else if (icons.contains(trayIcon)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                throw new IllegalArgumentException("adding TrayIcon that is already added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            icons.add(trayIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            newArray = systemTray.getTrayIcons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            trayIcon.setID(++currentIconID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            trayIcon.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            icons.remove(trayIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        firePropertyChange("trayIcons", oldArray, newArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Removes the specified <code>TrayIcon</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <code>SystemTray</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p> All icons added by the application are automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * removed from the <code>SystemTray</code> upon application exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * and also when the desktop system tray becomes unavailable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> If <code>trayIcon</code> is <code>null</code> or was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * added to the system tray, no exception is thrown and no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param trayIcon the <code>TrayIcon</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public void remove(TrayIcon trayIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (trayIcon == null) {
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
        TrayIcon[] oldArray = null, newArray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            oldArray = systemTray.getTrayIcons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // TrayIcon with no peer is not contained in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (icons == null || !icons.remove(trayIcon)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            trayIcon.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            newArray = systemTray.getTrayIcons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        firePropertyChange("trayIcons", oldArray, newArray);
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 an array of all icons added to the tray by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * application.  You can't access the icons added by another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * application.  Some browsers partition applets in different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * code bases into separate contexts, and establish walls between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * these contexts.  In such a scenario, only the tray icons added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * from this context will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p> The returned array is a copy of the actual array and may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * modified in any way without affecting the system tray.  To
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * remove a <code>TrayIcon</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <code>SystemTray</code>, use the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * #remove(TrayIcon)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return an array of all tray icons added to this tray, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * empty array if none has been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @see #add(TrayIcon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @see TrayIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public TrayIcon[] getTrayIcons() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (icons != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return (TrayIcon[])icons.toArray(new TrayIcon[icons.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
1168
5f1d2fcfcd3e 6280057: I have audited SystemTray and TrayIcon code
ant
parents: 2
diff changeset
   342
        return EMPTY_TRAY_ARRAY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns the size, in pixels, of the space that a tray icon will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * occupy in the system tray.  Developers may use this methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * acquire the preferred size for the image property of a tray icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * before it is created.  For convenience, there is a similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * method {@link TrayIcon#getSize} in the <code>TrayIcon</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return the default size of a tray icon, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see TrayIcon#setImageAutoSize(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see java.awt.Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see TrayIcon#getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public Dimension getTrayIconSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return peer.getTrayIconSize();
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
     * Adds a {@code PropertyChangeListener} to the list of listeners for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * specific property. The following properties are currently supported:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <table border=1 summary="SystemTray properties">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *    <th>Property</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *    <th>Description</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *    <td>{@code trayIcons}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *    <td>The {@code SystemTray}'s array of {@code TrayIcon} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *        The array is accessed via the {@link #getTrayIcons} method.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *        This property is changed when a tray icon is added to (or removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *        from) the system tray.<br> For example, this property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *        when the system tray becomes unavailable on the desktop<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *        and the tray icons are automatically removed.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *    <td>{@code systemTray}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *    <td>This property contains {@code SystemTray} instance when the system tray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *        is available or <code>null</code> otherwise.<br> This property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *        when the system tray becomes available or unavailable on the desktop.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *        The property is accessed by the {@link #getSystemTray} method.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The {@code listener} listens to property changes only in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * If {@code listener} is {@code null}, no exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param propertyName the specified property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param listener the property change listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see #removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see #getPropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public synchronized void addPropertyChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                                       PropertyChangeListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        getCurrentChangeSupport().addPropertyChangeListener(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Removes a {@code PropertyChangeListener} from the listener list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * for a specific property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * The {@code PropertyChangeListener} must be from this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * If {@code propertyName} or {@code listener} is {@code null} or invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * no exception is thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param propertyName the specified property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param listener the PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @see #getPropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public synchronized void removePropertyChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                                          PropertyChangeListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (listener == null) {
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
        getCurrentChangeSupport().removePropertyChangeListener(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Returns an array of all the listeners that have been associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * with the named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Only the listeners in this context are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param propertyName the specified property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return all of the {@code PropertyChangeListener}s associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         the named property; if no such listeners have been added or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         if {@code propertyName} is {@code null} or invalid, an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         array is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see #removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return getCurrentChangeSupport().getPropertyChangeListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Support for reporting bound property changes for Object properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * This method can be called when a bound property has changed and it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * send the appropriate PropertyChangeEvent to any registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * PropertyChangeListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param propertyName the property whose value has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param oldValue the property's previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param newValue the property's new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void firePropertyChange(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                    Object oldValue, Object newValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (oldValue != null && newValue != null && oldValue.equals(newValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        getCurrentChangeSupport().firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Returns the current PropertyChangeSupport instance for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * calling thread's context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return this thread's context's PropertyChangeSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    private synchronized PropertyChangeSupport getCurrentChangeSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        PropertyChangeSupport changeSupport =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            (PropertyChangeSupport)AppContext.getAppContext().get(SystemTray.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            changeSupport = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            AppContext.getAppContext().put(SystemTray.class, changeSupport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    synchronized void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (peer == null) {
1191
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   494
            Toolkit toolkit = Toolkit.getDefaultToolkit();
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   495
            if (toolkit instanceof SunToolkit) {
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   496
                peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this);
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   497
            } else if (toolkit instanceof HeadlessToolkit) {
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   498
                peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this);
f142c1da78c2 6737722: api/java_awt/TrayIcon/index.html#TrayIconHeadlessMode
dcherepanov
parents: 1168
diff changeset
   499
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    static void checkSystemTrayAllowed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (security != null) {
3956
2586d23078e4 6854954: Eliminate static dependency on java.awt.AWTPermission
alanb
parents: 1191
diff changeset
   506
            security.checkPermission(SecurityConstants.AWT.ACCESS_SYSTEM_TRAY_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private static void initializeSystemTrayIfNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        synchronized (SystemTray.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (systemTray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                systemTray = new SystemTray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
}