Provides access for extensions to the BlueJ application via a proxy object, and to the classes and objects which BlueJ is manipulating via a number of wrapper classes.

An extension can add a menu item to the BlueJ Tools, Class and/or Object menus, and a preference panel to the Tools/Preferences/Extensions panel. The BlueJ proxy object generates events when the user performs significant actions within BlueJ, and provides access to its projects, packages, classes and objects via a set of wrapper classes which behave in a similar way to the java.lang.reflect classes.

The editor sub-package provides access from an extension to the BlueJ editor, which can be used either to interact with the user's use of the editor, or to modify the text being edited.

A simple extension

The following example implements an extension which logs the name of every BlueJ project opened by the user to System.out. Once it is installed you should see an entry in the "Installed Extensions" panel of the BlueJ help menu.

How to build and install it

Note to Mac users: To navigate to the lib/extensions directory of your BlueJ installation, right-click (or control-click) on the BlueJ application icon, the select "Show Package Contents"and then extensions. The Mac equivalent of <USER_HOME>\bluej\extensions is ~/Library/Preferences/org.bluej/extensions.

Sample extension code

import bluej.extensions.*;
import bluej.extensions.event.*;

import java.net.URL;

/*
 * This is the starting point of a BlueJ Extension
 */
public class SimpleExtension extends Extension implements PackageListener {
    /*
     * When this method is called, the extension may start its work.
     */
    public void startup (BlueJ bluej) {
        // Listen for BlueJ events at the "package" level
        bluej.addPackageListener(this);
    }

    /*
     * A package has been opened. Print the name of the project it is part of.
     * System.out is redirected to the BlueJ debug log file.
     * The location of this file is given in the Help/About BlueJ dialog box.
     */
    public void packageOpened ( PackageEvent ev ) {
        try {
            System.out.println ("Project " + ev.getPackage().getProject().getName() + " opened.");
        } catch (ExtensionException e) {
            System.out.println("Project closed by BlueJ");
        }
    }  
  
    /*
     * A package is closing.
     */
    public void packageClosing ( PackageEvent ev ) {
    }  
    
    /*
     * This method must decide if this Extension is compatible with the 
     * current release of the BlueJ Extensions API
     */
    public boolean isCompatible () { 
        return true; 
    }

    /*
     * Returns the version number of this extension
     */
    public String  getVersion () { 
        return ("2004.09");  
    }

    /*
     * Returns the user-visible name of this extension
     */
    public String  getName () { 
        return ("Simple Extension");  
    }

    public void terminate() {
        System.out.println ("Simple extension terminates");
    }
    
    public String getDescription () {
        return ("A simple extension");
    }

    /*
     * Returns a URL where you can find info on this extension.
     * The real problem is making sure that the link will still be alive in three years...
     */
    public URL getURL () {
        try {
            return new URL("http://www.bluej.org/doc/writingextensions.html");
        } catch ( Exception eee ) {
            // The link is either dead or otherwise unreachable
            System.out.println ("Simple extension: getURL: Exception="+eee.getMessage());
            return null;
        }
    }
}