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.debug("Plugin "+plugin+" does not define mapFrameInitialized"); 048 } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { 049 handlePluginException(e); 050 } 051 } 052 053 @Override 054 public PreferenceSetting getPreferenceSetting() { 055 try { 056 return (PreferenceSetting) plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin); 057 } catch (NoSuchMethodException e) { 058 Main.debug("Plugin "+plugin+" does not define getPreferenceSetting"); 059 return null; 060 } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { 061 handlePluginException(e); 062 } 063 return null; 064 } 065 066 @Override 067 public void addDownloadSelection(List<DownloadSelection> list) { 068 try { 069 plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list); 070 } catch (NoSuchMethodException e) { 071 Main.debug("Plugin "+plugin+" does not define addDownloadSelection"); 072 } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { 073 handlePluginException(e); 074 } 075 } 076}