1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
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
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
135
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
146
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
157
158
159 public void pluginInfoAdded(PluginInfo info)
160 {
161 addInfo(info);
162 }
163
164
165
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 }