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