JMSL Tutorial: Plug-ins
Create hierarchical menus from scanned classes
When ClassBrowser has finished browsing the classpath, it has notified you ClassFinder of all class names it encountered.
The ClassFinder we set up was only interested in "Instrument" classes. The ClassFinder then contains a Vector of classnames which
implement Instrument.
You can pick these names from the Vector and use them as you like (build a gui for them or instantiate them according to some process).
Here we show you a nice technique which creates a hierarchical menu from fully qualified classnames,
where package names are expressed as nested submenus, and the last MenuItem in the nest is the class name. For example, the class "com.acme.Widgit" would show up
as a Menu named "com" containing a Menu named "acme" containing a MenuItem named "Widgit".
The following code excerpt shows how to use MenuBuilder. Add fully qualified classnames to it, and then call getMenu()
HierarchicalMenuBuilder menuBuilder = new HierarchicalMenuBuilder("Instruments (plug-ins)");
menuBuilder.addHierarchicalMenuItem("com.some.package.SomeClass");
pluginsMenu = menuBuilder.getMenu();
menuBar.add(pluginsMenu);
menuBuilder.addHierarchicalMenuListener(this);
Notice the call to menuBuilder.addHierarchicalMenuListener(this).
A HierarchicalMenuListener receives notification when a menu item is selected. You can then do what you wish with the fully qualified classname passed in.
public void hierarchicalMenuItemSelected(PVMenu topMenu, String hierarchicalName) {
System.out.println("Menu selected: " + topMenu.getLabel());
System.out.println("Plug-in class selected: " + hierarchicalName);
}
The applet below opens a Frame and builds a hierarchical menu that appears in the Frame. Fictitious classnames are used because applet's cannot scan your classpath.
Source
(C) 2000 Nick Didkovsky and Phil Burk, All Rights Reserved
JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.