bluej.extensions
Class MenuGenerator

java.lang.Object
  extended by bluej.extensions.MenuGenerator

public class MenuGenerator
extends java.lang.Object

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:
  1. 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.
  2. 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.
  3. will be called at least once for every menu which is displayed.
The code for the example MenuBuilder class is:
 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);
         }
     }
 }
 

Version:
$Id: MenuGenerator.java 6215 2009-03-30 13:28:25Z polle $

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

MenuGenerator

public MenuGenerator()
Method Detail

getMenuItem

public javax.swing.JMenuItem getMenuItem()
Deprecated. As of BlueJ 1.3.5, replaced by getToolsMenuItem(BPackage bp)

Returns the JMenuItem to be added to the BlueJ Tools menu. Extensions should not retain references to the menu items created.


getToolsMenuItem

public javax.swing.JMenuItem getToolsMenuItem(BPackage bp)
Returns the JMenuItem to be added to the BlueJ Tools menu. Extensions should not retain references to the menu items created.

Parameters:
bp - the BlueJ package with which this menu item will be associated.

getClassMenuItem

public 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.

Parameters:
bc - the BlueJ class with which this menu item will be associated.

getObjectMenuItem

public 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.

Parameters:
bo - the BlueJ object with which this menu item will be associated.

notifyPostToolsMenu

public void notifyPostToolsMenu(BPackage bp,
                                javax.swing.JMenuItem jmi)
Called by BlueJ when a tools menu added by an extension is about to be displayed. An extension can use this notification to decide whether to enable/disable menu items and so on. Note: Due to a bug in Apple's current Java implementation, this method will not be called when is running on a Mac. It will start working as soon as there's a fix.

Parameters:
bp - the BlueJ package for which the menu is to be displayed
jmi - the menu item which will be displayed (as provided by the extension in a previous call to getToolsMenuItem)

notifyPostClassMenu

public void notifyPostClassMenu(BClass bc,
                                javax.swing.JMenuItem jmi)
Called by BlueJ when a class menu added by an extension is about to be displayed. An extension can use this notification to decide whether to enable/disable menu items and so on.

Parameters:
bc - the BlueJ class for which the menu is to be displayed
jmi - the menu item which will be displayed (as provided by the extension in a previous call to getToolsMenuItem)

notifyPostObjectMenu

public void notifyPostObjectMenu(BObject bo,
                                 javax.swing.JMenuItem jmi)
Called by BlueJ when an object menu added by an extension is about to be displayed. An extension can use this notification to decide whether to enable/disable menu items and so on.

Parameters:
bo - the BlueJ object for which the menu is to be displayed
jmi - the menu item which will be displayed (as provided by the extension in a previous call to getToolsMenuItem)


BlueJ homepage