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