KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > Plugin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 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 package org.lucane.client;
20
21 import org.lucane.common.*;
22 import org.lucane.common.net.ObjectConnection;
23
24 import java.io.*;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.*;
28 import java.util.*;
29
30 import javax.swing.ImageIcon JavaDoc;
31
32
33 /**
34  * Every plugin has to extend this class and redefine its abstract methods.
35  * See the tutorial on writing extensions and the sources of
36  * the various plugins for more infiormations.
37  */

38 public abstract class Plugin
39 implements Runnable JavaDoc
40 {
41     //do not redeclare them in your plugin !
42
private boolean starter;
43     protected ResourceBundle bundle;
44     private ResourceBundle defaultBundle;
45     private LocalConfig config;
46     private String JavaDoc name = null;
47     
48     /**
49      * Used by the PluginManager
50      *
51      * @return the plugin class name
52      */

53     public String JavaDoc getName()
54     {
55         if(name != null)
56             return this.name;
57         
58         return this.getClass().getPackage().getName();
59     }
60     
61     /**
62      * Used by the PluginManager to overload plugin name
63      *
64      * @param name the plugin name
65      */

66     protected void setName(String JavaDoc name)
67     {
68         this.name = name;
69     }
70     
71     /**
72      * Used by the PluginManager to check if new versions needs to be downloaded
73      *
74      * @return lthe plugin's version
75      */

76     public String JavaDoc getVersion()
77     {
78         return tr("version");
79     }
80     
81     /**
82      * Allow you to know who made these beatiful bugs :-P
83      *
84      * @return the author's name
85      */

86     public String JavaDoc getAuthor()
87     {
88         return tr("author");
89     }
90     
91     /**
92      * Where to complain or send feedback.
93      *
94      * @return the author's mail address
95      */

96     public String JavaDoc getEmail()
97     {
98         return tr("email");
99     }
100     
101     /**
102      * Used in the MainInterface.
103      *
104      * @return a small description
105      */

106     public String JavaDoc getToolTip()
107     {
108         return tr("tooltip");
109     }
110     
111     /**
112      * Associate a picture with a plugin for ever move userfriendliness
113      *
114      * @return the plugin's icon name ("plugin.jpg" for example)
115      */

116     public String JavaDoc getIcon()
117     {
118         return tr("icon");
119     }
120     
121     /**
122      * Associate a picture with a plugin for ever move userfriendliness
123      *
124      * @return the plugin's icon name ("plugin.jpg" for example)
125      */

126     public String JavaDoc getIcon16()
127     {
128         return tr("icon16");
129     }
130     
131     /**
132      * Where to put the plugin in the MainInterface
133      *
134      * @return the plugin's category
135      */

136     public String JavaDoc getCategory()
137     {
138         return tr("category");
139     }
140     
141     /**
142      * The title to show
143      *
144      * @return the plugin's title
145      */

146     public String JavaDoc getTitle()
147     {
148         return tr("title");
149     }
150
151     /**
152      * Set the starter flag, to decide wether to run start() or follow()
153      * Called by the plugin manager.
154      *
155      * @param starter the flag
156      */

157     protected void setStarter(boolean starter)
158     {
159         this.starter = starter;
160     }
161     
162     /**
163      * Can the plugin be run without any users ?
164      * @return true if the plugin is standalone
165      */

166     public boolean isStandalone()
167     {
168         return false;
169     }
170     
171     /**
172      * Used by the PluginManager to initialize the plugin with adequate parameters
173      *
174      * @param friends the connections to use
175      * @param starter true if the start() method has to be called
176      * @return a new plugin's instance
177      */

178     public abstract Plugin newInstance(ConnectInfo[] friends);
179     
180     /**
181      * Used by the PluginManager to load a plugin previously initialized
182      * with a command String and a Socket to communicate with.
183      *
184      * @param oc the ObjectConnection
185      * @param who the request sender
186      * @param data network data for the plugin
187      */

188     public void load(ObjectConnection oc, ConnectInfo who, String JavaDoc data)
189     {
190     }
191     
192     /**
193      * Used when a plugin is started (ie. not loaded afeter a network request).
194      * For example, it is called when a user wants to send a mesage to another.
195      * It allows the plugin developpeur to ask for other parameters interactively.
196      */

197     public void start()
198     {
199         exit();
200     }
201     
202     /**
203      * Used by the PluginManager when a plugin is loaded after a network query.
204      */

205     public void follow()
206     {
207         exit();
208     }
209     
210     /**
211      * Invoke a method
212      *
213      * @param method the method name
214      * @param types the params types
215      * @param params the params
216      * @return the method result
217      */

218     public Object JavaDoc invoke(String JavaDoc method, Class JavaDoc[] types, Object JavaDoc[] params)
219     throws SecurityException JavaDoc, NoSuchMethodException JavaDoc, IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
220     {
221         Method JavaDoc m = this.getClass().getDeclaredMethod(method, types);
222         return m.invoke(this, params);
223     }
224     
225     
226     /**
227      * Allows the Client to know when to quit.
228      */

229     public void exit()
230     {
231         Logging.getLogger().fine("Plugin exited");
232         try {
233             this.config.save();
234         } catch(IOException ioe) {
235             Logging.getLogger().warning("Unable to save localconfig");
236             ioe.printStackTrace();
237         }
238         PluginManager.getInstance().removeRunningPlugin(this);
239     }
240     
241     /**
242      * Mandatory to be run as a new Thread
243      */

244     public void run()
245     {
246         PluginManager.getInstance().addRunningPlugin(this);
247         Logging.getLogger().fine("attempt to start a plugin thread");
248         Logging.getLogger().fine("starter = " + this.starter);
249         this.config = new LocalConfig(this.getName());
250         
251         if(this.starter)
252             start();
253         else
254             follow();
255     }
256     
257     /**
258      * Internationalization
259      *
260      * @param lang the language t use (ie. fr, en, ...)
261      */

262     public void setLocale(String JavaDoc lang)
263     {
264         try {
265             InputStream is = new URL(getDirectory() + "messages/messages.properties").openStream();
266             this.bundle = new PropertyResourceBundle(is);
267             this.defaultBundle = this.bundle;
268         } catch(Exception JavaDoc e2) {
269             this.bundle = null;
270             this.defaultBundle = null;
271         }
272         
273         try {
274             InputStream is = new URL(getDirectory() + "messages/messages_" + lang + ".properties").openStream();
275             this.bundle = new PropertyResourceBundle(is);
276         } catch(Exception JavaDoc e1) {
277             //bundles are already inited with the default locale
278
}
279     }
280     
281     /**
282      * Translation
283      *
284      * @param origin the String to get
285      * @return the corresponding String in the correct language
286      */

287     public String JavaDoc tr(String JavaDoc origin)
288     {
289         try {
290             return bundle.getString(origin);
291         } catch(Exception JavaDoc e) {
292             try {
293                 return defaultBundle.getString(origin);
294             } catch(Exception JavaDoc e2) {
295                 return origin;
296             }
297         }
298     }
299     /**
300      * Return the plugin's base directory
301      *
302      * @return the plugin's directory
303      */

304     public String JavaDoc getDirectory()
305     {
306         String JavaDoc pack = this.getClass().getPackage().getName();
307         
308         String JavaDoc url = "jar:file:///" + System.getProperty("user.dir") + "/" +
309         Client.APPLICATIONS_DIRECTORY + getName() + ".jar!/";
310         
311         return url.replace('\\', '/');
312     }
313     
314     /**
315      * Get an image
316      *
317      * @return the image icon
318      */

319     public ImageIcon JavaDoc getImageIcon(String JavaDoc file)
320     {
321         try {
322             return new ImageIcon JavaDoc(new URL(getDirectory() + "images/" + file));
323         } catch(Exception JavaDoc e) {
324             return new ImageIcon JavaDoc();
325         }
326     }
327     
328     /**
329      * Return the LocalConfig object
330      *
331      * @return the local config
332      */

333     public LocalConfig getLocalConfig()
334     {
335         return this.config;
336     }
337     
338     public String JavaDoc toString()
339     {
340         return getTitle();
341     }
342 }
343
Popular Tags