[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 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 | * 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
|
---|
[1169] | 26 | * via reflection. This is to be able to change this interface without the need of
|
---|
[626] | 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).
|
---|
[1169] | 34 | *
|
---|
[626] | 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 | */
|
---|
| 40 | public abstract class Plugin {
|
---|
| 41 |
|
---|
[1169] | 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;
|
---|
[626] | 50 |
|
---|
[1169] | 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 | }
|
---|
[626] | 57 |
|
---|
[1169] | 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) {}
|
---|
[626] | 64 |
|
---|
[1169] | 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+"/";
|
---|
[626] | 82 | File pluginDir = new File(pluginDirName);
|
---|
| 83 | if (!pluginDir.exists())
|
---|
[1169] | 84 | pluginDir.mkdirs();
|
---|
[626] | 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))
|
---|
[1169] | 89 | out.write(buffer, 0, len);
|
---|
[626] | 90 | in.close();
|
---|
| 91 | out.close();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|