View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program 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
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.gui.menu;
20  
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.util.Iterator;
24  
25  import javax.swing.JCheckBoxMenuItem;
26  import javax.swing.JMenu;
27  import javax.swing.JMenuItem;
28  
29  import org.apache.log4j.Logger;
30  import org.xnap.XNap;
31  import org.xnap.event.PluginListener;
32  import org.xnap.gui.XNapFrame;
33  import org.xnap.gui.plugin.PluginHelper;
34  import org.xnap.gui.util.IconHelper;
35  import org.xnap.plugin.Plugin;
36  import org.xnap.plugin.PluginInfo;
37  import org.xnap.plugin.PluginManager;
38  
39  public class EnablePluginsMenu extends JMenu implements PluginListener
40  {
41  	//--- Constant(s) ---
42  
43  	//--- Data field(s) ---
44  
45      private static Logger logger = Logger.getLogger(EnablePluginsMenu.class);
46  
47  	private JMenu networkPluginsMenu = new JMenu(XNap.tr("Network"));
48  	private JMenu otherPluginsMenu = new JMenu(XNap.tr("Other"));
49  	private JMenu chatPluginsMenu = new JMenu(XNap.tr("Chat"));
50  	private JMenu viewerPluginsMenu = new JMenu(XNap.tr("Viewer"));
51  	private JMenu lookandfeelPluginsMenu = new JMenu(XNap.tr("Look&Feel"));
52  
53  	ActionListener pluginListener = new PluginActionListener();
54  	
55  	//--- Constructor(s) ---
56  	
57      public EnablePluginsMenu()
58  	{
59  		super(XNap.tr("Enable Plugins"));
60  		initialize();
61  		PluginManager.getInstance().addPluginListener(this);
62  	}
63  
64  	private void initialize()
65  	{
66  		setIcon(IconHelper.getEmptyIcon(16));
67  		add(networkPluginsMenu);
68  		add(otherPluginsMenu);
69  		add(chatPluginsMenu);
70  		add(viewerPluginsMenu);	
71  		add(lookandfeelPluginsMenu);	
72  
73  		for (Iterator i = PluginManager.getInstance().infos(); i.hasNext();) {
74  			addInfo((PluginInfo)i.next());
75  		}
76  	}
77  
78  	private void addInfo(PluginInfo info)
79  	{
80  		if (!info.isPlugin()){
81  			return;
82  		}
83  		
84  		JMenuItem item = new JCheckBoxMenuItem(info.getName());
85  		
86  		item.setActionCommand(info.getName());
87  		item.addActionListener(pluginListener);
88  		item.setSelected(info.isEnabled());
89  	    
90  		getSectionMenu(info).add(item);
91  	}
92  
93  	private void removeInfo(PluginInfo info)
94  	{
95  		JMenu menu = getSectionMenu(info);
96  		JMenuItem item = getPluginItem(info);
97  		if (item != null) {
98  			menu.remove(item);
99  		}
100 	}
101 
102 	private JMenu getSectionMenu(PluginInfo info)
103 	{
104 		JMenu menu;
105 		if (info.getSection().equals("plugin/net")) {
106 			menu = networkPluginsMenu;
107 		}
108 		else if (info.getSection().equals("plugin/chat")) {
109 			menu = chatPluginsMenu;
110 		}
111 		else if (info.getSection().equals("plugin/viewer")) {
112 			menu = viewerPluginsMenu;
113 		}
114 		else if (info.getSection().equals("plugin/laf")) {
115 			menu = lookandfeelPluginsMenu;
116 		}
117 		else {
118 			menu = otherPluginsMenu;
119 		}
120 		return menu;
121 	}
122 
123 	private JMenuItem getPluginItem(PluginInfo info)
124 	{
125 		JMenu menu = getSectionMenu(info);
126 		for (int i = 0; i < menu.getItemCount(); i++) {
127 			if (menu.getItem(i).getActionCommand().equals(info.getName())) {
128 				return menu.getItem(i);
129 			}
130 		}
131 		return null;
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see org.xnap.event.PluginListener#pluginEnabled(org.xnap.plugin.Plugin)
136 	 */
137 	public void pluginEnabled(Plugin plugin)
138 	{
139 		JMenuItem item = getPluginItem(plugin.getInfo());
140 		if (item != null) {
141 			item.setSelected(true);
142 		}
143 	}
144 
145 	/* (non-Javadoc)
146 	 * @see org.xnap.event.PluginListener#pluginDisabled(org.xnap.plugin.Plugin)
147 	 */
148 	public void pluginDisabled(Plugin plugin)
149 	{
150 		JMenuItem item = getPluginItem(plugin.getInfo());
151 		if (item != null) {
152 			item.setSelected(false);
153 		}
154 	}
155 
156 	/* (non-Javadoc)
157 	 * @see org.xnap.event.PluginListener#pluginInfoAdded(org.xnap.plugin.PluginInfo)
158 	 */
159 	public void pluginInfoAdded(PluginInfo info)
160 	{
161 		addInfo(info);
162 	}
163 
164 	/* (non-Javadoc)
165 	 * @see org.xnap.event.PluginListener#pluginInfoRemoved(org.xnap.plugin.PluginInfo)
166 	 */
167 	public void pluginInfoRemoved(PluginInfo info)
168 	{
169 		removeInfo(info);
170 	}
171 
172     	
173 	private class PluginActionListener implements ActionListener
174 	{		
175 
176 		public void actionPerformed(ActionEvent e) 
177 		{
178 			JMenuItem item = (JMenuItem)e.getSource();		
179 			PluginInfo info = PluginManager.getInstance().getInfoByName
180 				(e.getActionCommand());
181 			
182 			if (info != null) {
183 				if (item.isSelected()) {
184 					PluginHelper.enablePlugin(XNapFrame.getInstance(), info);
185 				}
186 				else {
187 					PluginHelper.disablePlugin(XNapFrame.getInstance(), info);
188 				}
189 			}
190 		}
191 	}
192 
193 }