OpenJUMP programming Guideline

From OpenJUMP Wiki
Jump to navigation Jump to search

This section is written to help OpenJUMP programmers

  • to follow general java programming conventions
  • to use OpenJUMP as a library in a consistent way
  • to share good habits and to avoid bad habits

Note: These are guidelines for the practical use to make it other programmers easiere - an not strikt rules. I.e. we just want to share and to improve OpenJUMP code readability.

Follow general (Java) rules

First of all, there is a complete Java Programming Guideline here

Here are my preferences about the position of curly braces and on indentation

Most code in OpenJUMP follow this convention which is also the one I prefer

for (int i = 0 ; i < max ; i++) {
    // your code here
}

The following convention should be avoided

for (int i = 0 ; i < max ; i++)
{
    // your code here
}

About indentation, my preferred way is to use whitespaces and not tabs. This way, you have more chance to keep the original presentation through all the sources with any IDE and with any parameter for your tabulation width (in any case, using tabs to add whitespaces here and there is a bad idea).

OpenJUMP Packages

There are two main packages in the OpenJUMP source code.

  • com.vividsolutions.jump contains the original sources. Classes from this package can be fixed or improved, but functional improvements and new capabilities should be added to org.openjump package
  • org.openjump.core contains all new contributions, i.e. the improvements added after the inital JUMP developement stopped and since OpenJUMP was created

Most of the new plugins should be placed in one of the org.openjump.core.ui.plugin.xxx packages

PlugIns and Extensions

A PlugIn is basically a class that adds a new function to OpenJUMP by use of an "execute" method. A plugin also contains a "initialize" method to allow installation of the feature in the OpenJUMP user interface (GUI). Plugins can be also threaded plugins to avoid that long tasks stop interaction with the user interface. Here the ThreadedBasePlugin is used.

An extension is a class able to initialize one or several plugins. This class is automatically recognised when it is contained in a jar-file which is placed in the /ext/ directory.

You have a good explanation of the plugin architecture in the original JUMP Developer Guide

See also our Developer Documentation and HowTo and the page How to use and make own Plugins

PlugIn names and initialization

The PlugIn name is set in the getName() method of the PlugIn interface. If not overloaded, AbstractPlugin returns a default name, based on the class name. You must overload it to get an internationalized name, e.g.:

public String getName() {return I18N.get("org.core.ui.plugin.MyPlugIn");}

If the plugin is called from the menu, you generally want to place it in an appropiate sub-menu and call it by its name. You define where the plugin is installed in the user interface (e.g. as main menu item, or context menu item or mouse menu item) in plugin's initialize method. E.g. the code below initializes the plugin as main menu item.

context.getFeatureInstaller().addMainMenuItem {
    this,
    new String[] { MenuNames.TOOLS, MenuNames.STATISTICS}, 
    this.getName(), 
    false, 
    null, 
    null);

If the plugin opens a new dialog box, you should add "..." to the name, e.g.

context.getFeatureInstaller().addMainMenuItem {
    this,
    new String[] { MenuNames.TOOLS, MenuNames.STATISTICS}, 
    this.getName() + "...", 
    false, 
    null, 
    null);

Check the MenuNames class to explore available menus and submenus

If you want to activate/deactivate the plugin based on certain conditions, you must use an EnableCheck created with the EnableCheckFactory class, by using MultiEnableCheck, or by directly implementing EnableCheck. EnableCheckFactory can be obtain from the PlugInContext, e.g.:

context.getFeatureInstaller().addMainMenuItem {
    this,
    new String[] { MenuNames.TOOLS, MenuNames.STATISTICS}, 
    this.getName() + "...", 
    false, 
    null, 
    context.getCheckFactory().createAtLeastNLayersMustExistCheck(2));

PS: This page was written by M. Michaud - improved by S. Steiniger