KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > maininterface > MainInterface


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 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  
20 package org.lucane.applications.maininterface;
21
22 import org.lucane.client.*;
23 import org.lucane.client.widgets.*;
24 import org.lucane.common.*;
25 import org.lucane.common.net.ObjectConnection;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.io.IOException JavaDoc;
30 import java.util.*;
31 import java.util.List JavaDoc;
32 import javax.swing.*;
33
34 public class MainInterface
35   extends StandalonePlugin
36   implements ActionListener,
37              UserListListener
38 {
39   private final int NB_BUTTONS = 8;
40
41   private ConnectInfo service;
42   
43   /* plugins... */
44   private Vector buttons;
45   private Vector names;
46
47   /* client components */
48   private Client parent;
49   private Communicator commu;
50   private PluginManager ploader;
51
52   /* dialog components */
53   private ManagedWindow window;
54   private JPanel pnlUsers;
55   private JPanel pnlPlugins;
56   private JComboBox cmbGroups;
57   private JList lstUsers;
58   private JMenuBar mnuBar;
59   private JMenu mnuMain;
60   private JMenu mnuExtends;
61   private JMenuItem mnuExit;
62
63   /**
64    * Void contructor. Used by PluginManager
65    */

66   public MainInterface()
67   {
68   }
69
70   /**
71    * @param friends the Client the Plugin belongs to
72    * @return a new instance of the Plugin.
73    */

74   public Plugin newInstance(ConnectInfo[] friends)
75   {
76     return new MainInterface();
77   }
78
79
80   /**
81    * Show a dialog asking for the friend name
82    */

83   public void start()
84   {
85     this.names = new Vector();
86     this.buttons = new Vector();
87     this.commu = Communicator.getInstance();
88     this.parent = Client.getInstance();
89     this.ploader = PluginManager.getInstance();
90
91     this.service = commu.getConnectInfo(this.getName());
92     
93     window = new ManagedWindow(this, "Lucane - " + parent.getMyInfos().getName());
94     window.setName("maininterface");
95     
96     window.getContentPane().setLayout(new BorderLayout());
97     //window.addWindowListener(new PluginExitWindowListener(this));
98
mnuBar = new JMenuBar();
99     mnuMain = new JMenu("Lucane");
100     mnuExtends = new JMenu(tr("applications"));
101     mnuBar.add(mnuMain);
102     mnuBar.add(mnuExtends);
103     mnuBar.add(new WindowMenu(this));
104     window.setJMenuBar(mnuBar);
105     mnuExit = new JMenuItem(tr("quit"));
106     mnuExit.addActionListener(this);
107     mnuMain.add(mnuExit);
108
109     JPanel pnlButtons = new JPanel();
110     pnlUsers = new JPanel();
111     pnlPlugins = new JPanel();
112     pnlButtons.setLayout(new BorderLayout());
113     pnlPlugins.setLayout(new GridLayout(0, 1));
114     pnlUsers.setLayout(new BorderLayout());
115     window.getContentPane().add(pnlUsers, BorderLayout.CENTER);
116     window.getContentPane().add(pnlButtons, BorderLayout.EAST);
117     pnlButtons.add(pnlPlugins, BorderLayout.NORTH);
118     cmbGroups = new JComboBox();
119     lstUsers = new JList();
120     pnlUsers.add(cmbGroups, BorderLayout.NORTH);
121     pnlUsers.add(new JScrollPane(lstUsers), BorderLayout.CENTER);
122     
123     initGroupCombo();
124     
125
126     List JavaDoc list = parent.getUserList();
127     lstUsers.setListData(list.toArray());
128
129     displayAppsInMenu();
130     displayAppsAsButtons();
131
132     parent.addUserListListener(this);
133     
134     window.setPreferredSize(new Dimension(350, 400));
135     window.setExitPluginOnClose(true);
136     window.show();
137   }
138   
139   private void initGroupCombo()
140   {
141     cmbGroups.addItem(tr("everyone"));
142     try {
143         Iterator i = getMyGroups().iterator();
144         while(i.hasNext())
145             cmbGroups.addItem(i.next());
146     } catch (Exception JavaDoc e) {
147         DialogBox.error(tr("err.unableToFetchGroups"));
148     }
149     cmbGroups.addActionListener(this);
150   }
151   
152   private void displayAppsInMenu()
153   {
154     Iterator plugins = ploader.getAvailablePlugins();
155     plugins = PluginComparator.sortPlugins(plugins);
156     
157     while(plugins.hasNext())
158     {
159         Plugin p = (Plugin)plugins.next();
160         
161         // add to the Applications menu
162
if(! p.getCategory().equals("Invisible"))
163         {
164             int j;
165             ImageIcon icon = null;
166
167             //resize icon
168
try {
169                 icon = p.getImageIcon(p.getIcon());
170                 Logging.getLogger().finer("ICON: " + p.getDirectory() + p.getIcon());
171                 icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
172             } catch(Exception JavaDoc e) {
173                 DialogBox.error(tr("error") + e);
174             }
175
176             //add to the correct category
177
for(j = 0; j < mnuExtends.getMenuComponentCount(); j++)
178             {
179                 JMenu menu = (JMenu)mnuExtends.getMenuComponent(j);
180
181                 if(menu.getText().equals(p.getCategory()))
182                 {
183                     JMenuItem menuItem = new JMenuItem(p.getTitle(), icon);
184                     menuItem.setToolTipText(p.getToolTip());
185                     menuItem.addActionListener(this);
186                     menu.add(menuItem);
187                     buttons.addElement(menuItem);
188                     names.addElement(p.getName());
189                     break;
190                 }
191             }
192
193             //this cat doesn't exist yet, let's create it
194
if(j == mnuExtends.getMenuComponentCount())
195             {
196                 JMenu jm = new JMenu(p.getCategory());
197                 JMenuItem jmi = new JMenuItem(p.getTitle(), icon);
198                 jmi.addActionListener(this);
199                 jm.add(jmi);
200                 buttons.addElement(jmi);
201                 names.addElement(p.getName());
202                 mnuExtends.add(jm);
203             }
204         }
205     }
206   }
207   
208   private void displayAppsAsButtons()
209   {
210     //-- sort and display apps in buttons
211
Iterator plugins = ploader.getAvailablePlugins();
212     ArrayList pluginUse = new ArrayList();
213     while(plugins.hasNext())
214     {
215         Plugin p = (Plugin)plugins.next();
216         pluginUse.add(new PluginUse(p, getLocalConfig().get(p.getName())));
217     }
218     Collections.sort(pluginUse, Collections.reverseOrder());
219       
220     int nbButtons = NB_BUTTONS;
221     for(int i=0;i<nbButtons && i<pluginUse.size(); i++)
222     {
223         Plugin p = ((PluginUse)pluginUse.get(i)).getPlugin();
224         if(! p.getCategory().equals("Invisible"))
225         {
226             ImageIcon iic = p.getImageIcon(p.getIcon());
227     
228             JButton mybutton = new JButton(iic);
229             mybutton.addActionListener(this);
230             buttons.addElement(mybutton);
231             names.addElement(p.getName());
232             mybutton.setHorizontalAlignment(SwingConstants.LEFT);
233             mybutton.setText(p.getTitle());
234             mybutton.setToolTipText(p.getToolTip());
235             pnlPlugins.add(mybutton);
236         }
237         else
238             nbButtons++;
239     }
240   }
241
242   public void userListChanged(List JavaDoc logins)
243   {
244     refreshUserList();
245   }
246   
247   private void refreshUserList()
248   {
249     if(cmbGroups.getSelectedIndex() <= 0)
250         lstUsers.setListData(parent.getUserList().toArray());
251     else
252     {
253         try {
254             ArrayList groups = getUsersForGroup(cmbGroups.getSelectedItem().toString());
255             lstUsers.setListData(groups.toArray());
256         } catch(Exception JavaDoc e) {
257             DialogBox.error(tr("err.unableToFetchUsersForGroup"));
258             lstUsers.setListData(parent.getUserList().toArray());
259         }
260     }
261   }
262
263
264   public void actionPerformed(ActionEvent ae)
265   {
266     if(ae.getSource() == cmbGroups)
267     {
268         refreshUserList();
269         return;
270     }
271
272     if(ae.getSource() == mnuExit)
273     {
274       this.window.dispose();
275       cleanExit();
276       return;
277     }
278
279     String JavaDoc name = "";
280     for(int i = 0; i < buttons.size(); i++)
281     {
282       if(ae.getSource() == buttons.elementAt(i))
283       {
284         name = (String JavaDoc)names.elementAt(i);
285         break;
286       }
287     }
288
289     /* fetch users */
290     Object JavaDoc[] lstv = lstUsers.getSelectedValues();
291     ConnectInfo[] cis = new ConnectInfo[lstv.length];
292
293     for(int i = 0; i < lstv.length; i++)
294     {
295         Logging.getLogger().finer("Selected: " + (String JavaDoc)lstv[i]);
296       cis[i] = commu.getConnectInfo((String JavaDoc)lstv[i]);
297     }
298
299     //-- increment use counter
300
int currentUse = 0;
301     try {
302         currentUse = Integer.parseInt(this.getLocalConfig().get(name));
303     } catch(Exception JavaDoc e) {
304         //nothing, not yet used
305
}
306     this.getLocalConfig().set(name, String.valueOf(currentUse+1));
307
308     Logging.getLogger().finer("PluginToLoad: " + name);
309     ploader.run(name, cis);
310   }
311   
312   private ArrayList getMyGroups()
313   throws IOException JavaDoc, ClassNotFoundException JavaDoc
314   {
315     MainInterfaceAction action = new MainInterfaceAction(MainInterfaceAction.GET_MY_GROUPS);
316     ObjectConnection oc = commu.sendMessageTo(service, service.getName(), action);
317     ArrayList groups = (ArrayList)oc.read();
318     oc.close();
319     
320     return groups;
321   }
322   
323   private ArrayList getUsersForGroup(String JavaDoc group)
324   throws IOException JavaDoc, ClassNotFoundException JavaDoc
325   {
326     MainInterfaceAction action = new MainInterfaceAction(
327             MainInterfaceAction.GET_CONNECTED_USERS_FOR_GROUP, group);
328     ObjectConnection oc = commu.sendMessageTo(service, service.getName(), action);
329     ArrayList users = (ArrayList)oc.read();
330     oc.close();
331     
332     return users;
333   }
334   
335   public void windowClosing(WindowEvent we)
336   {
337     cleanExit();
338   }
339
340   private void cleanExit()
341   {
342     Logging.getLogger().finer("MainInterface::cleanExit()");
343     //WidgetState.save(getLocalConfig(), window);
344
parent.removeUserListListener(this);
345     exit();
346   }
347 }
348
Popular Tags