Package fbench.plugin

Source Code of fbench.plugin.PluginManager

// Copyright (c) 2007 University of Auckland
// Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License

package fbench.plugin;

import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.event.MenuListener;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

import fbench.Constants;
import fbench.FBench;
import fbench.IconFactory;
import fbench.ParserLib;

/**
* PluginManager for FBench. General Purpose of this class is to provide easier and organised access to
* various of FBench Plugin components.
*
* @author Jason Park, Cheng Pang
*
* @version 20070530/JP
*/
public class PluginManager implements Constants {

  private static final Hashtable<String, String> schemas = new Hashtable<String, String>();

  static {
    putSchemaVersion(pluginSchemaVersion, pluginSchemaPath);
  }

  // private FBench fBench;
  private Hashtable<String, Object> listenerTable = new Hashtable<String, Object>();
  private Hashtable<String, Object> pluginObjects = new Hashtable<String, Object>();

  private File pluginRoot = new File("Plugins" + File.separator);

  public PluginManager(FBench fBench) {
    listenerTable.put(fBench.getClass().toString().replace("class ", ""), fBench);
    // this.fBench = fBench;
    initPlugins();
  }

  public static void putSchemaVersion(String version, String uri) {
    schemas.put(version, uri);
  }

  private void initPlugins() {
    File[] rootFileList = pluginRoot.listFiles();
    System.out.println("Initiating plugins");
    for(File dir : rootFileList) {
      if(dir.isDirectory()) {
        File[] pluginFileList = dir.listFiles();
        for(File pluginFile : pluginFileList) {
          if(pluginFile.getName().endsWith("PlugIn.class")) {
            try {
              ClassPathUtility.addFile(dir);

              String pluginName = pluginFile.getName().replace(".class", "");
              String className = pluginName;
              System.out.println(pluginName + " resides in class " + className);
              Class<?> geclass = Class.forName(className);
              Object obj = geclass.newInstance();
              pluginObjects.put(pluginName, obj);
            }
            catch(Exception e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
  }

  public Vector<JMenu> loadMenuItems() {
    Vector<JMenu> menuItems = new Vector<JMenu>();
    Vector<Element> pluginList = loadXML();

    for(Element plugin : pluginList) {
      // System.out.println(" Plugin: " + plugin.getAttributeValue("Name"));
      Element menuHierarchy = (Element) plugin.getChild("MenuHierarchy");
      List<?> menuNodes = menuHierarchy.getChildren("Menu");
      for(int i = 0; i < menuNodes.size(); i++) {
        Element menuElement = (Element) menuNodes.get(i);
        // System.out.println(" item: " + menuElement.getAttributeValue("Label"));
        // Element rootElement = menu.getChild("Menu");
        JMenu menu = createMenuTree(menuElement);
        menuItems.add(menu);
      }
    }
    return menuItems;
  }

  private JMenu createMenuTree(Element menuElement) {
    String label = menuElement.getAttributeValue("Label");
    String icon = menuElement.getAttributeValue("Icon");
    String actionCommand = menuElement.getAttributeValue("ActionCommand");
    String menuListener = menuElement.getAttributeValue("MenuListener");
    String toolTip = menuElement.getAttributeValue("Tooltip");

    // System.out.println("JMenu name: " + label + " actionCoomand: " + actionCommand + " menuListener: "
    //        + menuListener);

    // TODO: Redundant? never get in
    if(menuListener != null && !listenerTable.containsKey(menuListener)) {
      try {
        listenerTable.put(menuListener, Class.forName(menuListener).newInstance());
      }
      catch(InstantiationException e) {
        e.printStackTrace();
      }
      catch(IllegalAccessException e) {
        e.printStackTrace();
      }
      catch(ClassNotFoundException e) {
        e.printStackTrace();
      }
    }

    JMenu menu = new JMenu(label);

    if(icon != null && !icon.isEmpty()) {
      menu.setIcon(IconFactory.getIcon(icon));
    }

    if(actionCommand != null && !actionCommand.isEmpty()) {
      menu.setActionCommand(actionCommand);
    }

    if(menuListener != null && !menuListener.isEmpty()) {
      menu.addMenuListener((MenuListener) listenerTable.get(menuListener));
    }

    if(toolTip != null && !toolTip.isEmpty()) {
      menu.setToolTipText(toolTip);
    }

    JMenuItem submenuItem = null;

    for(Object o : menuElement.getChildren()) {
      Element submenuElement = (Element) o;
      if(submenuElement.getName().equalsIgnoreCase("Menu")) {
        submenuItem = createMenuTree(submenuElement);
      }
      else {
        submenuItem = createMenuItem(submenuElement);
      }

      menu.add(submenuItem);
    }
    return menu;
  }

  private JMenuItem createMenuItem(Element menuElement) {
    String label = menuElement.getAttributeValue("Label");
    String icon = menuElement.getAttributeValue("Icon");
    String actionCommand = menuElement.getAttributeValue("ActionCommand");
    String actionListener = menuElement.getAttributeValue("ActionListener");
    String shortCut = menuElement.getAttributeValue("Shortcut");
    String toolTip = menuElement.getAttributeValue("Tooltip");
    String selection = menuElement.getAttributeValue("Selection");

    if(!listenerTable.containsKey(actionListener)) {
      try {
        listenerTable.put(actionListener, Class.forName(actionListener).newInstance());
      }
      catch(InstantiationException e) {
        e.printStackTrace();
      }
      catch(IllegalAccessException e) {
        e.printStackTrace();
      }
      catch(ClassNotFoundException e) {
        e.printStackTrace();
      }
    }

    // The PluginSchema only allows MenuItem, CheckBox, RadioButton, and Menu
    JMenuItem menuItem = null;
    if(menuElement.getName().equalsIgnoreCase("MenuItem")) {
      menuItem = new JMenuItem(label);
    }
    else if(menuElement.getName().equalsIgnoreCase("CheckBox")) {
      menuItem = new JCheckBoxMenuItem(label);
    }
    else if(menuElement.getName().equalsIgnoreCase("RadioButton")) {
      menuItem = new JRadioButtonMenuItem(label);
    }

    if(icon != null && !icon.isEmpty()) {
      menuItem.setIcon(IconFactory.getIcon(icon));
    }

    if(actionCommand != null && !actionCommand.isEmpty()) {
      menuItem.setActionCommand(actionCommand);
    }

    if(actionListener != null && !actionListener.isEmpty()) {
      menuItem.addActionListener((ActionListener) listenerTable.get(actionListener));
      // System.out.println("Added actionListener [" + actionListener + "] to [" + label + "]");
    }

    if(shortCut != null && !shortCut.isEmpty()) {
      char shortCutKey = shortCut.charAt(0);
      menuItem.setAccelerator(KeyStroke.getKeyStroke(shortCutKey, InputEvent.ALT_MASK));
    }

    if(toolTip != null && !toolTip.isEmpty()) {
      menuItem.setToolTipText(toolTip);
    }

    if(selection != null && !selection.isEmpty()) {
      boolean isSelected = Boolean.valueOf(selection);
      menuItem.setSelected(isSelected);
    }

    return menuItem;
  }

  public Vector<Element> loadXML() {
    Vector<Element> pluginList = new Vector<Element>();

    File[] rootFileList = pluginRoot.listFiles();

    for(File file : rootFileList) {
      if(file.isDirectory()) {
        File[] pluginFileList = file.listFiles();
        for(File pluginFile : pluginFileList) {
          if(pluginFile.getName().equals(defaultPluginXMLFile)) {
            try {
              StringBuilder sb = new StringBuilder();
              String attribute = "xsi:noNamespaceSchemaLocation=\"";
              FileInputStream fs = new FileInputStream(pluginFile);
              BufferedReader br = new BufferedReader(new InputStreamReader(fs));
              String line = null;
              while((line = br.readLine()) != null) {
                if(line.contains(attribute)) {

                  String schemaVersion = getSchemaVersion(line);
                  String schemaUri = null;
                  if(schemas.containsKey(schemaVersion))
                    schemaUri = schemas.get(schemaVersion);
                  else
                    schemaUri = schemas.get(pluginSchemaVersion);

                  int start = line.indexOf(attribute) + attribute.length();
                  String temp1 = line.substring(0, start);
                  String temp2 = line.substring(start);
                  temp2 = temp2.substring(temp2.indexOf("\""));
                  line = temp1 + schemaUri + temp2;
                }

                sb.append(line + "\n");
              }
              br.close();
              fs.close();

              Document xmlDocument = ParserLib.parser(new StringReader(sb.toString()), true);
              pluginList.add(xmlDocument.getRootElement());

              File directory = pluginFile.getParentFile();
              ClassPathUtility.addFile(directory);

            }
            catch(IOException e) {
              e.printStackTrace();
            }
            catch(JDOMException e) {
              System.err.println("Plugin validation failed: " + pluginFile.getPath());
              System.err.println(e.getMessage());
            }
          }
        }
      }
    }
    return pluginList;
  }

  private static String getSchemaVersion(String line) {
    int start = line.indexOf("SchemaVersion=\"") + "SchemaVersion=\"".length();
    int end = line.indexOf("\" Copyright");
    return new String(line.substring(start, end));
  }

  public Hashtable<String, Object> getPluginObjects() {
    return null;
  }
}
TOP

Related Classes of fbench.plugin.PluginManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.