001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.quicklaunch;
020:
021: import org.lucane.client.*;
022: import org.lucane.client.widgets.*;
023: import org.lucane.common.*;
024:
025: import java.awt.*;
026: import java.awt.event.*;
027: import java.util.*;
028:
029: import javax.swing.*;
030:
031: public class QuickLaunch extends StandalonePlugin implements
032: ActionListener, MouseListener {
033: private static final String MAIN_INTERFACE = "org.lucane.applications.maininterface";
034:
035: private TrayIcon trayIcon;
036:
037: /**
038: * Void contructor. Used by PluginManager
039: */
040: public QuickLaunch() {
041: }
042:
043: /**
044: * @param friends the Client the Plugin belongs to
045: * @param starter is true if the Clients started the plugin (start() will be
046: * called instead of follow()
047: * @return a new instance of the Plugin.
048: */
049: public Plugin newInstance(ConnectInfo[] friends) {
050: QuickLaunch self = new QuickLaunch();
051:
052: try {
053: self.trayIcon = new TrayIcon(
054: this .getImageIcon(getIcon16()), Client
055: .getInstance().getMyInfos().getName()
056: + " - Lucane Groupware");
057: } catch (Throwable t) {
058: self.trayIcon = null;
059: }
060:
061: return self;
062: }
063:
064: /**
065: * Show a dialog asking for the friend name
066: */
067: public void start() {
068: if (this .trayIcon == null) {
069: //no user message if we aren't on windows
070: if (System.getProperty("os.name").startsWith("Win"))
071: DialogBox.error(tr("err.noTray"));
072: else
073: Logging
074: .getLogger()
075: .info(
076: "Not on windows, running MainInterface instead of QuickLaunch");
077:
078: PluginManager.getInstance().run(MAIN_INTERFACE,
079: new ConnectInfo[0]);
080: Client.getInstance().setStartupPlugin(MAIN_INTERFACE);
081: return;
082: }
083:
084: addMenuToTray();
085:
086: this .trayIcon.addMouseListener(this );
087: this .trayIcon.setVisible(true);
088:
089: //show the message only once
090: if (getLocalConfig().getInt("show-popup", 1) != 0) {
091: this .trayIcon.showInfo(tr("lucane.is.ready"),
092: "Lucane Groupware");
093: getLocalConfig().set("show-popup", 0);
094: }
095: }
096:
097: public void actionPerformed(ActionEvent ae) {
098: if (ae.getSource() instanceof JMenuItem) {
099: JMenuItem src = (JMenuItem) ae.getSource();
100:
101: if (src.getName().equals("exit")) {
102: cleanExit();
103: return;
104: }
105:
106: if (src.getName().equals(MAIN_INTERFACE))
107: showMainInterface();
108: else
109: runPlugin(src.getName());
110: } else
111: showMainInterface();
112: }
113:
114: public void mouseClicked(MouseEvent e) {
115: }
116:
117: public void mouseEntered(MouseEvent e) {
118: }
119:
120: public void mouseExited(MouseEvent e) {
121: }
122:
123: public void mouseReleased(MouseEvent e) {
124: }
125:
126: public void mousePressed(MouseEvent e) {
127: if (e.getClickCount() == 2)
128: showMainInterface();
129: }
130:
131: private void addMenuToTray() {
132: HashMap categories = new HashMap();
133:
134: //create menu list
135: Iterator plugins = PluginManager.getInstance()
136: .getAvailablePlugins();
137: plugins = PluginComparator.sortPlugins(plugins);
138:
139: while (plugins.hasNext()) {
140: Plugin p = (Plugin) plugins.next();
141:
142: JMenu category = (JMenu) categories.get(p.getCategory());
143: if (category == null) {
144: category = new JMenu(p.getCategory());
145: categories.put(p.getCategory(), category);
146:
147: //copy menu to real one
148: if (!p.getCategory().equals("Invisible"))
149: this .trayIcon.add(category);
150: }
151:
152: ImageIcon icon = new ImageIcon();
153: try {
154: icon = p.getImageIcon(p.getIcon());
155: icon = new ImageIcon(icon.getImage().getScaledInstance(
156: 16, 16, Image.SCALE_SMOOTH));
157: } catch (Exception e) {
158: //error at icon loading
159: }
160:
161: JMenuItem menu = new JMenuItem(p.getTitle(), icon);
162: menu.setName(p.getName());
163: menu.setToolTipText(p.getToolTip());
164: menu.addActionListener(this );
165: category.add(menu);
166: }
167:
168: this .trayIcon.addSeparator();
169:
170: //windows
171: this .trayIcon.add(new WindowMenu(this ));
172:
173: //open main interface
174: JMenuItem menu = new JMenuItem(tr("open"));
175: menu.setName("org.lucane.applications.maininterface");
176: menu.addActionListener(this );
177: this .trayIcon.add(menu);
178:
179: //exit
180: menu = new JMenuItem(tr("exit"));
181: menu.setName("exit");
182: menu.addActionListener(this );
183: this .trayIcon.add(menu);
184: }
185:
186: private void runPlugin(String pluginName) {
187: ConnectInfo[] friends = null;
188: Plugin plugin = PluginManager.getInstance().getPlugin(
189: pluginName);
190:
191: // get users
192: if (plugin.isStandalone())
193: friends = new ConnectInfo[0];
194: else {
195: ListBox userList = new ListBox(null, plugin.getTitle(),
196: tr("msg.selectUsers"), Client.getInstance()
197: .getUserList());
198: Object[] users = userList.selectItems();
199: if (users != null) {
200: friends = new ConnectInfo[users.length];
201: for (int i = 0; i < friends.length; i++)
202: friends[i] = Communicator.getInstance()
203: .getConnectInfo((String) users[i]);
204: }
205: }
206:
207: // run the plugin if the user didn't click on cancel
208: if (friends != null)
209: PluginManager.getInstance().run(pluginName, friends);
210: }
211:
212: private boolean activatePlugin(String pluginName) {
213: boolean activated = false;
214:
215: Iterator plugins = PluginManager.getInstance()
216: .getRunningPlugins();
217: while (plugins.hasNext()) {
218: Plugin plugin = (Plugin) plugins.next();
219: if (!plugin.getName().equals(pluginName))
220: continue;
221:
222: Iterator windows = Client.getInstance().getWindowManager()
223: .getWindowsFor(plugin);
224: if (!windows.hasNext())
225: continue;
226:
227: while (windows.hasNext()) {
228: ManagedWindow window = (ManagedWindow) windows.next();
229: window.show();
230: }
231: activated = true;
232: }
233:
234: return activated;
235: }
236:
237: private void showMainInterface() {
238: if (!activatePlugin(MAIN_INTERFACE))
239: runPlugin(MAIN_INTERFACE);
240: }
241:
242: private void cleanExit() {
243: Logging.getLogger().finer("QuickLaunch::cleanExit()");
244: this .trayIcon.setVisible(false);
245: exit();
246: }
247: }
|