|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object bluej.extensions.MenuGenerator
public class 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 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 MenuGenerator 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 BPackage curPackage; private BClass curClass; private BObject curObject; public JMenuItem getToolsMenuItem(BPackage aPackage) { return new JMenuItem(new SimpleAction("Click Tools", "Tools menu:")); } public JMenuItem getClassMenuItem(BClass aClass) { return new JMenuItem(new SimpleAction("Click Class", "Class menu:")); } public JMenuItem getObjectMenuItem(BObject anObject) { return new JMenuItem(new SimpleAction("Click Object", "Object menu:")); } // These methods will be called 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; } // A utility method which pops up a dialog detailing the objects // involved in the current (SimpleAction) menu invocation. private void showCurrentStatus(String header) { try { if (curObject != null) curClass = curObject.getBClass(); if (curClass != null) curPackage = curClass.getPackage(); String msg = header; if (curPackage != null) msg += "\nCurrent Package = " + curPackage; if (curClass != null) msg += "\nCurrent Class = " + curClass; if (curObject != null) msg += "\nCurrent Object = " + curObject; JOptionPane.showMessageDialog(null, msg); } catch (Exception exc) { } } // The nested class that instantiates the different (simple) menus. class SimpleAction extends AbstractAction { private String msgHeader; public SimpleAction(String menuName, String msg) { putValue(AbstractAction.NAME, menuName); msgHeader = msg; } public void actionPerformed(ActionEvent anEvent) { showCurrentStatus(msgHeader); } } }
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 added 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)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |