Version 38 (modified by 16 years ago) ( diff ) | ,
---|
Developing Plugins
This page gives a short introduction for developers how to create, deploy and develop plugins for JOSM. Any Questions left? Ask at the developers mailinglist.
Setting up the environment
- Check out the plugin environment into an empty directory called
josm
svn co http://svn.openstreetmap.org/applications/editors/josm josm
- create a new plugin-directory
josm/plugins/yourpluginname
. You may create a copy of the template directory 00_plugin_dir_template. It includes a directory layout, a license file and a template for the build.xml. - Open the ant script (
build.xml
) in your plugin directory and configure the properties in the configuration section. The important thing of your build script is, that it places some attributes into theMANIFEST.MF
of the jar file. See below.build.xml
in the template directory takes care of this. - This readme explains how your plugin is built and made available to other JOSM users.
- Also note, that JOSM is compiled into Java5 compatible class files, so if you are using Java6 or Java7, specify "-target 1.5" as parameter to javac or your plugin will not be usable with the official josm builds.
- If you need other OSM resources you can check out the complete osm - trunk:
svn co http://svn.openstreetmap.org/
The Plugin class
After setting everything up, you need to create the plugin main class specified in the jar's manifest. The only prerequisite of this class is, that it must have a standard constructor taking no arguments. The constructor of the plugin class will be called during the JOSM startup if the plugin is registered. Here, you have full access to the object tree within JOSM.
It is recommended (but not required), that you subclass org.openstreetmap.josm.plugins.Plugin. This way you get some helper functions and always have the latest callback function signatures (see below) as hints in your baseclass to override them ;)
The required minimum in naming is one individual part in the path structure of you plugin class names. The path of the base class is used to detect the source of exceptions. So when you base class is my.example.class.MyPlugin, then "my.example.class" is used to detect your plugin code. All important parts of your plugin should use that and also it should be unique for the plugin (i.e. add at least one part with the name of your plugin).
Callbacks
Most of your plugin work will probably be tweaking the JOSM object tree at startup time, but there are a few callbacks that get automatically called on every plugin (whether you subclass Plugin or not, just the signature matters).
mapFrameInitialized
This is called after the first data has been loaded and the mapFrame is initialized. Use this to change something on the new MapFrame (or more common: the MapView (THE map ;) behind the frame)- As example, you could add yourself as LayerChangeListener to newMapFrame.mapView (please don't forget to remove yourself from oldMapFrame, if newMapFrame is null). This way you can capture changes of the active layer or the addition/removal of layers.
getPreferenceSetting
This will be called to retrieve an creator for GUI elements for the preferences dialog. The return is an interface with two methods: one to add the GUI elements to the dialog and on that is called when the preferences dialog finishes with ok. Be sure not to write preferences in your GUI code, but remember settings and only write them in the ok() method.
addDownloadSelection
This will be called whenever the user pops up the download dialog. The download dialog is prepared by first assembling a list of objects implementing the DownloadSelection interface, and after that giving each of them, via a call to their addGui method, the chance of adding a tab to the download dialog's tabpane. The DownloadSelection interface also has a method that is called by the download dialog if one of the other tabs changes the bounding box, and in turn the GUI elements created by addGui are expected to call boundingBoxChanged on the download dialog if they want to communicate a change to other tabs. The name addDownloadSelection is not 100% accurate as a plugin is also allowed to modify the existing list of DownloadSelection objects. For example, a plugin might want to replace the existing Bookmark handler; it can do so by finding the BookmarkSelection object in the list passed to addDownloadSelection and remove it.
Some tips about the JOSM object tree or What is where?
The initial JOSM author preferred public fields in Java classes over public methods, including public getters and setters. He justified this decision as follows:
First some words about my style of accessing public variables. Most people find this annoying and bad coding style in Java. If this would be an enterprise project, where most of the code is glue code and had to work with objects in a generic way, I would agree with them. But as JOSM is not, I like to keep the classes as simple as possible, which includes, that I don't add standard getter/setter but make the variable public. Also, there are no or very few so-called singleton-factories in JOSM that became popular in the past years. I use to reference singleton objects as global statics. This is unusual but equivalent to having stuff like Dependency Injection or Factory Methods (except you want to make complex things like auto-distributing stuff as seen in some enterprise programs).
Most of the current JOSM authors don't follow this approach and in 2008 and 2009 large parts of the JOSM code base have been refactored in order to improve the maintenability and stability of the code. In particular, this holds for the data classes. In plugins, you're encouraged to follow the well-established principles of encapsulation and information hiding, too.
There are still a couple of global objects accessible through Main, though.
Main.parent
This is the parent of all GUI elements. Use this as first parameter to JOptionPane.show* if you want to popup a message.
Main.pref
This is the (ONLY!) access to the global preferences file, located in .josm/preferences. Use Main.pref.get(...) and Main.pref.put(...) to access the preferences. They will be saved immediately after a put, so don't put anything you dont want to have there ;).
Please, prefix custom plugin preferences with your plugin name.
Main.proj
If you want to translate between Lat/Lon and East/North, use Main.proj.latlon2eastnorth and Main.proj.eastnorth2latlon.
Main.map.mapView
This is the GUI class of the map. You usually access this to call stuff like getCenter, getScale or zoomTo.
BEWARE: Main.map can be null, in which case no data is loaded yet.
Main.main.editLayer()
Call this to get the current OsmDataLayer. If there is no such layer, null is returned.
Plugin.getPluginDir() and Plugin.copy()
If you subclass from the plugin class (see above), then you can call this functions to get your plugin directory or copy data into your plugin directory. This is a directory where you can freely store/read data from. It is located at ~/.josm/plugins/youpluginname.
The MANIFEST.MF
You have to put some information into the manifest file of your jar. If you use ant, you can set these values within the build.xml
file.
Plugin-Mainversion | required | The lowest JOSM version required by this plugin. |
Plugin-Version | required | The SVN revision of the plugin SVN repository the plugin was built against |
Plugin-Class | required | Points to the main class of the plugin |
Plugin-Description | required | Gives the description of the plugin visible in the preferences page. For line breaks, you have to use <br> , not <br/>
|
Author | optional | The name and or email address of the author of this plugin. This is used in the error report window, if an error is detected within the plugin code. |
Plugin-Date | optional | The creation date of the plugin in ISO format. |
Plugin-Early | optional | Can be set to true , in which case the plugin is loaded as early as possible, more specific before the GUI classes are loaded. This is usefull if your plugin alters the GUI or the JOSM-startup process in any way.
|
Plugin-Link | optional | Informational URL to a webpage or other information source about that plugin. Is also used in the plugins information page. |
Plugin-Requires | optional | A list of other plugins which are required before plugin works. The list is separated by semi colons. |
Plugin-Stage | optional | An number of the order relative to other plugins, when the plugin is loaded. Smaller numbers gets loaded first, so if you have conflicts with other plugins, you can increase or decrease this number to get some control on the loading order. Defaults to 50. |
Class-Path | optional | An space-seperated list of additional classpaths your plugin wants to use when looking for ressources and classes. The plugin itself is added automatically. Don't forget to provide the additional jar's as well, if you add dependencies here. Note that all loaded plugins are in the class-path automatically, so don't specify plugin-dependencies here. |
<xxx>_Plugin-Url | optional | To support older JOSM version special entries may be added to supply older versions. This information is used by the internal plugin handler to select only matching version when updating. The entries are made like this <josm_version>_Plugin-Url: <plugin_version>;<url> |
<lang>_Plugin-Description | optional | The translated description text for the plugin. E.g. de_Plugin-Description contains the German translation. |
For SVN managed plugins, the links to old versions and the translated descriptions are automatically added to the plugin information, so JOSM can use that when it displays the list of plugins in the preference dialog.
Updating a plugin
You have found a bug in a plugin and you are able to fix it. Then your next steps are:
Fixing the bug
- fix the bug in the plugin, compile it, run it locally in JOSM, and test it
Publishing the new plugin
- Did you change something which will not work with every JOSM version? Then you should update Plugin-Mainversion in
build.xml
- look for the line
<attribute name="Plugin-Mainversion" value="..."/>
- replace the value by the lowest JOSM version required for the plugin after you've applied your fix
Do not increase Plugin-Mainversion if not necessary, though. If you do, JOSM requires users to update to the new version, otherwise users can choose whether they want to upgrade.Plugin-Mainversion
should always consist of the lowest possible revision.
- look for the line
- commit the new modified source
- counter intuitive, but important - update again from the SVN - after having commited you must update the source again from the SVN. This ensures that
Plugin-Version
in the pluginMANIFEST
will reflect the plugins SVN revision number - build the plugin using
ant clean
andant dist
. This creates the plugin jar file in the/dist
directory. - commit the
.jar
file in thedist
directory
Closing trac ticket
- Did you fix the bug based on a trac ticket? Please close it and leave a note. You can refer to the new plugin version using the macro
[o12345]
, where 12345 is a plugin revision number.
Ready. The new plugin version is now available. If necessary, JOSM asks users to upgrade to the new version, when JOSM is started up.
The steps describe above can be automated, see build.xml of the tageditor plugin. It includes an ant target publish
.
Legal stuff (Imis opinion)
Just because I have been asked: JOSM is licensed under GPL and if any code is a "derived work" of JOSM, then it has to be under GPL too. It is my believe, that any JOSM-Plugin is a derived work of JOSM, so GPL is the only possible license for a JOSM-Plugin. If you want to include non-GPL code into a plugin, it has to be seperated from the classes that use JOSM. "Use" as in "import org.openstreetmap.josm...". See the 'Class-Path' - MANIFEST.MF attribute for a way to include other jar files.