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

Last change on this file since 2602 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

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