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