001: /* *************************************************************************
002: *
003: * <rrl>
004: * =========================================================================
005: * LEGEND
006: *
007: * Use, duplication, or disclosure by the Government is as set forth in the
008: * Rights in technical data noncommercial items clause DFAR 252.227-7013 and
009: * Rights in noncommercial computer software and noncommercial computer
010: * software documentation clause DFAR 252.227-7014, with the exception of
011: * third party software known as Sun Microsystems' Java Runtime Environment
012: * (JRE), Quest Software's JClass, Oracle's JDBC, and JGoodies which are
013: * separately governed under their commercial licenses. Refer to the
014: * license directory for information regarding the open source packages used
015: * by this software.
016: *
017: * Copyright 2006 by BBNT Solutions, LLC.
018: * =========================================================================
019: * </rrl>
020: *
021: * $Id: BeanShellFrame.java,v 1.1 2007/06/20 19:13:13 jzinky Exp $
022: *
023: * ************************************************************************/
024:
025: package org.cougaar.tutorials.bsh;
026:
027: import java.awt.BorderLayout;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030:
031: import javax.swing.JButton;
032: import javax.swing.JFrame;
033: import javax.swing.JPanel;
034:
035: import bsh.EvalError;
036: import bsh.Interpreter;
037: import bsh.util.JConsole;
038:
039: /**
040: * A frame containing a Bean Shell console. Unfortunately, I can't
041: * figure out how to gracefully stop the interpreter. That means it
042: * might be best to hold on to the frame, redisplaying it as needed.
043: *
044: * @version $Revision: 1.1 $ on $Date: 2007/06/20 19:13:13 $
045: */
046: public class BeanShellFrame extends JFrame {
047:
048: /** The Bean Shell console window. */
049: private JConsole console;
050:
051: /** The Bean Shell interpreter. */
052: private Interpreter interpreter;
053:
054: /**
055: * Constructs a new Bean Shell Frame. The frame consists of a
056: * bean shell console and a close button. The default size is 500
057: * x 300.
058: */
059: public BeanShellFrame() {
060: this ("Bean Shell");
061: }
062:
063: /**
064: * Constructs a new Bean Shell Frame. The frame consists of a
065: * bean shell console and a close button. The default size is 500
066: * x 300.
067: */
068: public BeanShellFrame(String title) {
069: super (title);
070: console = new JConsole();
071: interpreter = new Interpreter(console);
072: init(interpreter);
073:
074: getContentPane().setLayout(new BorderLayout());
075: getContentPane().add(console, BorderLayout.CENTER);
076: JPanel buttonPanel = makeButtonPanel();
077: if (buttonPanel != null) {
078: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
079: }
080: setSize(700, 600);
081: }
082:
083: /**
084: * Performs any desired initializations on the Bean Shell
085: * Interpreter. This method can be overridden in extending
086: * classes to perform additional initializations on the
087: * interpreter.
088: *
089: * @param i the bean shell interpreter
090: */
091: protected void init(Interpreter i) {
092: try {
093: i.set("bsh.system.shutdownOnExit", false);
094: i.eval("show();");
095: } catch (EvalError e) {
096: // Do nothing, our inits didn't work out.
097: System.out.println("BeanShellFrame.init failure: ");
098: e.printStackTrace();
099: }
100: }
101:
102: /**
103: * Creates the button panel on the frame. The default is a close
104: * button. Extending classes can create a more compilcated button
105: * panel if desired by overriding this method.
106: */
107: protected JPanel makeButtonPanel() {
108: JButton closeButton = new JButton("Close");
109: closeButton.addActionListener(new ActionListener() {
110: public void actionPerformed(ActionEvent e) {
111: setVisible(false);
112: }
113: });
114:
115: JPanel buttonPanel = new JPanel();
116: buttonPanel.add(closeButton);
117: return buttonPanel;
118: }
119:
120: /**
121: * Import a class or package into the interpreter
122: *
123: * @param path the fully qualified class or package name
124: **/
125: public void interpreterImport(String path) {
126: try {
127: interpreter.eval("import " + path);
128: } catch (EvalError e) {
129: throw new RuntimeException(e);
130: }
131: }
132:
133: /**
134: * Sets the bean shell variable <code>var</code> to
135: * <code>obj</code> in the bean shell interpreter of this frame.
136: *
137: * @param var the bean shell variable to set
138: * @param obj the value of the beans shell variable
139: * <code>var</code>
140: */
141: public void set(String var, Object obj) {
142: try {
143: interpreter.set(var, obj);
144: } catch (EvalError e) {
145: throw new RuntimeException(e);
146: }
147: }
148:
149: /**
150: * Runs this interpreter in the current thread. I don't think
151: * this method will ever return because of limitations in the bean
152: * shell interpreter API.
153: */
154: public void run() {
155: interpreter.run();
156: }
157:
158: /**
159: * Runs this interpreter in its own thread.
160: */
161: public void runInThread() {
162: new Thread(interpreter).start();
163: }
164:
165: public static void main(String[] argv) {
166: BeanShellFrame bsf = new BeanShellFrame();
167: bsf.setTitle("Bean Shell");
168: bsf.pack();
169: bsf.setVisible(true);
170: bsf.run();
171: }
172: }
|