[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.plugins;
|
---|
| 3 |
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.FileNotFoundException;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.io.InputStream;
|
---|
[5241] | 8 | import java.net.URL;
|
---|
| 9 | import java.net.URLClassLoader;
|
---|
[9280] | 10 | import java.nio.file.Files;
|
---|
| 11 | import java.nio.file.StandardCopyOption;
|
---|
[6282] | 12 | import java.security.AccessController;
|
---|
| 13 | import java.security.PrivilegedAction;
|
---|
[626] | 14 | import java.util.List;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
| 17 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[5957] | 18 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
[626] | 19 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
| 20 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
[5874] | 21 | import org.openstreetmap.josm.tools.Utils;
|
---|
[626] | 22 |
|
---|
| 23 | /**
|
---|
| 24 | * For all purposes of loading dynamic resources, the Plugin's class loader should be used
|
---|
| 25 | * (or else, the plugin jar will not be within the class path).
|
---|
| 26 | *
|
---|
| 27 | * A plugin may subclass this abstract base class (but it is optional).
|
---|
| 28 | *
|
---|
| 29 | * The actual implementation of this class is optional, as all functions will be called
|
---|
[1169] | 30 | * via reflection. This is to be able to change this interface without the need of
|
---|
[626] | 31 | * recompiling or even breaking the plugins. If your class does not provide a
|
---|
| 32 | * function here (or does provide a function with a mismatching signature), it will not
|
---|
| 33 | * be called. That simple.
|
---|
| 34 | *
|
---|
| 35 | * Or in other words: See this base class as an documentation of what automatic callbacks
|
---|
| 36 | * are provided (you can register yourself to more callbacks in your plugin class
|
---|
| 37 | * constructor).
|
---|
[1169] | 38 | *
|
---|
[626] | 39 | * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
|
---|
| 40 | * with the correct actual plugin architecture of JOSM.
|
---|
| 41 | *
|
---|
| 42 | * @author Immanuel.Scholz
|
---|
| 43 | */
|
---|
[5957] | 44 | public abstract class Plugin implements MapFrameListener {
|
---|
[626] | 45 |
|
---|
[1169] | 46 | /**
|
---|
| 47 | * This is the info available for this plugin. You can access this from your
|
---|
| 48 | * constructor.
|
---|
| 49 | *
|
---|
| 50 | * (The actual implementation to request the info from a static variable
|
---|
| 51 | * is a bit hacky, but it works).
|
---|
| 52 | */
|
---|
[8840] | 53 | private PluginInformation info;
|
---|
[626] | 54 |
|
---|
[1169] | 55 | /**
|
---|
[2830] | 56 | * Creates the plugin
|
---|
[3530] | 57 | *
|
---|
[2830] | 58 | * @param info the plugin information describing the plugin.
|
---|
| 59 | */
|
---|
| 60 | public Plugin(PluginInformation info) {
|
---|
| 61 | this.info = info;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Replies the plugin information object for this plugin
|
---|
[3530] | 66 | *
|
---|
[2830] | 67 | * @return the plugin information object
|
---|
| 68 | */
|
---|
| 69 | public PluginInformation getPluginInformation() {
|
---|
| 70 | return info;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Sets the plugin information object for this plugin
|
---|
[3530] | 75 | *
|
---|
[5836] | 76 | * @param info the plugin information object
|
---|
[2830] | 77 | */
|
---|
| 78 | public void setPluginInformation(PluginInformation info) {
|
---|
| 79 | this.info = info;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
[1169] | 83 | * @return The directory for the plugin to store all kind of stuff.
|
---|
| 84 | */
|
---|
[3530] | 85 | public String getPluginDir() {
|
---|
[2817] | 86 | return new File(Main.pref.getPluginsDirectory(), info.name).getPath();
|
---|
[1169] | 87 | }
|
---|
[626] | 88 |
|
---|
[5957] | 89 | @Override
|
---|
[1169] | 90 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
|
---|
[626] | 91 |
|
---|
[1169] | 92 | /**
|
---|
| 93 | * Called in the preferences dialog to create a preferences page for the plugin,
|
---|
| 94 | * if any available.
|
---|
[8928] | 95 | * @return the preferences dialog, or {@code null}
|
---|
[1169] | 96 | */
|
---|
[8513] | 97 | public PreferenceSetting getPreferenceSetting() {
|
---|
| 98 | return null;
|
---|
| 99 | }
|
---|
[1169] | 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Called in the download dialog to give the plugin a chance to modify the list
|
---|
| 103 | * of bounding box selectors.
|
---|
[9231] | 104 | * @param list list of bounding box selectors
|
---|
[1169] | 105 | */
|
---|
| 106 | public void addDownloadSelection(List<DownloadSelection> list) {}
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
[2927] | 109 | * Copies the resource 'from' to the file in the plugin directory named 'to'.
|
---|
[9231] | 110 | * @param from source file
|
---|
| 111 | * @param to target file
|
---|
[8506] | 112 | * @throws FileNotFoundException if the file exists but is a directory rather than a regular file,
|
---|
| 113 | * does not exist but cannot be created, or cannot be opened for any other reason
|
---|
| 114 | * @throws IOException if any other I/O error occurs
|
---|
[1169] | 115 | */
|
---|
[8506] | 116 | public void copy(String from, String to) throws IOException {
|
---|
[3530] | 117 | String pluginDirName = getPluginDir();
|
---|
[626] | 118 | File pluginDir = new File(pluginDirName);
|
---|
[2817] | 119 | if (!pluginDir.exists()) {
|
---|
[1169] | 120 | pluginDir.mkdirs();
|
---|
[2817] | 121 | }
|
---|
[9280] | 122 | try (InputStream in = getClass().getResourceAsStream(from)) {
|
---|
[5987] | 123 | if (in == null) {
|
---|
| 124 | throw new IOException("Resource not found: "+from);
|
---|
| 125 | }
|
---|
[9280] | 126 | Files.copy(in, new File(pluginDirName, to).toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
[2817] | 127 | }
|
---|
[626] | 128 | }
|
---|
[5241] | 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Get a class loader for loading resources from the plugin jar.
|
---|
| 132 | *
|
---|
| 133 | * This can be used to avoid getting a file from another plugin that
|
---|
| 134 | * happens to have a file with the same file name and path.
|
---|
| 135 | *
|
---|
| 136 | * Usage: Instead of
|
---|
| 137 | * getClass().getResource("/resources/pluginProperties.properties");
|
---|
| 138 | * write
|
---|
| 139 | * getPluginResourceClassLoader().getResource("resources/pluginProperties.properties");
|
---|
| 140 | *
|
---|
| 141 | * (Note the missing leading "/".)
|
---|
[8928] | 142 | * @return a class loader for loading resources from the plugin jar
|
---|
[5241] | 143 | */
|
---|
| 144 | public ClassLoader getPluginResourceClassLoader() {
|
---|
| 145 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
| 146 | File pluginJar = new File(pluginDir, info.name + ".jar");
|
---|
[6615] | 147 | final URL pluginJarUrl = Utils.fileToURL(pluginJar);
|
---|
[6282] | 148 | return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
|
---|
[9231] | 149 | @Override
|
---|
[6282] | 150 | public ClassLoader run() {
|
---|
| 151 | return new URLClassLoader(new URL[] {pluginJarUrl}, Main.class.getClassLoader());
|
---|
| 152 | }
|
---|
| 153 | });
|
---|
[5241] | 154 | }
|
---|
[626] | 155 | }
|
---|