1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
13 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
14 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * For all purposes of loading dynamic resources, the Plugin's class loader should be used
|
---|
18 | * (or else, the plugin jar will not be within the class path).
|
---|
19 | *
|
---|
20 | * A plugin may subclass this abstract base class (but it is optional).
|
---|
21 | *
|
---|
22 | * The actual implementation of this class is optional, as all functions will be called
|
---|
23 | * via reflection. This is to be able to change this interface without the need of
|
---|
24 | * recompiling or even breaking the plugins. If your class does not provide a
|
---|
25 | * function here (or does provide a function with a mismatching signature), it will not
|
---|
26 | * be called. That simple.
|
---|
27 | *
|
---|
28 | * Or in other words: See this base class as an documentation of what automatic callbacks
|
---|
29 | * are provided (you can register yourself to more callbacks in your plugin class
|
---|
30 | * constructor).
|
---|
31 | *
|
---|
32 | * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
|
---|
33 | * with the correct actual plugin architecture of JOSM.
|
---|
34 | *
|
---|
35 | * @author Immanuel.Scholz
|
---|
36 | */
|
---|
37 | public abstract class Plugin {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * This is the info available for this plugin. You can access this from your
|
---|
41 | * constructor.
|
---|
42 | *
|
---|
43 | * (The actual implementation to request the info from a static variable
|
---|
44 | * is a bit hacky, but it works).
|
---|
45 | */
|
---|
46 | private PluginInformation info = null;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Creates the plugin
|
---|
50 | *
|
---|
51 | * @param info the plugin information describing the plugin.
|
---|
52 | */
|
---|
53 | public Plugin(PluginInformation info) {
|
---|
54 | this.info = info;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Replies the plugin information object for this plugin
|
---|
59 | *
|
---|
60 | * @return the plugin information object
|
---|
61 | */
|
---|
62 | public PluginInformation getPluginInformation() {
|
---|
63 | return info;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Sets the plugin information object for this plugin
|
---|
68 | *
|
---|
69 | * @parma info the plugin information object
|
---|
70 | */
|
---|
71 | public void setPluginInformation(PluginInformation info) {
|
---|
72 | this.info = info;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * @return The directory for the plugin to store all kind of stuff.
|
---|
77 | */
|
---|
78 | public String getPluginDir() {
|
---|
79 | return new File(Main.pref.getPluginsDirectory(), info.name).getPath();
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Called after Main.mapFrame is initalized. (After the first data is loaded).
|
---|
84 | * You can use this callback to tweak the newFrame to your needs, as example install
|
---|
85 | * an alternative Painter.
|
---|
86 | */
|
---|
87 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Called in the preferences dialog to create a preferences page for the plugin,
|
---|
91 | * if any available.
|
---|
92 | */
|
---|
93 | public PreferenceSetting getPreferenceSetting() { return null; }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Called in the download dialog to give the plugin a chance to modify the list
|
---|
97 | * of bounding box selectors.
|
---|
98 | */
|
---|
99 | public void addDownloadSelection(List<DownloadSelection> list) {}
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Copies the resource 'from' to the file in the plugin directory named 'to'.
|
---|
103 | */
|
---|
104 | public void copy(String from, String to) throws FileNotFoundException, IOException {
|
---|
105 | String pluginDirName = getPluginDir();
|
---|
106 | File pluginDir = new File(pluginDirName);
|
---|
107 | if (!pluginDir.exists()) {
|
---|
108 | pluginDir.mkdirs();
|
---|
109 | }
|
---|
110 | FileOutputStream out = new FileOutputStream(new File(pluginDirName, to));
|
---|
111 | InputStream in = getClass().getResourceAsStream(from);
|
---|
112 | byte[] buffer = new byte[8192];
|
---|
113 | for(int len = in.read(buffer); len > 0; len = in.read(buffer)) {
|
---|
114 | out.write(buffer, 0, len);
|
---|
115 | }
|
---|
116 | in.close();
|
---|
117 | out.close();
|
---|
118 | }
|
---|
119 | }
|
---|