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