Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/lucane/client/Plugin.java


1   /*
2    * Lucane - a collaborative platform
3    * Copyright (C) 2002  Vincent Fiack <vincent@lucane.org>
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  
23  import java.awt.event.*;
24  import java.io.*;
25  import java.net.*;
26  import java.util.*;
27  
28  
29  /**
30   * Every plugin has to extend this class and redefine its abstract methods.
31   * See the tutorial on writing extensions and the sources of
32   * the various plugins for more infiormations.
33   */
34  public abstract class Plugin
35    implements Runnable, WindowListener
36  {
37  
38    //do not redeclare them in your plugin !
39    protected boolean starter;
40    protected ResourceBundle bundle;
41    
42    /**
43     * Used by the PluginLoader
44     *
45     * @return the plugin class name
46     */
47    public String getName()
48    {
49      return this.getClass().getPackage().getName();
50    }
51  
52    /**
53     * Used by the PluginLoader to check if new versions needs to be downloaded
54     * 
55     * @return lthe plugin's version
56     */
57    public String getVersion()
58    {
59      return tr("version");
60    }
61  
62    /**
63     * Allow you to know who made these beatiful bugs :-P
64     * 
65     * @return the author's name
66     */
67    public String getAuthor()
68    {
69      return tr("author");
70    }
71  
72    /**
73     * Where to complain or send feedback.
74     * 
75     * @return the author's mail address
76     */
77    public String getEmail()
78    {
79      return tr("email");
80    }
81  
82    /**
83     * Used in the MainInterface.
84     * 
85     * @return a small description
86     */
87    public String getToolTip()
88    {
89      return tr("tooltip");
90    }
91  
92    /**
93     * Associate a picture with a plugin for ever move userfriendliness
94     * 
95     * @return the plugin's icon name ("plugin.jpg" for example)
96     */
97    public String getIcon()
98    {
99      return tr("icon");
100   }
101 
102   /**
103    * Where to put the plugin in the MainInterface
104    * 
105    * @return the plugin's category
106    */
107   public String getCategory()
108   {
109     return tr("category");
110   }
111 
112   /**
113    * The title to show
114    * 
115    * @return the plugin's title
116    */
117   public String getTitle()
118   {
119     return tr("title");
120   }
121 
122   /**
123    * Used by the PluginLoader to initialize the plugin with adequate parameters
124    * 
125    * @param friends the connections to use
126    * @param starter true if the start() method has to be called
127    * @return a new plugin's instance
128    */
129   public abstract Plugin init(ConnectInfo[] friends, boolean starter);
130 
131   /**
132    * Used by the PluginLoader to load a plugin previously initialized
133    * with a command String and a Socket to communicate with.
134    * 
135    * @param oc the ObjectConnection
136    * @param who the request sender
137    * @param data network data for the plugin
138    */
139   public abstract void load(ObjectConnection oc, ConnectInfo who, String data);
140 
141   /**
142    * Used when a plugin is started (ie. not loaded afeter a network request).
143    * For example, it is called when a user wants to send a mesage to another.
144    * It allows the plugin developpeur to ask for other parameters interactively.
145    */
146   public abstract void start();
147 
148   /**
149    * Used by the PluginLoader when a plugin is loaded after a network query.
150    */
151   public abstract void follow();
152 
153   /**
154    * Allows the Client to know when to quit.
155    */
156   public void exit()
157   {
158     Logging.getLogger().fine("Plugin exited");
159     Client.getInstance().deregisterPlugin();
160   }
161 
162   /**
163    * Mandatory to be run as a new Thread
164    */
165   public void run()
166   {
167     Client.getInstance().registerPlugin();
168   Logging.getLogger().fine("attempt to start a plugin thread");
169   Logging.getLogger().fine("starter = " + this.starter);
170 
171     if(this.starter)
172       start();
173     else
174       follow();
175   }
176 
177   /**
178    * Internationalization
179    * 
180    * @param lang the language t use (ie. fr, en, ...)
181    */
182   public void setLocale(String lang)
183   {
184     try
185     {
186       InputStream is = new URL(getDirectory() + "messages_" + lang +  ".properties").openStream();
187       this.bundle = new PropertyResourceBundle(is);
188     }
189     catch(Exception e1)
190     {
191       try
192       {
193         InputStream is = new URL(getDirectory() + "messages.properties").openStream();
194         this.bundle = new PropertyResourceBundle(is);
195       }
196       catch(Exception e2)
197       {
198         this.bundle = null;
199       }
200     }
201   }
202 
203   /**
204    * Translation
205    * 
206    * @param origin the String to get
207    * @return the corresponding String in the correct language
208    */
209   public String tr(String origin)
210   {
211     try
212     {
213       return bundle.getString(origin);
214     }
215     catch(Exception e)
216     {
217       return origin;
218     }
219   }
220 
221   /**
222    * Useful to exit() properly. Must be used as WindowListener
223    * for graphical plugins. Example :  JFrame jf =
224    * new JFrame(); jf.addWindowListener(this);
225    * 
226    * @param e an event.
227    */
228   public void windowClosing(WindowEvent e)
229   {
230     exit();
231   }
232 
233   public void windowActivated(WindowEvent e)
234   {}
235   public void windowClosed(WindowEvent e)
236   {}
237   public void windowDeactivated(WindowEvent e)
238   {}
239   public void windowDeiconified(WindowEvent e)
240   {}
241   public void windowIconified(WindowEvent e)
242   {}
243   public void windowOpened(WindowEvent e)
244   {}
245 
246   /**
247    * Return the plugin's base directory
248    * 
249    * @return the plugin's directory
250    */
251   public String getDirectory()
252   {
253     String pack = this.getClass().getPackage().getName();
254 
255     String url = "jar:file:///" + System.getProperty("user.dir") + "/" + 
256                  Constants.PLUGIN_DIRECTORY + pack + ".jar!/" +
257                  pack.replace('.', '/') + '/';            
258 
259     return url.replace('\\', '/');
260   }
261 }