1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import java.lang.reflect.InvocationTargetException;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
9 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
10 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
11 | import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Helper class for the JOSM system to communicate with the plugin.
|
---|
15 | *
|
---|
16 | * This class should be of no interest for sole plugin writer.
|
---|
17 | *
|
---|
18 | * @author Immanuel.Scholz
|
---|
19 | */
|
---|
20 | public class PluginProxy extends Plugin {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The plugin.
|
---|
24 | */
|
---|
25 | public final Object plugin;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code PluginProxy}.
|
---|
29 | * @param plugin the plugin
|
---|
30 | * @param info the associated plugin info
|
---|
31 | */
|
---|
32 | public PluginProxy(Object plugin, PluginInformation info) {
|
---|
33 | super(info);
|
---|
34 | this.plugin = plugin;
|
---|
35 | }
|
---|
36 |
|
---|
37 | private void handlePluginException(Exception e) {
|
---|
38 | PluginHandler.pluginLoadingExceptions.put(getPluginInformation().name, e);
|
---|
39 | BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
44 | try {
|
---|
45 | plugin.getClass().getMethod("mapFrameInitialized", MapFrame.class, MapFrame.class).invoke(plugin, oldFrame, newFrame);
|
---|
46 | } catch (NoSuchMethodException e) {
|
---|
47 | Main.trace(e);
|
---|
48 | Main.debug("Plugin "+plugin+" does not define mapFrameInitialized");
|
---|
49 | } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
|
---|
50 | handlePluginException(e);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public PreferenceSetting getPreferenceSetting() {
|
---|
56 | try {
|
---|
57 | return (PreferenceSetting) plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin);
|
---|
58 | } catch (NoSuchMethodException e) {
|
---|
59 | Main.trace(e);
|
---|
60 | Main.debug("Plugin "+plugin+" does not define getPreferenceSetting");
|
---|
61 | return null;
|
---|
62 | } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
|
---|
63 | handlePluginException(e);
|
---|
64 | }
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void addDownloadSelection(List<DownloadSelection> list) {
|
---|
70 | try {
|
---|
71 | plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list);
|
---|
72 | } catch (NoSuchMethodException e) {
|
---|
73 | Main.trace(e);
|
---|
74 | Main.debug("Plugin "+plugin+" does not define addDownloadSelection");
|
---|
75 | } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
|
---|
76 | handlePluginException(e);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|