Implementing cursortools

From OpenJUMP Wiki
Jump to navigation Jump to search

To implement a new cursortool, the best way is to study and subclass existant cursortools with a similar behaviour:

  • for example cursortool #DragTool if your tool needs a mouse drag;
  • or cursortool#NClickTool if your tool uses several clicks

(see Jump Developer's Guide chapter 6. for more informations on cursortool classes)

Let's create a simple cursortool that displays cursor coordinates in a java console, each time we click once in the layerViewPanel (not a very useful tool...):

public class CoordinateCursorTool extends AbstractCursorTool{
     private Coordinate actualCoord = new Coordinate(); 
     public RasterQueryCursorTool() //constructor does nothing special here

     public Icon getIcon() {
     return IconLoader.icon("My_toolbar_Icon.gif");
     //specify here the toolbar icon file (button image) to use%

     public Cursor getCursor() {
     return createCursor(IconLoader.icon("My_Cursor.gif")
            .getImage()); //specify here the cursor image file to use

    public void deactivate() {

    public boolean isRightMouseButtonUsed() {
        return false;

    public void mouseClicked(MouseEvent e) {
        try {//add here the code to    execute when the mouse is clicked%
            actualCoord = getPanel().getViewport().toModelCoordinate(
	system.out.println(actualCoord); //Display coordinates as a string
        } catch (NoninvertibleTransformException e1) {
	fireGestureFinished();  //Notify that mouse gesture is finished 

     public boolean isGestureInProgress() {
        return false;

    public void cancelGesture() {

    public String getName() {
        return "CoordinateTool";

    protected Shape getShape() throws Exception {
        return null; //because nothing should be drawn here

    protected void gestureFinished() throws Exception {


Then we add our cursortool to the workbench toolbar. This can be done in the initialize() method of a new plugin:

     public void initialize(PlugInContext context){
      WorkbenchFrame frame = context.getWorkbenchContext()

         frame.getToolBar().addCursorTool(new CoordinateCursorTool());


In the workbench, you have now a new button. Press it and the CoordinateCursortool becomes active (the #activate method is called). Have fun!

Thanks to Paul Plouy for writing up the tutorial above. Note that there are some subclasses of AbstractCursorTool that you can subclass to get additional power for free; for example: MultiClickTool, SpecifyFeaturesTool, etc. For examples of their use, see the classes in com.vividsolutions.jump.workbench.ui.cursortool [Jon Aquino 2005-09-22]

There are several good examples of CursorTools in the OpenJUMP source code. Check out the cursortools folder. [Jon Aquino 2005-11-04]