001: /*
002: * $Id: JGraphpadBshPlugin.java,v 1.1.1.1 2005/08/04 11:21:58 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.bshplugin;
011:
012: import java.awt.Component;
013:
014: import javax.swing.JTabbedPane;
015:
016: import org.w3c.dom.Node;
017:
018: import bsh.EvalError;
019: import bsh.Interpreter;
020: import bsh.util.JConsole;
021:
022: import com.jgraph.JGraphEditor;
023: import com.jgraph.editor.JGraphEditorPlugin;
024: import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
025: import com.jgraph.pad.factory.JGraphpadPane;
026: import com.jgraph.pad.util.JGraphpadFocusManager;
027:
028: /**
029: * Plugin for using Bsh in JGraphpad Pro. Adds a shell console to the bottom
030: * tabs in the main window.
031: */
032: public class JGraphpadBshPlugin implements JGraphEditorPlugin {
033:
034: /**
035: * Initializes the plugin by registering all factory methods.
036: *
037: * @param editor
038: * The enclosing editor for the plugin.
039: * @param configuration
040: * The object to configure the plugin with.
041: */
042: public void initialize(JGraphEditor editor, Node configuration) {
043: editor.getFactory().addMethod(new FactoryMethod(editor));
044: editor.getFactory().addMethod(
045: new ExtendedBottomTabFactoryMethod(editor));
046: }
047:
048: /**
049: * Factory method to construct a bsh console.
050: */
051: public static class FactoryMethod extends JGraphEditorFactoryMethod {
052:
053: /**
054: * Defines the default name for factory methods of this kind.
055: */
056: public static String NAME = "createBshConsole";
057:
058: /**
059: * References the enclosing editor.
060: */
061: protected JGraphEditor editor;
062:
063: /**
064: * Constructs a new factory method for the specified enclosing editor
065: * using {@link #NAME}.
066: *
067: * @param editor
068: * The editor that contains the factory method.
069: */
070: public FactoryMethod(JGraphEditor editor) {
071: super (NAME);
072: this .editor = editor;
073: }
074:
075: /*
076: * (non-Javadoc)
077: */
078: public Component createInstance(Node configuration) {
079: JConsole console = new JConsole();
080: Interpreter interpreter = new Interpreter(console);
081: try {
082:
083: // Assings the editor and mgr variable in the shell so the
084: // editor and last focused graph are accessible.
085: interpreter.set("editor", editor);
086: interpreter.set("mgr", JGraphpadFocusManager
087: .getCurrentGraphFocusManager());
088: } catch (EvalError e) {
089: // ignore
090: }
091: new Thread(interpreter).start();
092: interpreter
093: .print("Use the editor and mgr variables, eg. \"print(editor);\""
094: + " to access the enclosing application\n");
095: return console;
096: }
097: }
098:
099: /**
100: * Utility class to add the property sheet to the bottom tab of the main
101: * application window. This factory method replaces the original factory
102: * method and keeps a reference to it. When it is invoked it uses the
103: * original factory method to create the original object and then adds its
104: * own tabs to that object before returning it.
105: */
106: public static class ExtendedBottomTabFactoryMethod extends
107: JGraphEditorFactoryMethod {
108:
109: /**
110: * Reference to factory method that was replaced.
111: */
112: protected JGraphEditorFactoryMethod previous = null;
113:
114: /**
115: * References the enclosing editor.
116: */
117: protected JGraphEditor editor;
118:
119: /**
120: * Constructs a new factory method for the specified enclosing editor
121: * using JGraphpadPane.BottomTabFactoryMethod.NAME.
122: *
123: * @param editor
124: * The editor that contains the factory method.
125: */
126: public ExtendedBottomTabFactoryMethod(JGraphEditor editor) {
127: super (JGraphpadPane.BottomTabFactoryMethod.NAME);
128: previous = editor.getFactory().getMethod(
129: JGraphpadPane.BottomTabFactoryMethod.NAME);
130: this .editor = editor;
131: }
132:
133: /*
134: * (non-Javadoc)
135: */
136: public Component createInstance(Node configuration) {
137: if (previous != null) {
138: Component component = previous
139: .createInstance(configuration);
140: if (component instanceof JTabbedPane) {
141: JTabbedPane tabPane = (JTabbedPane) component;
142:
143: Component bshPanel = editor.getFactory()
144: .executeMethod(FactoryMethod.NAME);
145: if (bshPanel != null)
146: tabPane.addTab("Shell", bshPanel);
147:
148: }
149: return component;
150: }
151: return null;
152: }
153:
154: }
155:
156: }
|