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    public final Object plugin;
022
023    public PluginProxy(Object plugin, PluginInformation info) {
024        super(info);
025        this.plugin = plugin;
026    }
027
028    @Override public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
029        try {
030            plugin.getClass().getMethod("mapFrameInitialized", MapFrame.class, MapFrame.class).invoke(plugin, oldFrame, newFrame);
031        } catch (NoSuchMethodException e) {
032            Main.debug("Plugin "+plugin+" does not define mapFrameInitialized");
033        } catch (Exception e) {
034            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
035        }
036    }
037
038    @Override public PreferenceSetting getPreferenceSetting() {
039        try {
040            return (PreferenceSetting)plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin);
041        } catch (NoSuchMethodException e) {
042            Main.debug("Plugin "+plugin+" does not define getPreferenceSetting");
043            return null;
044        } catch (Exception e) {
045            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
046        }
047        return null;
048    }
049
050    @Override public void addDownloadSelection(List<DownloadSelection> list) {
051        try {
052            plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list);
053        } catch (NoSuchMethodException e) {
054            Main.debug("Plugin "+plugin+" does not define addDownloadSelection");
055        } catch (Exception e) {
056            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
057        }
058    }
059}