001: /**********************************************************************************
002:
003: Feedzeo!
004: A free and open source RSS/Atom/RDF feed aggregator
005:
006: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
021:
022: ************************************************************************************/package app;
023:
024: import javax.swing.*;
025: import java.awt.*;
026: import java.awt.event.*;
027: import java.util.*;
028:
029: import util.*;
030:
031: public class AppSysTray implements ActionListener {
032:
033: private final String IconFile = "feedzeo_tray.gif";
034: private final String TrayCaption = "Feedzeo!";
035:
036: // variable to track double click event
037: long prevEventTime;
038: long DBLCLICK_TIME = 1000; // in ms
039:
040: private ImageIcon icon;
041: private JPopupMenu pmenu = null;
042: private SysTrayItem trayItem;
043: private Vector sysTrayListeners;
044:
045: private JPopupMenu createPopupMenu(String[] itemNames) {
046:
047: JPopupMenu menu = null;
048: JMenuItem menuItem = null;
049:
050: menu = new JPopupMenu("Feedzeo");
051:
052: for (int i = 0; i < itemNames.length; i++) {
053: menuItem = new JMenuItem(itemNames[i]);
054: menuItem.addActionListener(this );
055: menu.add(menuItem);
056: }
057:
058: return menu;
059: }
060:
061: public AppSysTray(String[] menuItemNames) {
062: icon = new ImageIcon(IconFile);
063: if (menuItemNames != null) {
064: pmenu = createPopupMenu(menuItemNames);
065: trayItem = new SysTrayItem(icon, TrayCaption, pmenu);
066: } else
067: trayItem = new SysTrayItem(icon, TrayCaption);
068:
069: trayItem.addActionListener(this );
070: trayItem.attachToSystemTray();
071:
072: prevEventTime = 0;
073: sysTrayListeners = new Vector();
074: }
075:
076: private boolean doubleClick(ActionEvent e) {
077: String cmd = e.getActionCommand();
078: boolean dblClick = false;
079: long currEventTime = 0;
080:
081: if (cmd.equalsIgnoreCase("PressAction")) { // jdic sets the cmdstr to "PressAction"
082: currEventTime = e.getWhen();
083: if ((currEventTime - prevEventTime) <= DBLCLICK_TIME)
084: dblClick = true;
085: }
086: prevEventTime = currEventTime;
087: return dblClick;
088: }
089:
090: public void actionPerformed(ActionEvent e) {
091:
092: if ((e.getSource()) instanceof JMenuItem) {
093: // notify listeners that a menu event has occured
094: for (int i = 0; i < sysTrayListeners.size(); i++) {
095: AppSysTrayEvtListener evtListener = (AppSysTrayEvtListener) sysTrayListeners
096: .elementAt(i);
097: evtListener.handleSysTrayEvent(new AppSysTrayEvt(
098: AppSysTrayEvt.MENUITEM_CLICK), (JMenuItem) e
099: .getSource(), pmenu);
100: }
101: } else if (doubleClick(e)) {
102: // notify listeners that a dblclk has occured
103: for (int i = 0; i < sysTrayListeners.size(); i++) {
104: AppSysTrayEvtListener evtListener = (AppSysTrayEvtListener) sysTrayListeners
105: .elementAt(i);
106: evtListener.handleSysTrayEvent(new AppSysTrayEvt(
107: AppSysTrayEvt.DOUBLE_CLICK), null, null);
108: }
109:
110: }
111: }
112:
113: public void addEventListener(AppSysTrayEvtListener lnr) {
114: sysTrayListeners.add(lnr);
115: }
116:
117: }
|