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

Last change on this file since 292 was 292, checked in by imi, 17 years ago
  • fixed Bug Report module to work better with plugins
  • fixed Plugin.getPreferenceDir and Plugin.copy
  • fixed Ctrl-Q not asking for changed data
  • fixed several typos in Shortcuts
  • removed check for latest josm version
File size: 3.4 KB
Line 
1package org.openstreetmap.josm.plugins;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.List;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.gui.MapFrame;
12import org.openstreetmap.josm.gui.download.DownloadSelection;
13import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
14
15/**
16 * All plugins *must* have an standard constructor taking no arguments.
17 *
18 * This constructor is called at JOSM startup, after all Main-objects have been initialized.
19 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
20 * (or else, the plugin jar will not be within the class path).
21 *
22 * A plugin may subclass this abstract base class (but it is optional).
23 *
24 * The actual implementation of this class is optional, as all functions will be called
25 * via reflection. This is to be able to change this interface without the need of
26 * recompiling or even breaking the plugins. If your class does not provide a
27 * function here (or does provide a function with a mismatching signature), it will not
28 * be called. That simple.
29 *
30 * Or in other words: See this base class as an documentation of what automatic callbacks
31 * are provided (you can register yourself to more callbacks in your plugin class
32 * constructor).
33 *
34 * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
35 * with the correct actual plugin architecture of JOSM.
36 *
37 * @author Immanuel.Scholz
38 */
39public abstract class Plugin {
40
41 /**
42 * This is the info available for this plugin. You can access this from your
43 * constructor.
44 *
45 * (The actual implementation to request the info from a static variable
46 * is a bit hacky, but it works).
47 */
48 public final PluginInformation info = PluginInformation.currentPluginInitialization;
49
50 /**
51 * @return The directory for the plugin to store all kind of stuff.
52 */
53 public final String getPluginDir() {
54 return Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
55 }
56
57 /**
58 * Called after Main.mapFrame is initalized. (After the first data is loaded).
59 * You can use this callback to tweak the newFrame to your needs, as example install
60 * an alternative Painter.
61 */
62 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
63
64 /**
65 * Called in the preferences dialog to create a preferences page for the plugin,
66 * if any available.
67 */
68 public PreferenceSetting getPreferenceSetting() { return null; }
69
70 /**
71 * Called in the download dialog to give the plugin a chance to modify the list
72 * of bounding box selectors.
73 */
74 public void addDownloadSelection(List<DownloadSelection> list) {}
75
76 /**
77 * Copies the ressource 'from' to the file in the plugin directory named 'to'.
78 */
79 public void copy(String from, String to) throws FileNotFoundException, IOException {
80 String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
81 File pluginDir = new File(pluginDirName);
82 if (!pluginDir.exists())
83 pluginDir.mkdirs();
84 FileOutputStream out = new FileOutputStream(pluginDirName+to);
85 InputStream in = getClass().getResourceAsStream(from);
86 byte[] buffer = new byte[8192];
87 for(int len = in.read(buffer); len > 0; len = in.read(buffer))
88 out.write(buffer, 0, len);
89 in.close();
90 out.close();
91 }
92}
Note: See TracBrowser for help on using the repository browser.