1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.plugin;
21
22 import java.awt.Component;
23
24 import org.apache.log4j.Logger;
25 import org.xnap.XNap;
26 import org.xnap.gui.Dialogs;
27 import org.xnap.gui.ErrorDialog;
28 import org.xnap.gui.StatusBar;
29 import org.xnap.plugin.PluginInfo;
30 import org.xnap.plugin.PluginInitializeException;
31 import org.xnap.plugin.PluginManager;
32
33 public class PluginHelper {
34
35
36
37 private static Logger logger = Logger.getLogger(PluginHelper.class);
38
39
40
41 public static boolean disablePlugin(Component parent, PluginInfo info)
42 {
43 if (!info.isLoaded()) {
44
45 return false;
46 }
47
48 if (!info.canDisable()) {
49 info.setEnableOnStartup(false);
50 Dialogs.error
51 (parent,
52 XNap.tr("Can not disable plugin. Restart XNap to disable plugin."));
53 return false;
54 }
55
56 try {
57 StatusBar.setText(XNap.tr("Disabling plugin") + "...");
58 PluginManager.getInstance().setEnabled
59 (info.getPlugin(), false);
60 return true;
61 }
62 catch (Throwable e) {
63 logger.error("could not disable plugin", e);
64 ErrorDialog.show(parent, XNap.tr("Could not disable plugin."), e);
65 return false;
66 }
67 finally {
68 StatusBar.clear();
69 }
70 }
71
72 public static boolean enablePlugin(Component parent, PluginInfo info)
73 {
74 try {
75 StatusBar.setText(XNap.tr("Loading plugin") + "...");
76 if (info.isLoaded() || load(parent, info)) {
77
78 try {
79 StatusBar.setText(XNap.tr("Enabling plugin") + "...");
80 PluginManager.getInstance().setEnabled
81 (info.getPlugin(), true);
82 return true;
83 }
84 catch (PluginInitializeException e) {
85 logger.error("could not enable plugin", e);
86 Dialogs.error(parent,
87 XNap.tr("Error while initializing plugin: {0}",
88 e.getLocalizedMessage()));
89
90 }
91 catch (ClassNotFoundException e) {
92 logger.error("could not enable plugin", e);
93
94 Dialogs.error(parent,
95 XNap.tr("A required class is missing: {0}",
96 e.getLocalizedMessage()));
97 }
98 catch (Throwable e) {
99 logger.error("could not enable plugin", e);
100 ErrorDialog.show
101 (parent, XNap.tr("Could not enable plugin."), e);
102 }
103 }
104 return false;
105 }
106 finally {
107 StatusBar.clear();
108 }
109 }
110
111 /***
112 * Loads <code>info</code>. Displays a message box, if load fails.
113 *
114 * @param c the parent component for the error dialog
115 * @param info the info to load
116 * @return true, if successful; false, if plugin class could not be loaded
117 * @see PluginManager
118 */
119 public static boolean load(Component c, PluginInfo info)
120 {
121 try {
122 PluginManager.getInstance().load(info);
123 return true;
124 }
125 catch (ClassNotFoundException e) {
126 logger.error("could not load plugin", e);
127
128 Dialogs.error(c, XNap.tr("A required class is missing: {0}",
129 e.getLocalizedMessage()));
130 }
131 catch (Throwable e) {
132 logger.error("could not load " + info.getClassName(), e);
133 ErrorDialog.show(c, XNap.tr("Could not load plugin: {0}.",
134 e.getLocalizedMessage()), e);
135 }
136
137 return false;
138 }
139
140 }