1 5 6 package java.awt; 7 8 import java.awt.Point ; 9 import java.awt.Toolkit ; 10 import java.awt.GraphicsEnvironment ; 11 import java.awt.event.*; 12 import java.awt.AWTEvent ; 13 import java.awt.AWTEventMulticaster ; 14 import java.awt.EventQueue ; 15 import java.awt.PopupMenu ; 16 import java.awt.Image ; 17 import java.util.EventListener ; 18 import java.awt.peer.TrayIconPeer; 19 import sun.awt.AppContext; 20 import sun.awt.SunToolkit; 21 import java.util.EventObject ; 22 23 71 public class TrayIcon { 72 private Image image; 73 private String tooltip; 74 private PopupMenu popup; 75 private boolean autosize; 76 private int id; 77 private String actionCommand; 78 79 transient private TrayIconPeer peer; 80 81 transient MouseListener mouseListener; 82 transient MouseMotionListener mouseMotionListener; 83 transient ActionListener actionListener; 84 85 88 transient private Object privateKey = new Object (); 89 90 static { 91 Toolkit.loadLibraries(); 92 if (!GraphicsEnvironment.isHeadless()) { 93 initIDs(); 94 } 95 } 96 97 private TrayIcon() 98 throws UnsupportedOperationException , HeadlessException , SecurityException 99 { 100 SystemTray.checkSystemTrayAllowed(); 101 if (GraphicsEnvironment.isHeadless()) { 102 throw new HeadlessException (); 103 } 104 if (!SystemTray.isSupported()) { 105 throw new UnsupportedOperationException (); 106 } 107 SunToolkit.insertTargetMapping(this, AppContext.getAppContext()); 108 } 109 110 128 public TrayIcon(Image image) { 129 this(); 130 if (image == null) { 131 throw new IllegalArgumentException ("creating TrayIcon with null Image"); 132 } 133 setImage(image); 134 } 135 136 157 public TrayIcon(Image image, String tooltip) { 158 this(image); 159 setToolTip(tooltip); 160 } 161 162 187 public TrayIcon(Image image, String tooltip, PopupMenu popup) { 188 this(image, tooltip); 189 setPopupMenu(popup); 190 } 191 192 214 public void setImage(Image image) { 215 if (image == null) { 216 throw new NullPointerException ("setting null Image"); 217 } 218 this.image = image; 219 220 TrayIconPeer peer = this.peer; 221 if (peer != null) { 222 peer.updateImage(); 223 } 224 } 225 226 233 public Image getImage() { 234 return image; 235 } 236 237 263 public void setPopupMenu(PopupMenu popup) { 264 if (popup == this.popup) { 265 return; 266 } 267 synchronized (TrayIcon .class) { 268 if (popup != null) { 269 if (popup.isTrayIconPopup) { 270 throw new IllegalArgumentException ("the PopupMenu is already set for another TrayIcon"); 271 } 272 popup.isTrayIconPopup = true; 273 } 274 if (this.popup != null) { 275 this.popup.isTrayIconPopup = false; 276 } 277 this.popup = popup; 278 } 279 } 280 281 287 public PopupMenu getPopupMenu() { 288 return popup; 289 } 290 291 304 public void setToolTip(String tooltip) { 305 this.tooltip = tooltip; 306 307 TrayIconPeer peer = this.peer; 308 if (peer != null) { 309 peer.setToolTip(tooltip); 310 } 311 } 312 313 320 public String getToolTip() { 321 return tooltip; 322 } 323 324 342 public void setImageAutoSize(boolean autosize) { 343 this.autosize = autosize; 344 345 TrayIconPeer peer = this.peer; 346 if (peer != null) { 347 peer.updateImage(); 348 } 349 } 350 351 358 public boolean isImageAutoSize() { 359 return autosize; 360 } 361 362 382 public synchronized void addMouseListener(MouseListener listener) { 383 if (listener == null) { 384 return; 385 } 386 mouseListener = AWTEventMulticaster.add(mouseListener, listener); 387 } 388 389 401 public synchronized void removeMouseListener(MouseListener listener) { 402 if (listener == null) { 403 return; 404 } 405 mouseListener = AWTEventMulticaster.remove(mouseListener, listener); 406 } 407 408 420 public synchronized MouseListener[] getMouseListeners() { 421 return (MouseListener[])(getListeners(MouseListener.class)); 422 } 423 424 443 public synchronized void addMouseMotionListener(MouseMotionListener listener) { 444 if (listener == null) { 445 return; 446 } 447 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, listener); 448 } 449 450 462 public synchronized void removeMouseMotionListener(MouseMotionListener listener) { 463 if (listener == null) { 464 return; 465 } 466 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, listener); 467 } 468 469 481 public synchronized MouseMotionListener[] getMouseMotionListeners() { 482 return (MouseMotionListener[]) (getListeners(MouseMotionListener.class)); 483 } 484 485 492 public String getActionCommand() { 493 return actionCommand; 494 } 495 496 507 public void setActionCommand(String command) { 508 actionCommand = command; 509 } 510 511 529 public synchronized void addActionListener(ActionListener listener) { 530 if (listener == null) { 531 return; 532 } 533 actionListener = AWTEventMulticaster.add(actionListener, listener); 534 } 535 536 549 public synchronized void removeActionListener(ActionListener listener) { 550 if (listener == null) { 551 return; 552 } 553 actionListener = AWTEventMulticaster.remove(actionListener, listener); 554 } 555 556 568 public synchronized ActionListener[] getActionListeners() { 569 return (ActionListener[])(getListeners(ActionListener.class)); 570 } 571 572 581 public enum MessageType { 582 583 ERROR, 584 585 WARNING, 586 587 INFO, 588 589 NONE 590 }; 591 592 616 public void displayMessage(String caption, String text, MessageType messageType) { 617 if (caption == null && text == null) { 618 throw new NullPointerException ("displaying the message with both caption and text being null"); 619 } 620 621 TrayIconPeer peer = this.peer; 622 if (peer != null) { 623 peer.displayMessage(caption, text, messageType.toString()); 624 } 625 } 626 627 638 public Dimension getSize() { 639 return SystemTray.getSystemTray().getTrayIconSize(); 640 } 641 642 645 <T extends EventListener > T[] getListeners(Class <T> listenerType) { 646 EventListener l = null; 647 if (listenerType == MouseListener.class) { 648 l = mouseListener; 649 } else if (listenerType == MouseMotionListener.class) { 650 l = mouseMotionListener; 651 } else if (listenerType == ActionListener.class) { 652 l = actionListener; 653 } 654 return AWTEventMulticaster.getListeners(l, listenerType); 655 } 656 657 void addNotify() 658 throws AWTException 659 { 660 synchronized (this) { 661 if (peer == null) { 662 peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createTrayIcon(this); 663 } 664 } 665 peer.setToolTip(tooltip); 666 } 667 668 void removeNotify() { 669 TrayIconPeer p = null; 670 synchronized (this) { 671 p = peer; 672 peer = null; 673 } 674 if (p != null) { 675 p.dispose(); 676 } 677 } 678 679 void setID(int id) { 680 this.id = id; 681 } 682 683 int getID(){ 684 return id; 685 } 686 687 void dispatchEvent(AWTEvent e) { 688 EventQueue.setCurrentEventAndMostRecentTime(e); 689 Toolkit.getDefaultToolkit().notifyAWTEventListeners(e); 690 processEvent(e); 691 } 692 693 void processEvent(AWTEvent e) { 694 if (e instanceof MouseEvent) { 695 switch(e.getID()) { 696 case MouseEvent.MOUSE_PRESSED: 697 case MouseEvent.MOUSE_RELEASED: 698 case MouseEvent.MOUSE_CLICKED: 699 processMouseEvent((MouseEvent)e); 700 break; 701 case MouseEvent.MOUSE_MOVED: 702 processMouseMotionEvent((MouseEvent)e); 703 break; 704 default: 705 return; 706 } 707 } else if (e instanceof ActionEvent) { 708 processActionEvent((ActionEvent)e); 709 } 710 } 711 712 void processMouseEvent(MouseEvent e) { 713 MouseListener listener = mouseListener; 714 715 TrayIconPeer peer = this.peer; 716 if (e.isPopupTrigger() && peer != null) { 717 peer.showPopupMenu(e.getPoint().x, e.getPoint().y); 718 } 719 720 if (listener != null) { 721 int id = e.getID(); 722 switch(id) { 723 case MouseEvent.MOUSE_PRESSED: 724 listener.mousePressed(e); 725 break; 726 case MouseEvent.MOUSE_RELEASED: 727 listener.mouseReleased(e); 728 break; 729 case MouseEvent.MOUSE_CLICKED: 730 listener.mouseClicked(e); 731 break; 732 default: 733 return; 734 } 735 } 736 } 737 738 void processMouseMotionEvent(MouseEvent e) { 739 MouseMotionListener listener = mouseMotionListener; 740 if (listener != null && 741 e.getID() == MouseEvent.MOUSE_MOVED) 742 { 743 listener.mouseMoved(e); 744 } 745 } 746 747 void processActionEvent(ActionEvent e) { 748 ActionListener listener = actionListener; 749 if (listener != null) { 750 listener.actionPerformed(e); 751 } 752 } 753 754 private static native void initIDs(); 755 } 756 | Popular Tags |