Changeset 292 in josm for src/org/openstreetmap/josm/plugins
- Timestamp:
- 2007-07-19T23:19:21+02:00 (18 years ago)
- Location:
- src/org/openstreetmap/josm/plugins
- Files:
-
- 3 edited
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 pluginName+"/";105 106 107 108 109 110 111 112 113 114 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 } -
src/org/openstreetmap/josm/plugins/PluginInformation.java
r291 r292 36 36 37 37 public final Map<String, String> attr = new TreeMap<String, String>(); 38 39 /** 40 * Used in the Plugin constructor to make the information of the plugin 41 * that is currently initializing available. 42 * 43 * If you think this is hacky, you are probably right. But it is 44 * convinient anyway ;-) 45 */ 46 static PluginInformation currentPluginInitialization = null; 38 47 39 48 /** … … 102 111 public PluginProxy load(Class<?> klass) { 103 112 try { 113 currentPluginInitialization = this; 104 114 return new PluginProxy(klass.newInstance(), this); 105 115 } catch (Exception e) { -
src/org/openstreetmap/josm/plugins/PluginProxy.java
r267 r292 19 19 public final Object plugin; 20 20 public final PluginInformation info; 21 public boolean misbehaving = false;22 21 23 22 public PluginProxy(Object plugin, PluginInformation info) { 24 23 this.plugin = plugin; 25 24 this.info = info; 26 27 // setting name of the plugin by reflection28 if (plugin instanceof Plugin) {29 try {30 Plugin.class.getDeclaredField("name").set(plugin, info.name);31 } catch (Exception e) {32 if (e instanceof RuntimeException)33 throw (RuntimeException)e;34 throw new RuntimeException(e);35 }36 }37 25 } 38 26
Note:
See TracChangeset
for help on using the changeset viewer.