home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Micrsoft / SAMPLES / VJ6SAMPL.EXE / DeskMgr / TrayIcon.java < prev   
Encoding:
Java Source  |  1998-02-24  |  11.0 KB  |  363 lines

  1.  
  2. /*  Copyright (C) Microsoft Corporation, 1996-1998.  All rights reserved.
  3.  
  4.   This source code is intended only as a supplement to Microsoft
  5.   Visual J++ 6.0.  See this product's on-line documentation for detailed   
  6. information regarding Microsoft code samples.
  7.  
  8. */
  9.  
  10. import wfc.app.*;
  11. import wfc.ui.*;
  12. import wfc.core.*;
  13. import wfc.win32.*;
  14. import wfc.util.*;
  15.  
  16. public class TrayIcon extends Component {
  17.    public static final int MB_LEFT   = Control.MB_LEFT;
  18.    public static final int MB_RIGHT  = Control.MB_RIGHT;
  19.    public static final int MB_MIDDLE = Control.MB_MIDDLE;
  20.  
  21.    private static final Object EVENT_MOUSEDOWN  = new Object();
  22.    private static final Object EVENT_MOUSEENTER = new Object();
  23.    private static final Object EVENT_MOUSELEAVE = new Object();
  24.    private static final Object EVENT_MOUSEMOVE  = new Object();
  25.    private static final Object EVENT_MOUSEUP    = new Object();
  26.    private static final Object EVENT_CLICK      = new Object();
  27.    private static final Object EVENT_DOUBLECLICK = new Object();
  28.  
  29.  
  30.    // This API creates a unique message ID, and associates it with a string
  31.    // that you specify.  It allows multiple applications to get the same
  32.    // ID for the same message, and to ensure that the ID doesn't collide with
  33.    // other Win32 message IDs already defined.  That's what we use it for here.
  34.    private static final int WM_TRAYMOUSEMESSAGE = RegisterWindowMessage( "TrayIconNotification" ); // Windows.WM_USER + 1024;
  35.    /** @dll.import( "user32", auto ) */
  36.    private native static int RegisterWindowMessage( String messageName );
  37.  
  38.    private Icon icon = null;
  39.    private String text = null;
  40.    private int id = 0;
  41.    private boolean visible = false;
  42.    private boolean added = false;
  43.    private boolean enabled = true;
  44.    private TrayIconWindow window = null;
  45.    private Menu contextMenu = null;
  46.  
  47.    private static int nextId = 0;
  48.    private static NOTIFYICONDATA data = new NOTIFYICONDATA();
  49.    static {
  50.       data.uCallbackMessage = WM_TRAYMOUSEMESSAGE;
  51.    }
  52.  
  53.    public TrayIcon() {
  54.       id = ++nextId;
  55.       window = new TrayIconWindow(this);
  56.    }
  57.  
  58.    public void addOnMouseDown(MouseEventHandler value) {
  59.       addEventHandler(EVENT_MOUSEDOWN, value);
  60.    }
  61.  
  62.    public void addOnMouseMove(MouseEventHandler value) {
  63.       addEventHandler(EVENT_MOUSEMOVE, value);
  64.    }
  65.  
  66.    public void addOnMouseUp(MouseEventHandler value) {
  67.       addEventHandler(EVENT_MOUSEUP, value);
  68.    }
  69.  
  70.    protected void fireMouseEvent(Object key, MouseEvent e) {
  71.       com.ms.lang.Delegate handler = getEventHandler(key);
  72.       if (handler != null) ((MouseEventHandler)handler).invoke(this, e);
  73.    }
  74.  
  75.    public void finalize() {
  76.       updateIcon(false);
  77.       window.destroyHandle();
  78.    }
  79.  
  80.    public boolean getEnabled() {
  81.       return enabled;
  82.    }
  83.  
  84.    public String getText() {
  85.       return text;
  86.    }
  87.  
  88.    public Menu getContextMenu() {
  89.       return contextMenu;
  90.    }
  91.  
  92.    public Icon getIcon() {
  93.       return icon;
  94.    }
  95.  
  96.    public boolean getVisible() {
  97.       return visible;
  98.    }
  99.  
  100.    protected void onMouseDown(MouseEvent e) {
  101.       if (enabled)
  102.          fireMouseEvent(EVENT_MOUSEDOWN, e);
  103.    }
  104.  
  105.    protected void onMouseMove(MouseEvent e) {
  106.       if (enabled)
  107.            fireMouseEvent(EVENT_MOUSEMOVE, e);
  108.    }
  109.  
  110.    protected void onMouseUp(MouseEvent e) {
  111.       if (enabled)
  112.            fireMouseEvent(EVENT_MOUSEUP, e);
  113.    }
  114.  
  115.    public void removeOnMouseDown(MouseEventHandler value) {
  116.       removeEventHandler(EVENT_MOUSEDOWN, value);
  117.    }
  118.  
  119.    public void removeOnMouseMove(MouseEventHandler value) {
  120.       removeEventHandler(EVENT_MOUSEMOVE, value);
  121.    }
  122.  
  123.    public void removeOnMouseUp(MouseEventHandler value) {
  124.       removeEventHandler(EVENT_MOUSEUP, value);
  125.    }
  126.  
  127.    public void setText(String text) {
  128.       if (!Utils.equals(this.text, text)) {
  129.          this.text = text;
  130.          if (added) {
  131.             updateIcon(true);
  132.          }
  133.       }
  134.    }
  135.  
  136.    public void setContextMenu(Menu contextMenu) {
  137.       this.contextMenu = contextMenu;
  138.    }
  139.  
  140.    public void setIcon(Icon icon) {
  141.       this.icon = icon;
  142.       if (added) {
  143.          updateIcon(true);
  144.       }
  145.    }
  146.  
  147.    public void setVisible(boolean value) {
  148.       if (visible != value) {
  149.          updateIcon(value);
  150.          visible = value;
  151.       }
  152.    }
  153.  
  154.    public void setEnabled( boolean value ) {
  155.       enabled = value;
  156.    }
  157.  
  158.    private void showContextMenu() {
  159.       if (contextMenu != null && enabled) {
  160.          POINT pt = new POINT();
  161.          Windows.GetCursorPos(pt);
  162.          Windows.TrackPopupMenuEx(contextMenu.getHandle(),
  163.             Windows.TPM_VERTICAL,
  164.             pt.x,
  165.             pt.y,
  166.             window.getHandle(),
  167.             null);
  168.       }
  169.    }
  170.  
  171.    private synchronized void updateIcon(boolean visible) {
  172.       // Bail if in design mode...
  173.       //
  174.       ISite site = getSite();
  175.       if (site != null) {
  176.          if (site.getDesignMode()) {
  177.             return;
  178.          }
  179.       }
  180.  
  181.       data.uFlags = Windows.NIF_MESSAGE;
  182.       if (visible) {
  183.          if (window.getHandle() == 0) {
  184.             window.createHandle(new CreateParams());
  185.          }
  186.  
  187.          data.hWnd = window.getHandle();
  188.       }
  189.       data.uID = id;
  190.       data.hIcon = 0;
  191.       data.szTip = null;
  192.       if (icon != null) {
  193.          data.uFlags |= Windows.NIF_ICON;
  194.          data.hIcon = icon.getHandle();
  195.       }
  196.       if (text != null) {
  197.          data.uFlags |= Windows.NIF_TIP;
  198.          data.szTip = text;
  199.       }
  200.  
  201.       if (visible) {
  202.          if (!added) {
  203.             Windows.Shell_NotifyIcon(Windows.NIM_ADD, data);
  204.             added = true;
  205.          }
  206.          else {
  207.             Windows.Shell_NotifyIcon(Windows.NIM_MODIFY, data);
  208.          }
  209.       }
  210.       else if (added) {
  211.          Windows.Shell_NotifyIcon(Windows.NIM_DELETE, data);
  212.          added = false;
  213.       }
  214.    }
  215.  
  216.    private void wmMouseDown(Message m, int button, int clicks) {
  217.       onMouseDown(new MouseEvent(button, clicks, 0, 0, 0));
  218.    }
  219.  
  220.    private void wmMouseMove(Message m) {
  221.       onMouseMove(new MouseEvent(Control.getMouseButtons(), 0, 0, 0, 0));
  222.    }
  223.  
  224.    private void wmMouseUp(Message m, int button) {
  225.       onMouseUp(new MouseEvent(button, 0, 0, 0, 0));
  226.    }
  227.  
  228.    protected void wndProc(Message msg) {
  229.       if (msg.msg == WM_TRAYMOUSEMESSAGE) {
  230.          switch (msg.lParam) {
  231.             case Windows.WM_LBUTTONDBLCLK:
  232.                wmMouseDown(msg, MB_LEFT, 2);
  233.                break;
  234.             case Windows.WM_LBUTTONDOWN:
  235.                wmMouseDown(msg, MB_LEFT, 1);
  236.                break;
  237.             case Windows.WM_LBUTTONUP:
  238.                wmMouseUp(msg, MB_LEFT);
  239.                break;
  240.             case Windows.WM_MBUTTONDBLCLK:
  241.                wmMouseDown(msg, MB_MIDDLE, 2);
  242.                break;
  243.             case Windows.WM_MBUTTONDOWN:
  244.                wmMouseDown(msg, MB_MIDDLE, 1);
  245.                break;
  246.             case Windows.WM_MBUTTONUP:
  247.                wmMouseUp(msg, MB_MIDDLE);
  248.                break;
  249.             case Windows.WM_MOUSEMOVE:
  250.                wmMouseMove(msg);
  251.                break;
  252.             case Windows.WM_RBUTTONDBLCLK:
  253.                wmMouseDown(msg, MB_RIGHT, 2);
  254.                break;
  255.             case Windows.WM_RBUTTONDOWN:
  256.                if (contextMenu != null) {
  257.                   showContextMenu();
  258.                }
  259.                wmMouseDown(msg, MB_RIGHT, 1);
  260.                break;
  261.             case Windows.WM_RBUTTONUP:
  262.                wmMouseUp(msg, MB_RIGHT);
  263.                break;
  264.          }
  265.       }
  266.       else if (msg.msg == Windows.WM_COMMAND) {
  267.          if (0 == msg.lParam)
  268.             if (Command.dispatchID(msg.wParam & 0xFFFF)) return;
  269.       }
  270.    }
  271.  
  272.    private static class TrayIconWindow extends Window {
  273.       com.ms.vm.WeakReference ref;
  274.       TrayIcon rootRef;
  275.  
  276.       TrayIconWindow(TrayIcon control) {
  277.          ref = new com.ms.vm.WeakReference(control);
  278.       }
  279.  
  280.       protected void wndProc(Message m) {
  281.          TrayIcon control = (TrayIcon)ref.getReferent();
  282.          control.wndProc(m);
  283.          super.wndProc(m);
  284.       }
  285.    }
  286.  
  287.    public static class ClassInfo extends Component.ClassInfo {
  288.  
  289.       public static final PropertyInfo ContextMenu =
  290.          new PropertyInfo(TrayIcon.class, "contextMenu", Menu.class,
  291.          new DefaultValueAttribute(null),
  292.          new DescriptionAttribute("The pop-up menu to show when the user right-clicks the icon"));
  293.  
  294.       public static final PropertyInfo Text =
  295.          new PropertyInfo(TrayIcon.class, "text", String.class,
  296.          CategoryAttribute.Appearance,
  297.          new LocalizableAttribute(),
  298.          new DescriptionAttribute("The text that will be displayed when the mouse hovers over the icon."));
  299.  
  300.       public static final PropertyInfo Icon =
  301.          new PropertyInfo(TrayIcon.class, "icon", Icon.class,
  302.          CategoryAttribute.Appearance,
  303.          new LocalizableAttribute(),
  304.          new DescriptionAttribute("The icon to display in the system tray."));
  305.  
  306.       public static final PropertyInfo Visible =
  307.          new PropertyInfo(TrayIcon.class, "visible", Boolean.TYPE,
  308.          CategoryAttribute.Behavior,
  309.          new DefaultValueAttribute(Boolean.FALSE),
  310.          new DescriptionAttribute("Determines whether the control is visible or hidden"));
  311.  
  312.       public static final PropertyInfo Enabled =
  313.          new PropertyInfo( TrayIcon.class, "enabled", Boolean.TYPE,
  314.                            CategoryAttribute.Behavior,
  315.                            new DefaultValueAttribute( null ),
  316.                            new DescriptionAttribute( "Determines whether the control is enabled or disabled" )
  317.                          );
  318.  
  319.       public static final EventInfo OnMouseDown =
  320.          new EventInfo(TrayIcon.class, "mouseDown", MouseEventHandler.class,
  321.          CategoryAttribute.Mouse,
  322.          new DescriptionAttribute("Occurs when a mouse button is pressed"));
  323.  
  324.       public static final EventInfo OnMouseMove =
  325.          new EventInfo(TrayIcon.class, "mouseMove", MouseEventHandler.class,
  326.          CategoryAttribute.Mouse,
  327.          new DescriptionAttribute("Occurs when the mouse is moved"));
  328.  
  329.       public static final EventInfo OnMouseUp =
  330.          new EventInfo(TrayIcon.class, "mouseUp", MouseEventHandler.class,
  331.          CategoryAttribute.Mouse,
  332.          new DescriptionAttribute("Occurs when a mouse button is released"));
  333.  
  334.       public void getAttributes( IAttributes attr ) {
  335.          attr.add( new ShowInToolboxAttribute() );
  336.       }
  337.  
  338.       public String getDefaultPropertyName() {
  339.          return "text";
  340.       }
  341.  
  342.       public String getDefaultEventName() {
  343.          return "mouseDown";
  344.       }
  345.  
  346.       public void getEvents(IEvents events) {
  347.          super.getEvents(events);
  348.          events.add(OnMouseDown);
  349.          events.add(OnMouseMove);
  350.          events.add(OnMouseUp);
  351.       }
  352.  
  353.       public void getProperties(IProperties props) {
  354.          super.getProperties(props);
  355.          props.add(ContextMenu);
  356.          props.add(Enabled);
  357.          props.add(Icon);
  358.          props.add(Text);
  359.          props.add(Visible);
  360.       }
  361.    }
  362. }
  363.