/* * Created by Nick on Dec 19, 2004 * */ package jmslexamples; import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.util.Enumeration; import java.util.Vector; import com.didkovsky.portview.*; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.util.classbrowser.ClassBrowser; import com.softsynth.jmsl.util.classbrowser.ClassFinder; import com.softsynth.jmsl.view.*; /** * Scans a folder named "jmsl_plugins" which must be in the CLASSPATH.
* Searches for classes implementing the Instrument interface.
* Prints a list of found classes to the console.
* Builds a HierarchicalMenu from the found classes.
* Select an item from the HierarchicalMenu and get notified of the fully qualified classname.
* This can be used to instantiate an instance of the selected item.
*
* The design of this example is at the heart of the JMSL plugins model.
* * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class PluginScannerExample extends PVFrameAdapter implements HierarchicalMenuListener { Vector foundInstrumentList; PVMenuBar menuBar; PVMenu pluginsMenu; PVLabel selectedPluginLabel; public PluginScannerExample() { super("Plug-in scanner example (c) 2004 Nick Didkovsky"); menuBar = JMSL.getViewFactory().createMenuBar(); setFrameLayout(new BorderLayout()); add((selectedPluginLabel = new PVLabelAdapter("Select a plugin from the menu")).getComponent()); setPVMenuBar(menuBar); } public void buildHierarchicalMenuFromClasses() { HierarchicalMenuBuilder menuBuilder = new HierarchicalMenuBuilder("Instruments (plug-ins)"); for (Enumeration e = foundInstrumentList.elements(); e.hasMoreElements();) { String mitem = (String) e.nextElement(); menuBuilder.addHierarchicalMenuItem(mitem); } pluginsMenu = menuBuilder.getMenu(); menuBar.add(pluginsMenu); menuBuilder.addHierarchicalMenuListener(this); } public String scanForClasses(Vector searchPaths) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException { ClassBrowser browser = new ClassBrowser(); if (searchPaths != null) { for (Enumeration e = searchPaths.elements(); e.hasMoreElements();) { browser.addSearchPath((String) e.nextElement()); } } ClassFinder findInstruments = new ClassFinder(com.softsynth.jmsl.Instrument.class.getName()); browser.addClassListener(findInstruments); browser.browse(); foundInstrumentList = findInstruments.getFoundClasses(); return browser.getUnmatchedFiles(); } public void hierarchicalMenuItemSelected(PVMenu topMenu, String hierarchicalName) { System.out.println("Menu selected: " + topMenu.getLabel()); System.out.println("Plug-in class selected: " + hierarchicalName); selectedPluginLabel.setText(hierarchicalName); } /** * @return Returns the foundInstrumentList. */ public Vector getFoundInstrumentList() { return foundInstrumentList; } public static void main(String[] args) { PluginScannerExample pluginScanner = new PluginScannerExample(); Vector searchPaths = new Vector(); searchPaths.addElement("jmsl_plugins"); try { String unmatchedClasses = pluginScanner.scanForClasses(searchPaths); pluginScanner.buildHierarchicalMenuFromClasses(); System.out.println("Unmatched classpath folders = " + unmatchedClasses); Vector foundClasses = pluginScanner.getFoundInstrumentList(); System.out.println(foundClasses.size() + " classes found"); for (Enumeration e = foundClasses.elements(); e.hasMoreElements();) { String classname = (String) e.nextElement(); System.out.println("Found: " + classname); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } pluginScanner.setSize(400, 200); pluginScanner.setVisible(true); pluginScanner.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }