001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.gui.trayicon;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021:
022: import javax.swing.Icon;
023: import javax.swing.JPopupMenu;
024:
025: import org.columba.api.gui.frame.IFrameMediator;
026: import org.columba.core.base.OSInfo;
027: import org.columba.core.gui.base.SelfClosingPopupMenu;
028: import org.columba.core.gui.menu.MenuXMLDecoder;
029: import org.columba.core.io.DiskIO;
030: import org.columba.core.logging.Logging;
031: import org.columba.core.resourceloader.ImageLoader;
032: import org.columba.core.shutdown.ShutdownManager;
033:
034: /**
035: * Uses the JDIC api to add a tray icon to the system default tray.
036: *
037: * @author Timo Stich <tstich@users.sourceforge.net>
038: */
039: public class ColumbaTrayIcon {
040:
041: private IFrameMediator frameMediator;
042:
043: /**
044: * Default icon for the TrayIcon.
045: */
046: public static final Icon DEFAULT_ICON = ImageLoader
047: .getMiscIcon("trayicon.png");
048:
049: private static ColumbaTrayIcon instance = new ColumbaTrayIcon();
050:
051: private JPopupMenu menu;
052:
053: private TrayIconInterface activeIcon;
054:
055: protected ColumbaTrayIcon() {
056: activeIcon = new DefaultTrayIcon();
057:
058: }
059:
060: /**
061: * Gets the instance of the ColumbaTrayIcon.
062: *
063: * @return singleton instance
064: */
065: public static ColumbaTrayIcon getInstance() {
066: return instance;
067: }
068:
069: /**
070: * Add the tray icon to the default system tray.
071: *
072: */
073: public void addToSystemTray(IFrameMediator frameMediator) {
074:
075: initPopupMenu();
076:
077: this .frameMediator = frameMediator;
078: activeIcon.addToTray(DEFAULT_ICON, "Columba");
079: activeIcon.setPopupMenu(menu);
080:
081: ShutdownManager.getInstance().register(new Runnable() {
082: public void run() {
083: ColumbaTrayIcon.getInstance().removeFromSystemTray();
084: }
085:
086: });
087: }
088:
089: /**
090: * Sets the tooltip of the tray icon.
091: *
092: * @param tooltip
093: */
094: public void setTooltip(String tooltip) {
095: activeIcon.setTooltip(tooltip);
096: }
097:
098: /**
099: * Sets the icon of the tray icon.
100: *
101: * @param icon
102: */
103: public void setIcon(Icon 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();
117:
118: try {
119: InputStream is = DiskIO
120: .getResourceStream("org/columba/core/action/trayiconmenu.xml");
121:
122: menu = new MenuXMLDecoder(frameMediator)
123: .createPopupMenu(is);
124: } catch (IOException e) {
125: e.printStackTrace();
126: }
127:
128: // menu.add(new CMenuItem(new OpenNewMailWindowAction(null)));
129: // menu.add(new CMenuItem(new
130: // OpenNewAddressbookWindowAction(null)));
131: // menu.addSeparator();
132: // menu.add(new CMenuItem(new AboutDialogAction(null)));
133: // menu.add(new CMenuItem(new ShowHelpAction(null)));
134: // menu.addSeparator();
135: // menu.add(new CMenuItem(new ExitAction(null)));
136:
137: new SelfClosingPopupMenu(menu);
138: }
139: }
140:
141: /**
142: * @return Returns the activeIcon.
143: */
144: public TrayIconInterface getActiveIcon() {
145: return activeIcon;
146: }
147:
148: /**
149: * @param activeIcon
150: * The activeIcon to set.
151: */
152: public void initActiveIcon() {
153: try {
154: if (OSInfo.isLinux()) {
155: activeIcon = new JDICTrayIcon();
156: } else if (OSInfo.isWin32Platform()) {
157: activeIcon = new JDICTrayIcon();
158: } else if (OSInfo.isMac()) {
159: // tray icon not supported on Mac
160: }
161: } catch (Exception e) {
162: if (Logging.DEBUG)
163: e.printStackTrace();
164:
165: activeIcon = new DefaultTrayIcon();
166: } catch (Error e) {
167: if (Logging.DEBUG)
168: e.printStackTrace();
169:
170: activeIcon = new DefaultTrayIcon();
171: }
172: }
173:
174: }
|