Changeset 149 in josm for src


Ignore:
Timestamp:
2006-10-05T22:33:21+02:00 (18 years ago)
Author:
imi
Message:
  • added dynamic loading of plugins
  • removed mappaint (now seperate plugin)
  • fixed UrlLabel for Linux and Mac (untested)
Location:
src/org/openstreetmap/josm
Files:
3 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/Main.java

    r146 r149  
    5757import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    5858import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
    59 import org.openstreetmap.josm.plugins.Plugin;
     59import org.openstreetmap.josm.plugins.PluginException;
     60import org.openstreetmap.josm.plugins.PluginLoader;
     61import org.openstreetmap.josm.plugins.PluginProxy;
    6062import org.openstreetmap.josm.tools.ImageProvider;
    6163
     
    9496         * All installed and loaded plugins (resp. their main classes)
    9597         */
    96         public final Collection<Plugin> plugins = new LinkedList<Plugin>();
     98        public final static Collection<PluginProxy> plugins = new LinkedList<PluginProxy>();
    9799
    98100        /**
     
    127129                redoUndoListener.commandChanged(0,0);
    128130
    129                 for (Plugin plugin : plugins)
    130                         plugin.mapFrameInitialized(old, map);
     131                for (PluginProxy plugin : plugins)
     132            plugin.mapFrameInitialized(old, map);
    131133        }
    132134
     
    248250                contentPane.updateUI();
    249251               
     252
    250253                // Plugins
    251                 if (pref.hasKey("plugins")) {
    252                         for (String pluginName : pref.get("plugins").split(",")) {
     254                if (Main.pref.hasKey("plugins")) {
     255                        PluginLoader loader = new PluginLoader();
     256                        for (String pluginName : Main.pref.get("plugins").split(",")) {
    253257                                try {
    254                         plugins.add((Plugin)Class.forName(pluginName).newInstance());
    255                 } catch (Exception e) {
    256                         e.printStackTrace();
    257                         JOptionPane.showMessageDialog(parent, tr("Could not load plugin {0}.", pluginName));
    258                 }
     258                                        File pluginFile = new File(pref.getPreferencesDir()+"plugins/"+pluginName+".jar");
     259                                        if (pluginFile.exists())
     260                                                plugins.add(loader.loadPlugin(loader.loadClassName(pluginFile), pluginFile));
     261                                        else
     262                                                JOptionPane.showMessageDialog(Main.parent, tr("Plugin not found: {0}.", pluginName));
     263                                } catch (PluginException e) {
     264                                        e.printStackTrace();
     265                                        JOptionPane.showMessageDialog(Main.parent, tr("Could not load plugin {0}.", pluginName));
     266                                }
    259267                        }
    260268                }
  • src/org/openstreetmap/josm/data/Preferences.java

    r137 r149  
    4747        }
    4848
    49         synchronized final public boolean hasKey(final String key) {
     49        synchronized public boolean hasKey(final String key) {
    5050                return properties.containsKey(key);
    5151        }
    52         synchronized final public String get(final String key) {
     52        synchronized public String get(final String key) {
    5353                if (!properties.containsKey(key))
    5454                        return "";
    5555                return properties.get(key);
    5656        }
    57         synchronized final public String get(final String key, final String def) {
     57        synchronized public String get(final String key, final String def) {
    5858                final String prop = properties.get(key);
    5959                if (prop == null || prop.equals(""))
     
    6161                return prop;
    6262        }
    63         synchronized final public Map<String, String> getAllPrefix(final String prefix) {
     63        synchronized public Map<String, String> getAllPrefix(final String prefix) {
    6464                final Map<String,String> all = new TreeMap<String,String>();
    6565                for (final Entry<String,String> e : properties.entrySet())
     
    6868                return all;
    6969        }
    70         synchronized final public boolean getBoolean(final String key) {
     70        synchronized public boolean getBoolean(final String key) {
    7171                return getBoolean(key, false);
    7272        }
    73         synchronized final public boolean getBoolean(final String key, final boolean def) {
     73        synchronized public boolean getBoolean(final String key, final boolean def) {
    7474                return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
    7575        }
    7676
    7777
    78         synchronized final public void put(final String key, final String value) {
     78        synchronized public void put(final String key, final String value) {
    7979                if (value == null)
    8080                        properties.remove(key);
     
    8484                firePreferenceChanged(key, value);
    8585        }
    86         synchronized final public void put(final String key, final boolean value) {
     86        synchronized public void put(final String key, final boolean value) {
    8787                properties.put(key, Boolean.toString(value));
    8888                save();
  • src/org/openstreetmap/josm/gui/PreferenceDialog.java

    r134 r149  
    1010import java.awt.event.ActionEvent;
    1111import java.awt.event.ActionListener;
     12import java.io.File;
     13import java.util.Collection;
     14import java.util.HashMap;
     15import java.util.HashSet;
    1216import java.util.Locale;
    1317import java.util.Map;
     
    4448import org.openstreetmap.josm.Main;
    4549import org.openstreetmap.josm.data.projection.Projection;
     50import org.openstreetmap.josm.plugins.PluginProxy;
    4651import org.openstreetmap.josm.tools.ColorHelper;
    4752import org.openstreetmap.josm.tools.GBC;
    4853import org.openstreetmap.josm.tools.ImageProvider;
     54import org.openstreetmap.josm.tools.UrlLabel;
    4955
    5056/**
     
    190196        private JComboBox projectionCombo = new JComboBox(Projection.allProjections);
    191197        private JList annotationSources = new JList(new DefaultListModel());
     198        private Map<PluginProxy, Boolean> pluginMap = new HashMap<PluginProxy, Boolean>();
    192199
    193200
     
    268275                        ((DefaultListModel)annotationSources.getModel()).addElement(st.nextToken());
    269276
    270 
     277                Box pluginPanel = Box.createVerticalBox();
     278                Collection<String> availablePlugins = new HashSet<String>();
     279                for (File f : new File(Main.pref.getPreferencesDir()+"plugins").listFiles()) {
     280                        if (!f.isFile() || !f.getName().endsWith(".jar"))
     281                                continue;
     282                        availablePlugins.add(f.getName().substring(0, f.getName().length()-4));
     283                }
     284                for (PluginProxy plugin : Main.plugins) {
     285                        boolean available = availablePlugins.contains(plugin.name);
     286                        JCheckBox pluginCheck = new JCheckBox(plugin.name, available);
     287                        String desc = plugin.getDescription();
     288                        pluginPanel.add(pluginCheck);
     289                        if (desc != null) {
     290                                pluginCheck.setToolTipText(desc);
     291                                JLabel label = new JLabel("<html><i>"+desc+"</i></html>");
     292                                label.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
     293                                pluginPanel.add(label);
     294                                pluginPanel.add(Box.createVerticalStrut(5));
     295                        }
     296                        pluginMap.put(plugin, available);
     297                }
     298                JScrollPane pluginPane = new JScrollPane(pluginPanel);
     299                pluginPane.setBorder(null);
     300               
    271301                Map<String,String> allColors = new TreeMap<String, String>(Main.pref.getAllPrefix("color."));
    272302
     
    436466                map.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
    437467
     468               
     469                // Plugin tab
     470                JPanel plugin = createPreferenceTab("plugin", tr("Plugins"), tr("Configure available Plugins."));
     471                plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
     472                plugin.add(GBC.glue(0,10), GBC.eol());
     473                plugin.add(new UrlLabel("http://josm.eigenheimstrasse.de/wiki/Plugins", "Get more plugins"), GBC.std());
    438474
    439475                tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
     
    467503        private JPanel createPreferenceTab(String icon, String title, String desc) {
    468504                JPanel p = new JPanel(new GridBagLayout());
     505                p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    469506                p.add(new JLabel(title), GBC.eol().anchor(GBC.CENTER).insets(0,5,0,10));
    470507
  • src/org/openstreetmap/josm/plugins/Plugin.java

    r145 r149  
    11package org.openstreetmap.josm.plugins;
     2
     3import java.net.URL;
     4import java.net.URLClassLoader;
    25
    36import org.openstreetmap.josm.Main;
     
    58
    69/**
    7  * All plugins must have at least one class implementing this interface.
     10 * All plugins *must* have an standard constructor taking no arguments.
     11 * This constructor is called at JOSM startup, after all Main-objects have been initialized.
     12 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
     13 * (or else, the plugin jar will not be within the class path).
     14 *
     15 * All plugins should have at least one class subclassing this abstract base class.
    816 *
    9  * All plugins must have an default constructor (taking no arguments). This constructor
    10  * is called at JOSM startup, after all Main-objects have been initialized.
     17 * The actual implementation of this interface is optional, as all functions will be called
     18 * via reflection. This is to be able to change this interface without the need of recompiling
     19 * or even breaking the plugins. If your class does not provide a function here (or does
     20 * provide a function with a mismatching signature), it will not be called. That simple.
    1121 *
    12  * The pluginname is also the name of the directory to store the plugin's
    13  * own stuff (located under the josm preferences directory)
     22 * Or in other words: See this base class as an documentation of what functions are provided.
     23 * Subclassing it and overriding some functions makes it easy for you to keep sync with the
     24 * correct actual plugin architecture of JOSM.
     25 *
     26 *
     27 * The pluginname provided to the constructor is also the name of the directory to
     28 * store the plugin's own stuff (located under the josm preferences directory)
     29 *
    1430 * @author Immanuel.Scholz
    1531 */
    1632public abstract class Plugin {
    1733
    18         private final String name;
     34        private String name;
    1935
    20         public Plugin(String pluginName) {
    21                 this.name = pluginName;
     36        public Plugin() {
     37                URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
     38                String s = urls[urls.length-1].toString();
     39                int lastSlash = s.lastIndexOf('/');
     40                name = s.substring(lastSlash+1, s.length()-4);
     41    }
     42       
     43        /**
     44         * @return The name of this plugin. This is the name of the .jar file.
     45         */
     46        public final String getName() {
     47                return name;
    2248        }
    23 
     49        /**
     50         * @return The directory for the plugin to store all kind of stuff.
     51         */
    2452        public final String getPluginDir() {
    2553                return Main.pref.getPreferencesDir()+"plugins/"+name+"/";
    2654        }
    2755
     56       
     57
    2858        /**
    2959         * Called after Main.mapFrame is initalized. (After the first data is loaded).
     60         * You can use this callback to tweak the newFrame to your needs, as example install
     61         * an alternative Painter.
    3062         */
    3163        public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
     64
     65        /**
     66         * Called to retrieve a one-liner description of what this plugin does for tooltips.
     67         * @return <code>null</code>, which means: "no description available".
     68         */
     69        public String getDescription() {return null;}
     70
    3271}
  • src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r113 r149  
    2121
    2222import org.openstreetmap.josm.Main;
     23import org.openstreetmap.josm.plugins.PluginException;
     24import org.openstreetmap.josm.plugins.PluginProxy;
    2325
    2426/**
     
    3537                                JOptionPane.showMessageDialog(Main.parent, "You are out of memory. Strange things may happen.\nPlease restart JOSM and load smaller data sets.");
    3638                                return;
     39                        }
     40                       
     41                        if (e instanceof PluginException) {
     42                                PluginProxy plugin = ((PluginException)e).getPlugin();
     43                                if (plugin != null && !plugin.misbehaving) {
     44                                        JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} throwed an exception: {1}\nIt may be outdated. Please contact the plugin's autor.\nThis message will not shown again until JOSM is restarted.", plugin.name, e.getMessage()));
     45                                        plugin.misbehaving = true;
     46                                        return;
     47                                }
    3748                        }
    3849                       
  • src/org/openstreetmap/josm/tools/OpenBrowser.java

    r69 r149  
    1414        public static String displayUrl(String url) {
    1515                String os = System.getProperty("os.name");
     16                if (os == null)
     17                        return "unknown operating system";
    1618                try {
    1719                        if (os != null && os.startsWith("Windows"))
    18                                 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
    19                         else {
    20                                 //...
    21                         }
     20                                windows(url);
     21                        else if (os.equals("Linux") || os.equals("Solaris") || os.equals("SunOS") || os.equals("AIX") || os.equals("FreeBSD"))
     22                                linux(url);
     23                        else if (os.equals("Mac OS") || os.equals("Mac OS X"))
     24                                mac(url);
     25                        else
     26                                return "unknown operating system";
    2227                } catch (IOException e) {
    2328                        return e.getMessage();
     
    2530                return null;
    2631        }
     32
     33        private static void windows(String url) throws IOException {
     34                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
     35        }
     36
     37        private static void linux(String url) throws IOException {
     38                try {
     39                Runtime.getRuntime().exec("gnome-open " + url);
     40        } catch (IOException e) {
     41                Runtime.getRuntime().exec("kfmclient openURL " + url);
     42        }
     43        }
     44
     45        private static void mac(String url) throws IOException {
     46                Runtime.getRuntime().exec("open " + url);
     47        }
    2748}
  • src/org/openstreetmap/josm/tools/UrlLabel.java

    r104 r149  
    1414
    1515        public UrlLabel(String url) {
     16                this (url, url);
     17        }
     18
     19        public UrlLabel(String url, String description) {
    1620                this.url = url;
    1721                setContentType("text/html");
    18                 setText("<html><a href=\""+url+"\">"+url+"</a></html>");
     22                setText("<html><a href=\""+url+"\">"+description+"</a></html>");
    1923                setEditable(false);
    2024                setOpaque(false);
Note: See TracChangeset for help on using the changeset viewer.