KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > quicklaunch > QuickLaunch


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.quicklaunch;
20
21 import org.lucane.client.*;
22 import org.lucane.client.widgets.*;
23 import org.lucane.common.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.util.*;
28
29 import javax.swing.*;
30
31
32 public class QuickLaunch
33   extends StandalonePlugin
34   implements ActionListener, MouseListener
35 {
36     private static final String JavaDoc MAIN_INTERFACE = "org.lucane.applications.maininterface";
37     
38   private TrayIcon trayIcon;
39   
40   /**
41    * Void contructor. Used by PluginManager
42    */

43   public QuickLaunch()
44   {
45   }
46
47   /**
48    * @param friends the Client the Plugin belongs to
49    * @param starter is true if the Clients started the plugin (start() will be
50    * called instead of follow()
51    * @return a new instance of the Plugin.
52    */

53   public Plugin newInstance(ConnectInfo[] friends)
54   {
55     QuickLaunch self = new QuickLaunch();
56     
57     try {
58         self.trayIcon = new TrayIcon(this.getImageIcon(getIcon16()),
59                 Client.getInstance().getMyInfos().getName() + " - Lucane Groupware");
60     } catch (Throwable JavaDoc t) {
61         self.trayIcon = null;
62     }
63     
64     return self;
65   }
66
67
68   /**
69    * Show a dialog asking for the friend name
70    */

71   public void start()
72   {
73     if(this.trayIcon == null)
74     {
75         //no user message if we aren't on windows
76
if(System.getProperty("os.name").startsWith("Win"))
77             DialogBox.error(tr("err.noTray"));
78         else
79             Logging.getLogger().info("Not on windows, running MainInterface instead of QuickLaunch");
80         
81         PluginManager.getInstance().run(MAIN_INTERFACE, new ConnectInfo[0]);
82         Client.getInstance().setStartupPlugin(MAIN_INTERFACE);
83         return;
84     }
85     
86     addMenuToTray();
87     
88     this.trayIcon.addMouseListener(this);
89     this.trayIcon.setVisible(true);
90     
91     //show the message only once
92
if(getLocalConfig().getInt("show-popup", 1) != 0)
93     {
94         this.trayIcon.showInfo(tr("lucane.is.ready"), "Lucane Groupware");
95         getLocalConfig().set("show-popup", 0);
96     }
97   }
98
99   public void actionPerformed(ActionEvent ae)
100   {
101     if(ae.getSource() instanceof JMenuItem)
102     {
103         JMenuItem src = (JMenuItem)ae.getSource();
104     
105         if(src.getName().equals("exit"))
106         {
107             cleanExit();
108             return;
109         }
110     
111         if(src.getName().equals(MAIN_INTERFACE))
112             showMainInterface();
113         else
114             runPlugin(src.getName());
115     }
116     else
117         showMainInterface();
118   }
119   
120   public void mouseClicked(MouseEvent e) {}
121   public void mouseEntered(MouseEvent e) {}
122   public void mouseExited(MouseEvent e) {}
123   public void mouseReleased(MouseEvent e) {}
124   public void mousePressed(MouseEvent e)
125   {
126     if(e.getClickCount() == 2)
127         showMainInterface();
128   }
129   
130   
131   private void addMenuToTray()
132   {
133     HashMap categories = new HashMap();
134     
135     //create menu list
136
Iterator plugins = PluginManager.getInstance().getAvailablePlugins();
137     plugins = PluginComparator.sortPlugins(plugins);
138     
139     while(plugins.hasNext())
140     {
141         Plugin p = (Plugin)plugins.next();
142
143         JMenu category = (JMenu)categories.get(p.getCategory());
144         if(category == null)
145         {
146             category = new JMenu(p.getCategory());
147             categories.put(p.getCategory(), category);
148             
149             //copy menu to real one
150
if(!p.getCategory().equals("Invisible"))
151                 this.trayIcon.add(category);
152         }
153         
154         ImageIcon icon = new ImageIcon();
155         try {
156             icon = p.getImageIcon(p.getIcon());
157             icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
158         } catch(Exception JavaDoc e) {
159             //error at icon loading
160
}
161         
162         JMenuItem menu = new JMenuItem(p.getTitle(), icon);
163         menu.setName(p.getName());
164         menu.setToolTipText(p.getToolTip());
165         menu.addActionListener(this);
166         category.add(menu);
167     }
168     
169     this.trayIcon.addSeparator();
170     
171     //windows
172
this.trayIcon.add(new WindowMenu(this));
173     
174     //open main interface
175
JMenuItem menu = new JMenuItem(tr("open"));
176     menu.setName("org.lucane.applications.maininterface");
177     menu.addActionListener(this);
178     this.trayIcon.add(menu);
179     
180     //exit
181
menu = new JMenuItem(tr("exit"));
182     menu.setName("exit");
183     menu.addActionListener(this);
184     this.trayIcon.add(menu);
185   }
186
187   private void runPlugin(String JavaDoc pluginName)
188   {
189     ConnectInfo[] friends = null;
190     Plugin plugin = PluginManager.getInstance().getPlugin(pluginName);
191     
192     // get users
193
if(plugin.isStandalone())
194         friends = new ConnectInfo[0];
195     else
196     {
197         ListBox userList = new ListBox(null, plugin.getTitle(), tr("msg.selectUsers"), Client.getInstance().getUserList());
198         Object JavaDoc[] users = userList.selectItems();
199         if(users != null)
200         {
201             friends = new ConnectInfo[users.length];
202             for(int i=0;i<friends.length;i++)
203                 friends[i] = Communicator.getInstance().getConnectInfo((String JavaDoc)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 JavaDoc pluginName)
213   {
214     boolean activated = false;
215     
216     Iterator plugins = PluginManager.getInstance().getRunningPlugins();
217     while(plugins.hasNext())
218     {
219         Plugin plugin = (Plugin)plugins.next();
220         if(!plugin.getName().equals(pluginName))
221             continue;
222         
223         Iterator windows = Client.getInstance().getWindowManager().getWindowsFor(plugin);
224         if(!windows.hasNext())
225             continue;
226         
227         while(windows.hasNext())
228         {
229             ManagedWindow window = (ManagedWindow)windows.next();
230             window.show();
231         }
232         activated = true;
233     }
234     
235     return activated;
236   }
237   
238   private void showMainInterface()
239   {
240     if(!activatePlugin(MAIN_INTERFACE))
241         runPlugin(MAIN_INTERFACE);
242   }
243   
244   private void cleanExit()
245   {
246     Logging.getLogger().finer("QuickLaunch::cleanExit()");
247     this.trayIcon.setVisible(false);
248     exit();
249   }
250 }
251
Popular Tags