KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > trayicon > ColumbaTrayIcon


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.gui.trayicon;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 import javax.swing.Icon JavaDoc;
23 import javax.swing.JPopupMenu JavaDoc;
24
25 import org.columba.api.gui.frame.IFrameMediator;
26 import org.columba.core.base.OSInfo;
27 import org.columba.core.gui.base.SelfClosingPopupMenu;
28 import org.columba.core.gui.menu.MenuXMLDecoder;
29 import org.columba.core.io.DiskIO;
30 import org.columba.core.logging.Logging;
31 import org.columba.core.resourceloader.ImageLoader;
32 import org.columba.core.shutdown.ShutdownManager;
33
34 /**
35  * Uses the JDIC api to add a tray icon to the system default tray.
36  *
37  * @author Timo Stich <tstich@users.sourceforge.net>
38  */

39 public class ColumbaTrayIcon {
40
41     private IFrameMediator frameMediator;
42
43     /**
44      * Default icon for the TrayIcon.
45      */

46     public static final Icon JavaDoc DEFAULT_ICON = ImageLoader
47             .getMiscIcon("trayicon.png");
48
49     private static ColumbaTrayIcon instance = new ColumbaTrayIcon();
50
51     private JPopupMenu JavaDoc menu;
52
53     private TrayIconInterface activeIcon;
54
55     protected ColumbaTrayIcon() {
56         activeIcon = new DefaultTrayIcon();
57
58     }
59
60     /**
61      * Gets the instance of the ColumbaTrayIcon.
62      *
63      * @return singleton instance
64      */

65     public static ColumbaTrayIcon getInstance() {
66         return instance;
67     }
68
69     /**
70      * Add the tray icon to the default system tray.
71      *
72      */

73     public void addToSystemTray(IFrameMediator frameMediator) {
74
75         initPopupMenu();
76
77         this.frameMediator = frameMediator;
78         activeIcon.addToTray(DEFAULT_ICON, "Columba");
79         activeIcon.setPopupMenu(menu);
80
81         ShutdownManager.getInstance().register(new Runnable JavaDoc() {
82             public void run() {
83                 ColumbaTrayIcon.getInstance().removeFromSystemTray();
84             }
85
86         });
87     }
88
89     /**
90      * Sets the tooltip of the tray icon.
91      *
92      * @param tooltip
93      */

94     public void setTooltip(String JavaDoc tooltip) {
95         activeIcon.setTooltip(tooltip);
96     }
97
98     /**
99      * Sets the icon of the tray icon.
100      *
101      * @param icon
102      */

103     public void setIcon(Icon JavaDoc icon) {
104         activeIcon.setIcon(icon);
105     }
106
107     /**
108      * Removes the tray icon from the system tray.s
109      */

110     public void removeFromSystemTray() {
111         activeIcon.removeFromTray();
112     }
113
114     private void initPopupMenu() {
115         if (menu == null) {
116             menu = new JPopupMenu JavaDoc();
117
118             try {
119                 InputStream JavaDoc is = DiskIO
120                         .getResourceStream("org/columba/core/action/trayiconmenu.xml");
121
122                 menu = new MenuXMLDecoder(frameMediator).createPopupMenu(is);
123             } catch (IOException JavaDoc e) {
124                 e.printStackTrace();
125             }
126
127             // menu.add(new CMenuItem(new OpenNewMailWindowAction(null)));
128
// menu.add(new CMenuItem(new
129
// OpenNewAddressbookWindowAction(null)));
130
// menu.addSeparator();
131
// menu.add(new CMenuItem(new AboutDialogAction(null)));
132
// menu.add(new CMenuItem(new ShowHelpAction(null)));
133
// menu.addSeparator();
134
// menu.add(new CMenuItem(new ExitAction(null)));
135

136             new SelfClosingPopupMenu(menu);
137         }
138     }
139
140     /**
141      * @return Returns the activeIcon.
142      */

143     public TrayIconInterface getActiveIcon() {
144         return activeIcon;
145     }
146
147     /**
148      * @param activeIcon
149      * The activeIcon to set.
150      */

151     public void initActiveIcon() {
152         try {
153             if (OSInfo.isLinux()) {
154                 activeIcon = new JDICTrayIcon();
155             } else if (OSInfo.isWin32Platform()) {
156                 activeIcon = new JDICTrayIcon();
157             } else if (OSInfo.isMac()) {
158                 // tray icon not supported on Mac
159
}
160         } catch (Exception JavaDoc e) {
161             if (Logging.DEBUG)
162                 e.printStackTrace();
163
164             activeIcon = new DefaultTrayIcon();
165         } catch (Error JavaDoc e) {
166             if (Logging.DEBUG)
167                 e.printStackTrace();
168
169             activeIcon = new DefaultTrayIcon();
170         }
171     }
172
173 }
Popular Tags