|
Extensions which wish to add a menu item to BlueJ's menus should
register an instance of MenuGenerator with the BlueJ proxy object.
A MenuGenerator provides a set of functions which can be called
back by BlueJ to request the actual menu items which will be displayed,
and to indicate that a particular menu item is about to be displayed,
so that an extension can (e.g.) enable or disable appropriate items.
Note that the JMenuItem which is returned by the extension can itself
be a JMenu, allowing extensions to build more complex menu structures,
but that the "notify" methods below will only be called
for the item which has actually been added, and not any subsidiary
items. Below is a simple example which creates menus for Tools,
Classes and Objects. To activate the menus you instantiate an object
of the ExtensionMenu class and then register it with the BlueJ proxy
object, e.g.:
MenuBuilder myMenus = new MenuBuilder();
bluej.setMenuGenerator(myMenus);
Note that the MenuGenerator's get*MenuItem() methods:
- may be called more than once during a BlueJ session, they should
return a new set of MenuItems for each invocation. This is a restriction
required by the Swing implementation, which does not allow sharing
of MenuItems between menus. You can, of course, share MenuActions
between all of the appropriate MenuItems.
- may not be called between the registration of a new MenuGenerator
and the display of a menu. That is to say old menu items may still
be active for previously registered menus, despite the registration
of a new MenuGenerator.
- will be called at least once for every menu which is displayed.
The source code for this example MenuGenerator is:
class MenuBuilder extends MenuGenerator { private ToolsAction aToolsAction; private ClassAction aClassAction; private ObjectAction aObjectAction; private BPackage curPackage; private BClass curClass; private BObject curObject;
MenuBuilder() {
aToolsAction = new ToolsAction("Click Tools");
aClassAction = new ClassAction("Click Class");
aObjectAction = new ObjectAction("Click Object");
}
public JMenuItem getToolsMenuItem(BPackage aPackage) {
return new JMenuItem(aToolsAction);
}
public JMenuItem getClassMenuItem(BClass aClass) {
return new JMenuItem(aClassAction);
}
public JMenuItem getObjectMenuItem(BObject anObject) {
return new JMenuItem(aObjectAction);
}
// A utility method which prints the objects involved in the current
// menu invocation.
private void printCurrentStatus(String header) {
try {
if (curObject != null)
curClass = curObject.getBClass();
if (curClass != null)
curPackage = curClass.getPackage();
System.out.println(header);
if (curPackage != null)
System.out.println(" Current Package=" + curPackage);
if (curClass != null)
System.out.println(" Current Class=" + curClass);
if (curObject != null)
System.out.println(" Current Object=" + curObject);
} catch (Exception exc) { }
}
// Now the nested classes that instantiate the different menus.
class ToolsAction extends AbstractAction {
public ToolsAction(String menuName) {
putValue(AbstractAction.NAME, menuName);
}
public void actionPerformed(ActionEvent anEvent) {
printCurrentStatus("Tools menu:");
}
}
class ClassAction extends AbstractAction {
public ClassAction(String menuName) {
putValue(AbstractAction.NAME, menuName);
}
public void actionPerformed(ActionEvent anEvent) {
printCurrentStatus("Class menu:");
}
}
class ObjectAction extends AbstractAction {
public ObjectAction(String menuName) {
putValue(AbstractAction.NAME, menuName);
}
public void actionPerformed(ActionEvent anEvent) {
printCurrentStatus("Object menu:");
}
}
// and the methods which will be called in the main class when
// each of the different menus are about to be invoked.
public void notifyPostToolsMenu(BPackage bp, JMenuItem jmi) {
System.out.println("Post on Tools menu");
curPackage = bp ; curClass = null ; curObject = null;
}
public void notifyPostClassMenu(BClass bc, JMenuItem jmi) {
System.out.println("Post on Class menu");
curPackage = null ; curClass = bc ; curObject = null;
}
public void notifyPostObjectMenu(BObject bo, JMenuItem jmi) {
System.out.println("Post on Object menu");
curPackage = null ; curClass = null ; curObject = bo;
}
}
|