Writing BlueJ Extensions

Download Report

Transcript Writing BlueJ Extensions

Writing BlueJ Extensions
Ian Utting
University of Kent
First BlueJ Day, Houston, Texas, 1st March 2006
1
Aims of the Extensions API
• To allow you to do programmatically
what users can do through the BlueJ
GUI:
– Add menu items to the BlueJ GUI for
packages, classes and objects
– Create and interrogate projects,
packages, classes and objects (and their
BlueJ GUI representations)
– Interact with source code through the
editor
First BlueJ Day, Houston, Texas, 1st March 2006
2
Why?
• To allow you to extend BlueJ with
stuff only you need (or you only
need sometimes)
• To use BlueJ as a tool for
researching student behaviour
• (As a source of undergraduate
projects)
First BlueJ Day, Houston, Texas, 1st March 2006
3
Overview – an Extension is:
• A single jar file containing (at least) a
class which extends
bluej.extensions.Extension (the
Jar’s MainClass attribute should name
this class)
• The Jar can be installed system-wide,
per-user or per-project.
• Tip: don’t rely on external class
libraries, bundle them with your
extension
First BlueJ Day, Houston, Texas, 1st March 2006
4
abstract class Extension
• Provides (and requires) version and
compatibility information.
• Requires:
– abstract void startup(BlueJ bluej)
called when the extension can start
– void terminate() called when the
extension is about to be unloaded
• Watch out: version request calls can
occur at any time, don’t use static
initialisers of heavyweight
constructors.
First BlueJ Day, Houston, Texas, 1st March 2006
5
class BlueJ
• Acts as a proxy for the BlueJ
application
• Allows an extension to:
– Create and open BlueJ projects, and
identify the “current” package
– Add Listeners for various events
– Get/set persistent properties
– Register menu and preference panel
generators
First BlueJ Day, Houston, Texas, 1st March 2006
6
Menus
• Extensions can add a menu item (or
sub-menu) to the Tools, Class and
Object menus. Don’t share them, or
hold onto them, or rely on when BlueJ
might ask for them.
• BlueJ will call you back on the
notifyPost methods just before
displaying your menu (so you can
enable/disable your items).
First BlueJ Day, Houston, Texas, 1st March 2006
7
BProject and BPackage
• Represent BlueJ’s view of
projects and packages
• As with BlueJ itself, most of the
action is in packages
• Through BPackage, you can
retrieve, create and delete new
classes and objects, including
BlueJ’s representations of them
First BlueJ Day, Houston, Texas, 1st March 2006
8
BClass, BObject, BMethod, etc.
• Manipulate BlueJ’s set of classes and
objects
• Invoke methods (and constructors)
and place the resulting objects on the
bench
• Designed to look like Java reflection,
and backed up by real reflection (but
watch out for side effects when using
real reflection)
First BlueJ Day, Houston, Texas, 1st March 2006
9
Watching what users are doing
• Using a standard listener
mechanism, an extension can
find out when:
– packages are opened or closed
– classes are compiled (and what
happened)
– objects are invoked
First BlueJ Day, Houston, Texas, 1st March 2006
10
Interacting with the Editor
• Many possible extensions want more
interaction with the user during code
creation
• We provide an interface to the BlueJ
editor for a class, for manipulating
text
• A “property” mechanism allows an
extension to interact with a modified
editor for more detailed interaction
First BlueJ Day, Houston, Texas, 1st March 2006
11
More information
• The Extensions API documentation,
and an example, are at:
www.bluej.org/extensions/extensions.html#write
• If you want to get involved in an
existing extension, consider the
Project Exporter at:
bluej-project-exporter.dev.java.net
• Remember: BlueJ extensions make
great student projects!
First BlueJ Day, Houston, Texas, 1st March 2006
12