Changeset 292 in josm for src/org/openstreetmap/josm/plugins


Ignore:
Timestamp:
2007-07-19T23:19:21+02:00 (18 years ago)
Author:
imi
Message:
  • fixed Bug Report module to work better with plugins
  • fixed Plugin.getPreferenceDir and Plugin.copy
  • fixed Ctrl-Q not asking for changed data
  • fixed several typos in Shortcuts
  • removed check for latest josm version
Location:
src/org/openstreetmap/josm/plugins
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/plugins/Plugin.java

    r291 r292  
    66import java.io.IOException;
    77import java.io.InputStream;
    8 import java.net.URL;
    9 import java.net.URLClassLoader;
    108import java.util.List;
    119
     
    1715/**
    1816 * All plugins *must* have an standard constructor taking no arguments.
     17 *
    1918 * This constructor is called at JOSM startup, after all Main-objects have been initialized.
    2019 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
    2120 * (or else, the plugin jar will not be within the class path).
    2221 *
    23  * All plugins should have at least one class subclassing this abstract base class.
     22 * A plugin may subclass this abstract base class (but it is optional).
    2423 *
    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.
    2929 *
    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.
    3736 *
    3837 * @author Immanuel.Scholz
     
    4039public abstract class Plugin {
    4140
    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;
    5649
    5750        /**
    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.
    6052         */
    61         @Deprecated public final String getName() {
    62                 return name;
     53        public final String getPluginDir() {
     54                return Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
    6355        }
    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 
    7356
    7457        /**
     
    9275       
    9376        /**
    94          * @deprecated Use copy(String pluginName, String from, String to) instead
    95          */
    96         @Deprecated public void copy(String from, String to) throws FileNotFoundException, IOException {
    97                 copy(name, from, to);
    98     }
    99 
    100         /**
    10177         * Copies the ressource 'from' to the file in the plugin directory named 'to'.
    10278         */
    103         public void copy(String pluginName, String from, 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();
    11591    }
    11692}
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r291 r292  
    3636
    3737        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;
    3847
    3948        /**
     
    102111        public PluginProxy load(Class<?> klass) {
    103112                try {
     113                        currentPluginInitialization = this;
    104114                        return new PluginProxy(klass.newInstance(), this);
    105115                } catch (Exception e) {
  • src/org/openstreetmap/josm/plugins/PluginProxy.java

    r267 r292  
    1919        public final Object plugin;
    2020        public final PluginInformation info;
    21         public boolean misbehaving = false;
    2221
    2322        public PluginProxy(Object plugin, PluginInformation info) {
    2423                this.plugin = plugin;
    2524                this.info = info;
    26 
    27                 // setting name of the plugin by reflection
    28                 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                 }
    3725    }
    3826
Note: See TracChangeset for help on using the changeset viewer.