1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.impl;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.logging.Logger;
25
26 import javax.swing.Icon;
27 import javax.swing.JDialog;
28
29 import net.sourceforge.mflow.api.Plugin;
30 import net.sourceforge.mflow.api.PluginManager;
31 import net.sourceforge.mflow.reflect.ParamArgPair;
32 import net.sourceforge.mflow.reflect.ReflectionException;
33 import net.sourceforge.mflow.reflect.ReflectionUtil;
34
35 /***
36 * @author carrd
37 */
38 class PluginImpl implements Plugin {
39 private static final Logger log =
40 Logger.getLogger(PluginImpl.class.getName());
41 private PluginManager pluginManager;
42 private String className;
43 private String name;
44 private String type;
45 private String description;
46 private String version;
47 private Object plugin;
48 private Icon icon;
49 private JDialog dialog;
50
51 PluginImpl(
52 PluginManager pluginManager,
53 String className,
54 String name,
55 String type,
56 String description,
57 String version,
58 Icon icon,
59 JDialog dialog) {
60 this.pluginManager = pluginManager;
61 this.className = className;
62 this.name = name;
63 this.type = type;
64 this.description = description;
65 this.version = version;
66 this.icon = icon;
67 this.dialog = dialog;
68 }
69
70 /***
71 * Get the version number for this plugin.
72 *
73 * @return The version number.
74 */
75 public String getVersion() {
76 return this.version;
77 }
78
79 /***
80 * Get the class name for this plugin.
81 *
82 * @return The class name.
83 */
84
85 public String getClassName() {
86 return this.className;
87 }
88
89 /***
90 * Get the name of this plugin.
91 *
92 * @return The name.
93 */
94
95 public String getName() {
96 return this.name;
97 }
98
99 /***
100 * Get the type of this plugin.
101 *
102 * @return The type.
103 */
104
105 public String getType() {
106 return this.type;
107 }
108
109 /***
110 * Get the description of this plugin.
111 *
112 * @return The description, or <b>null</b> if no description is available.
113 */
114 public String getDescription() {
115 return this.description;
116 }
117
118 /***
119 * Get the icon for this plugin.
120 *
121 * @return The icon, or <b>null</b> if no icon is available.
122 */
123
124 public Icon getIcon() {
125 return this.icon;
126 }
127
128 /***
129 * Load the plugin. This method attempts to load the plugin entry-point class
130 * and create an instance of it. The entry-point class must have a public
131 * constructor that accepts a <code>Plugin</code> object as an argument; it
132 * will be invoked with a reference to this plugin.
133 *
134 * @throws InvocationTargetException If the plugin could not be loaded.
135 */
136 public void load() throws InvocationTargetException {
137 if (this.plugin != null) {
138 return;
139 }
140 Class[] paramType = new Class[] { PluginManager.class };
141 Object[] arg = new Object[] { this.pluginManager };
142 List sigs = new ArrayList();
143 ParamArgPair pair = new ParamArgPair(paramType, arg);
144 sigs.add(pair);
145 try {
146 this.plugin = ReflectionUtil.instantiate(this.className, sigs);
147 } catch (ReflectionException re) {
148 throw new InvocationTargetException(re);
149 }
150 }
151
152 /***
153 * Dispose of the plugin. This method calls the <code>dispose()</code>
154 * method on the plugin entry-point class, if there is one, and then destroys
155 * its reference to the class. The class can be re-loaded via another call to
156 * <code>load()</code>.
157 */
158 public void dispose() {
159 if (this.plugin != null) {
160 List sigs = new ArrayList();
161 sigs.add(new ParamArgPair());
162 try {
163 ReflectionUtil.invokeMethod(this.plugin, "dispose", sigs);
164 } catch (ReflectionException re) {
165 log.throwing(getClass().getName(), "dispose", re);
166 }
167 this.plugin = null;
168 }
169 }
170
171 /***
172 * Get a reference to the instance of the plugin Activator class.
173 *
174 * @return The instance, or <code>null</code> if the class has not yet been
175 * loaded.
176 */
177 public Object getPluginObject() {
178 return this.plugin;
179 }
180
181 public String toString() {
182 String ret;
183 if (this.version != null) {
184 ret = this.name + " " + this.version;
185 } else {
186 ret = this.name;
187 }
188 return ret;
189 }
190
191 public boolean isLoaded() {
192 return this.plugin != null;
193 }
194
195 public JDialog getConfigurationDialog() {
196 return this.dialog;
197 }
198 }
This page was automatically generated by Maven