source: josm/trunk/src/org/openstreetmap/josm/plugins/Plugin.java@ 9062

Last change on this file since 9062 was 8928, checked in by Don-vip, 8 years ago

javadoc fixes

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