Changeset 292 in josm for src/org/openstreetmap/josm/plugins/Plugin.java
- Timestamp:
- 2007-07-19T23:19:21+02:00 (19 years ago)
- File:
-
- 1 edited
-
src/org/openstreetmap/josm/plugins/Plugin.java (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/plugins/Plugin.java
r291 r292 6 6 import java.io.IOException; 7 7 import java.io.InputStream; 8 import java.net.URL;9 import java.net.URLClassLoader;10 8 import java.util.List; 11 9 … … 17 15 /** 18 16 * All plugins *must* have an standard constructor taking no arguments. 17 * 19 18 * This constructor is called at JOSM startup, after all Main-objects have been initialized. 20 19 * For all purposes of loading dynamic resources, the Plugin's class loader should be used 21 20 * (or else, the plugin jar will not be within the class path). 22 21 * 23 * A llplugins should have at least one classsubclassingthis abstract base class.22 * A plugin may subclass this abstract base class (but it is optional). 24 23 * 25 * The actual implementation of this interface is optional, as all functions will be called 26 * via reflection. This is to be able to change this interface without the need of recompiling 27 * or even breaking the plugins. If your class does not provide a function here (or does 28 * provide a function with a mismatching signature), it will not be called. That simple. 24 * The actual implementation of this class is optional, as all functions will be called 25 * via reflection. This is to be able to change this interface without the need of 26 * recompiling or even breaking the plugins. If your class does not provide a 27 * function here (or does provide a function with a mismatching signature), it will not 28 * be called. That simple. 29 29 * 30 * Or in other words: See this base class as an documentation of what functions are provided. 31 * Subclassing it and overriding some functions makes it easy for you to keep sync with the 32 * correct actual plugin architecture of JOSM. 33 * 34 * 35 * The pluginname provided to the constructor is also the name of the directory to 36 * store the plugin's own stuff (located under the josm preferences directory) 30 * Or in other words: See this base class as an documentation of what automatic callbacks 31 * are provided (you can register yourself to more callbacks in your plugin class 32 * constructor). 33 * 34 * Subclassing Plugin and overriding some functions makes it easy for you to keep sync 35 * with the correct actual plugin architecture of JOSM. 37 36 * 38 37 * @author Immanuel.Scholz … … 40 39 public abstract class Plugin { 41 40 42 String name; 43 44 public Plugin() { 45 try { 46 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs(); 47 name = urls[urls.length-1].toString(); 48 if (name.toLowerCase().endsWith(".jar")) { 49 int lastSlash = name.lastIndexOf('/'); 50 name = name.substring(lastSlash+1, name.length()-4); 51 } 52 } catch (RuntimeException e) { 53 name = "unknown"; 54 } 55 } 41 /** 42 * This is the info available for this plugin. You can access this from your 43 * constructor. 44 * 45 * (The actual implementation to request the info from a static variable 46 * is a bit hacky, but it works). 47 */ 48 public final PluginInformation info = PluginInformation.currentPluginInitialization; 56 49 57 50 /** 58 * @return The name of this plugin. This is the name of the .jar file. 59 * @deprecated Plugins have to know their name by themself. 51 * @return The directory for the plugin to store all kind of stuff. 60 52 */ 61 @Deprecatedpublic final String getName() {62 return name;53 public final String getPluginDir() { 54 return Main.pref.getPreferencesDir()+"plugins/"+info.name+"/"; 63 55 } 64 /**65 * @return The directory for the plugin to store all kind of stuff.66 * @deprecated Use <code>Main.pref.getPreferencesDir()+"plugins/"+name+"/";</code> instead.67 */68 @Deprecated public final String getPluginDir() {69 return Main.pref.getPreferencesDir()+"plugins/"+name+"/";70 }71 72 73 56 74 57 /** … … 92 75 93 76 /** 94 * @deprecated Use copy(String pluginName, String from, String to) instead95 */96 @Deprecated public void copy(String from, String to) throws FileNotFoundException, IOException {97 copy(name, from, to);98 }99 100 /**101 77 * Copies the ressource 'from' to the file in the plugin directory named 'to'. 102 78 */ 103 public void copy(String pluginName, Stringfrom, String to) throws FileNotFoundException, IOException {104 String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+pluginName+"/";105 File pluginDir = new File(pluginDirName);106 if (!pluginDir.exists())107 pluginDir.mkdirs();108 FileOutputStream out = new FileOutputStream(pluginDirName+to);109 InputStream in = getClass().getResourceAsStream(from);110 byte[] buffer = new byte[8192];111 for(int len = in.read(buffer); len > 0; len = in.read(buffer))112 out.write(buffer, 0, len);113 in.close();114 out.close();79 public void copy(String from, String to) throws FileNotFoundException, IOException { 80 String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+info.name+"/"; 81 File pluginDir = new File(pluginDirName); 82 if (!pluginDir.exists()) 83 pluginDir.mkdirs(); 84 FileOutputStream out = new FileOutputStream(pluginDirName+to); 85 InputStream in = getClass().getResourceAsStream(from); 86 byte[] buffer = new byte[8192]; 87 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) 88 out.write(buffer, 0, len); 89 in.close(); 90 out.close(); 115 91 } 116 92 }
Note:
See TracChangeset
for help on using the changeset viewer.
