|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectbluej.extensions.MenuGenerator
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 disp layed, 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: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.
import bluej.extensions.*;
import javax.swing.*;
import java.awt.event.*;
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;
}
}
| Constructor Summary | |
MenuGenerator()
|
|
| Method Summary | |
javax.swing.JMenuItem |
getClassMenuItem(BClass bc)
Returns the JMenuItem to be added to the BlueJ Class menu Extensions should not retain references to the menu items created. |
javax.swing.JMenuItem |
getMenuItem()
Deprecated. As of BlueJ 1.3.5, replaced by getToolsMenuItem(BPackage bp) |
javax.swing.JMenuItem |
getObjectMenuItem(BObject bo)
Returns the JMenuItem to be added to the BlueJ Object menu Extensions should not retain references to the menu items created. |
javax.swing.JMenuItem |
getToolsMenuItem(BPackage bp)
Returns the JMenuItem to be adde d to the BlueJ Tools menu. |
void |
notifyPostClassMenu(BClass bc,
javax.swing.JMenuItem jmi)
Called by BlueJ when a class menu added by an extension is about to be displayed. |
void |
notifyPostObjectMenu(BObject bo,
javax.swing.JMenuItem jmi)
Called by BlueJ when an object menu added by an extension is about to be displayed. |
void |
notifyPostToolsMenu(BPackage bp,
javax.swing.JMenuItem jmi)
Called by BlueJ when a tools menu added by an extension is about to be displayed. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public MenuGenerator()
| Method Detail |
public javax.swing.JMenuItem getMenuItem()
getToolsMenuItem(BPackage bp)
public javax.swing.JMenuItem getToolsMenuItem(BPackage bp)
bp - the BlueJ package with which this menu item will be associated.public javax.swing.JMenuItem getClassMenuItem(BClass bc)
bc - the BlueJ class with which this menu item will be associated.public javax.swing.JMenuItem getObjectMenuItem(BObject bo)
bo - the BlueJ object with which this menu item will be associated.
public void notifyPostToolsMenu(BPackage bp,
javax.swing.JMenuItem jmi)
bp - the BlueJ package for which the menu is to be displayedjmi - the menu item which will be displayed (as provided by the
extension in a previous call to getToolsMenuItem)
public void notifyPostClassMenu(BClass bc,
javax.swing.JMenuItem jmi)
bc - the BlueJ class for which the menu is to be displayedjmi - the menu item which will be displayed (as provided by the
extension in a previous call to getToolsMenuItem)
public void notifyPostObjectMenu(BObject bo,
javax.swing.JMenuItem jmi)
bo - the BlueJ object for which the menu is to be displayedjmi - the menu item which will be displayed (as provided by the
extension in a previous call to getToolsMenuItem)