View Javadoc

1   /*
2    * @(#)$Id: SystemTray.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   Adds an icon to the system tray.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Dec 17, 2007		crocimi		Initial Release
15   * Jan 14, 2008     bsigner     New icon loading mechanism
16   * Jan 20, 2008     bsigner     Update to new GUI bundle functionality
17   *
18   * -----------------------------------------------------------------------
19   *
20   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
21   *
22   * This software is the proprietary information of ETH Zurich.
23   * Use is subject to license terms.
24   * 
25   */
26  
27  
28  package org.ximtec.igesture.geco.util;
29  
30  import java.awt.AWTException;
31  import java.awt.Frame;
32  import java.awt.Image;
33  import java.awt.MenuItem;
34  import java.awt.PopupMenu;
35  import java.awt.TrayIcon;
36  import java.awt.event.ActionEvent;
37  import java.awt.event.ActionListener;
38  import java.awt.event.MouseEvent;
39  import java.awt.event.MouseListener;
40  
41  import org.ximtec.igesture.geco.gui.MainView;
42  
43  
44  /**
45   * Adds an icon to the system tray.
46   * @version 0.9, Dec 2007
47   * @author Michele Croci, mcroci@gmail.com
48   * @author Beat Signer, signer@inf.ethz.ch
49   */
50  public class SystemTray {
51  
52     private MainView mainView;
53     private static TrayIcon trayIcon;
54     private PopupMenu popup;
55     private Image image;
56     private static java.awt.SystemTray tray;
57  
58  
59     public SystemTray(MainView view) {
60        this.mainView = view;
61        addIconToTray();
62     }
63  
64  
65     public void addIconToTray() {
66        if (java.awt.SystemTray.isSupported()) {
67           tray = java.awt.SystemTray.getSystemTray();
68           image = GuiBundleTool.getBundle().getSmallIcon(GuiBundleTool.KEY)
69                 .getImage();
70           popup = new PopupMenu();
71           trayIcon = new TrayIcon(image, GuiBundleTool.getBundle()
72                 .getShortDescription(GuiBundleTool.KEY), popup);
73           MenuItem exitItem = new MenuItem(Constant.EXIT);
74           exitItem.addActionListener(new ActionListener() {
75  
76              public void actionPerformed(ActionEvent e) {
77                 tray.remove(trayIcon);
78              }
79           });
80  
81           exitItem.addActionListener(mainView.getActionHandler()
82                 .getExitApplicationAction());
83           popup.add(exitItem);
84           MenuItem showItem = new MenuItem(GuiBundleTool.getBundle().getName(
85                 Constant.SHOW_APPLICATION_MENU_ITEM));
86           showItem.addActionListener(new ActionListener() {
87  
88              public void actionPerformed(ActionEvent e) {
89                 mainView.setVisible(true);
90              }
91           });
92  
93           popup.add(showItem);
94           popup.add(exitItem);
95           trayIcon.setImageAutoSize(true);
96           trayIcon.addMouseListener(new IconMouseListener());
97  
98           try {
99              tray.add(trayIcon);
100          }
101          catch (AWTException e) {
102             e.printStackTrace();
103          }
104 
105       }
106       else {
107          trayIcon = null;
108       }
109 
110    } // addIconToTray
111 
112    private class IconMouseListener implements MouseListener {
113 
114       /**
115        * Handles an entering of the cursor in the button area.
116        * @param event associated mouse event.
117        */
118       public void mouseEntered(MouseEvent event) {
119       } // mouseEntered
120 
121 
122       /**
123        * Handles a leaving of the cursor from the button area.
124        * @param event associated mouse event.
125        */
126       public void mouseExited(MouseEvent event) {
127       } // mouseExited
128 
129 
130       /**
131        * Handles a clicked button.
132        * @param event associated mouse event.
133        */
134       public void mouseClicked(MouseEvent event) {
135          if (event.getClickCount() == 2) {
136 
137             if (mainView.isVisible()) {
138                mainView.setVisible(false);
139             }
140             else {
141                mainView.setVisible(true);
142                mainView.setState(Frame.NORMAL);
143                mainView.requestFocus();
144                mainView.toFront();
145             }
146 
147          }
148 
149       } // mouseClicked
150 
151 
152       /**
153        * Handles a pressed button.
154        * @param event the associated mouse event.
155        */
156       public void mousePressed(MouseEvent event) {
157       } // mousePressed
158 
159 
160       /**
161        * Handles a released button.
162        * @param event the associated mouse event.
163        */
164       public void mouseReleased(MouseEvent event) {
165       } // mouseReleased
166 
167    }
168 
169 
170    public static void removeTrayIcon() {
171       tray.remove(trayIcon);
172    } // removeTrayIcon
173 
174 }