001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins;
003
004import java.util.List;
005
006import org.openstreetmap.josm.Main;
007import org.openstreetmap.josm.gui.MapFrame;
008import org.openstreetmap.josm.gui.download.DownloadSelection;
009import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
010import org.openstreetmap.josm.tools.BugReportExceptionHandler;
011
012/**
013 * Helper class for the JOSM system to communicate with the plugin.
014 *
015 * This class should be of no interest for sole plugin writer.
016 *
017 * @author Immanuel.Scholz
018 */
019public class PluginProxy extends Plugin {
020
021    /**
022     * The plugin.
023     */
024    public final Object plugin;
025
026    /**
027     * Constructs a new {@code PluginProxy}.
028     * @param plugin the plugin
029     * @param info the associated plugin info
030     */
031    public PluginProxy(Object plugin, PluginInformation info) {
032        super(info);
033        this.plugin = plugin;
034    }
035
036    private void handlePluginException(Exception e) {
037        PluginHandler.pluginLoadingExceptions.put(getPluginInformation().name, e);
038        BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
039    }
040
041    @Override
042    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
043        try {
044            plugin.getClass().getMethod("mapFrameInitialized", MapFrame.class, MapFrame.class).invoke(plugin, oldFrame, newFrame);
045        } catch (NoSuchMethodException e) {
046            Main.debug("Plugin "+plugin+" does not define mapFrameInitialized");
047        } catch (Exception e) {
048            handlePluginException(e);
049        }
050    }
051
052    @Override
053    public PreferenceSetting getPreferenceSetting() {
054        try {
055            return (PreferenceSetting) plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin);
056        } catch (NoSuchMethodException e) {
057            Main.debug("Plugin "+plugin+" does not define getPreferenceSetting");
058            return null;
059        } catch (Exception e) {
060            handlePluginException(e);
061        }
062        return null;
063    }
064
065    @Override
066    public void addDownloadSelection(List<DownloadSelection> list) {
067        try {
068            plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list);
069        } catch (NoSuchMethodException e) {
070            Main.debug("Plugin "+plugin+" does not define addDownloadSelection");
071        } catch (Exception e) {
072            handlePluginException(e);
073        }
074    }
075}