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