KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.lucane.applications.quicklaunch;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.swing.JMenu JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import javax.swing.event.MenuEvent JavaDoc;
30 import javax.swing.event.MenuListener JavaDoc;
31
32 import org.lucane.client.Client;
33 import org.lucane.client.Plugin;
34 import org.lucane.client.PluginManager;
35 import org.lucane.client.widgets.ManagedWindow;
36
37 public class WindowMenu extends JMenu JavaDoc
38 implements MenuListener JavaDoc, ActionListener JavaDoc
39 {
40     private Plugin plugin;
41     private HashMap JavaDoc toggleButtons;
42     
43     public WindowMenu(Plugin plugin)
44     {
45         super(plugin.tr("mnu.windows"));
46         this.plugin = plugin;
47         
48         this.toggleButtons = new HashMap JavaDoc();
49         this.addMenuListener(this);
50     }
51     
52     public void menuCanceled(MenuEvent JavaDoc e) {}
53     public void menuDeselected(MenuEvent JavaDoc e) {}
54     public void menuSelected(MenuEvent JavaDoc e)
55     {
56         //create the menu
57
toggleButtons.clear();
58         this.removeAll();
59         Iterator JavaDoc plugins = PluginManager.getInstance().getRunningPlugins();
60         while(plugins.hasNext())
61         {
62             Plugin plugin = (Plugin)plugins.next();
63             
64             //zap startup plugin
65
if(plugin.getName().equals(Client.getInstance().getStartupPlugin()))
66                 continue;
67
68             Iterator JavaDoc windows = Client.getInstance().getWindowManager().getWindowsFor(plugin);
69             
70             //if the plugin has no window, we don't care
71
if(!windows.hasNext())
72                 continue;
73             
74             JMenu JavaDoc menu = new JMenu JavaDoc(plugin.getTitle());
75             
76             //showAll/hideAll
77
JMenuItem JavaDoc toggle = new JMenuItem JavaDoc(tr("mnu.hide"));
78             toggleButtons.put(toggle, plugin);
79             if(isHidden(plugin))
80                 toggle.setText(tr("mnu.show"));
81             toggle.addActionListener(this);
82             menu.add(toggle);
83             menu.addSeparator();
84             
85             while(windows.hasNext())
86             {
87                 ManagedWindow window = (ManagedWindow)windows.next();
88                 JMenuItem JavaDoc item = new JMenuItem JavaDoc(window.getTitle());
89                 item.setEnabled(false);
90                 menu.add(item);
91             }
92             
93             this.add(menu);
94         }
95         
96         //if no window, show a dumb item
97
if(this.getItemCount() == 0)
98         {
99             JMenuItem JavaDoc dumb = new JMenuItem JavaDoc(tr("mnu.none"));
100             dumb.setEnabled(false);
101             this.add(dumb);
102         }
103     }
104
105     public void actionPerformed(ActionEvent JavaDoc ae)
106     {
107         JMenuItem JavaDoc toggle = (JMenuItem JavaDoc)ae.getSource();
108         if(toggle.getText().equals(tr("mnu.hide")))
109         {
110             Plugin plugin = (Plugin)toggleButtons.get(toggle);
111             hideWindowsFor(plugin);
112         }
113         else
114         {
115             Plugin plugin = (Plugin)toggleButtons.get(toggle);
116             showWindowsFor(plugin);
117         }
118     }
119
120     private boolean isHidden(Plugin plugin)
121     {
122         Iterator JavaDoc windows = Client.getInstance().getWindowManager().getWindowsFor(plugin);
123         while(windows.hasNext())
124         {
125             ManagedWindow window = (ManagedWindow)windows.next();
126             if(window.isVisible())
127                 return false;
128         }
129         
130         return true;
131     }
132     
133     private void hideWindowsFor(Plugin plugin)
134     {
135         Iterator JavaDoc windows = Client.getInstance().getWindowManager().getWindowsFor(plugin);
136         while(windows.hasNext())
137         {
138             ManagedWindow window = (ManagedWindow)windows.next();
139             window.hide();
140         }
141     }
142     
143     private void showWindowsFor(Plugin plugin)
144     {
145         Iterator JavaDoc windows = Client.getInstance().getWindowManager().getWindowsFor(plugin);
146         while(windows.hasNext())
147         {
148             ManagedWindow window = (ManagedWindow)windows.next();
149             window.show();
150         }
151     }
152     
153     private String JavaDoc tr(String JavaDoc s)
154     {
155         return plugin.tr(s);
156     }
157 }
Popular Tags