/* Jacl Tcl Shell @Author: Christopher Hylands @Version: @(#)TclShell.java 1.5 02/19/98 @Copyright (c) 1998 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ import tcl.lang.*; import java.applet.Applet; import java.awt.*; import java.net.*; // Need URL ////////////////////////////////////////////////////////////////////////// //// TclShell /** TclShell front end to Jacl. * @author Christopher Hylands * @version @(#)TclShell.java 1.5 02/19/98 */ public class TclShell extends Applet implements Runnable { /** Display the results. * (Tycho method) */ public void displayResult (String result) { _textArea.append(result); _textArea.setCaretPosition(_textArea.getText().length()); _userTextStart = _textArea.getCaretPosition(); } /** Return a string describing this applet. */ public String getAppletInfo() { return "Hello World, a Jacl Applet\n" + "By: Christopher Hylands, cxh@eecs.berkeley.edu\n" + "@(#)TclShell.java 1.5 02/19/98"; } /** Return information about parameters. */ public String[][] getParameterInfo () { String pinfo[][] = { {"background", "hexcolor value", "background color"}, {"foreground", "hexcolor value", "foreground color"}, {"scripturl", "url", "the URL of the tcl script"} }; return pinfo; } /** * Initialize the applet. Read the applet parameters. */ public void init() { if (_debug > 8) System.out.println("TclShell: init"); int width,height; setLayout(new BorderLayout()); _textArea = new TextArea("", 10, 40, TextArea.SCROLLBARS_BOTH); add("Center", _textArea); _newline = System.getProperty("line.separator"); //show(); // The Java Programmnig FAQ at http://www.best.com/~pvdl/javafaq.html // says: // 6.20 How do I redirect the System.err stream to a file? // A. You cannot assign a new FileOutputStream to System.err, as it is // final. Instead use the System.setErr() library call, like this: // System.setErr(new FileOutputStream("myerrors.txt")); // This was introduced with JDK 1.1. There is also a corresponding // setIn() for redirecting standard in, and a setOut() for // standard out. // // We should use this to redirect the output. // Process the documentBase applet parameter. // Need the catch here because applets used as components have // no parameters. URL docbase = null; try { docbase = getDocumentBase(); } catch (NullPointerException e) { System.err.println("TclShell: init: NullPointerException while"+ "handling getDocumentBase" + e); } // Process the width and height applet parameters try { width = Integer.valueOf(getParameter("width")).intValue(); } catch (NullPointerException e) { width = 400; } try { height = Integer.valueOf(getParameter("height")).intValue(); } catch (NullPointerException e) { height = 400; } if (_debug > 8) System.out.println("TclShell: init: about to resize"+width); resize(width,height); // Process the background parameter. try { Color background = Color.white; //background = PlotBox.getColorByName(getParameter("background")); //setBackground(background); } catch (NullPointerException e) {} // Process the foreground parameter. try { Color foreground = Color.white; //foreground = PlotBox.getColorByName(getParameter("foreground")); //setForeground(foreground); } catch (NullPointerException e) {} // Process the scripturl parameter. String scripturl = null; try { scripturl = getParameter("scripturl"); } catch (NullPointerException e) {} // Eval the scripturl if (scripturl != null) { Cursor oldCursor = _textArea.getCursor(); _textArea.setCursor(new Cursor(Cursor.WAIT_CURSOR)); try { _interp.evalFile(scripturl); } catch (TclException e) { _errorDisplay(e, "evalFile " + scripturl); } finally { _textArea.setCursor(oldCursor); } } printPrompt(""); super.init(); _interp = new Interp(); } /** Handle return keys in the TextArea. * @deprecated As of JDK1.1 in java.awt.component * but we need to compile under 1.0.2 for netscape4.04 compatibility. */ public boolean keyDown (Event evt, int key) { if (evt.target == _textArea && key == 10) { // The user hit a return. _evalTextEntry(); return true; } else { return super.keyDown(evt, key); } } /** Run this applet as an application. */ public static void main(String args[]) { TclShell TclShell; TclShell = new TclShell(); TclShell.init(); TclShell.start(); } /** Paint the screen with our message */ public void paint(Graphics g) { if (_debug > 8) System.out.println("TclShell: paint"); super.paint(g); } /** Print the prompt. * (Tycho method) * FIXME: if flag is INCOMPLETE, then a prompt for incomplete commands * should be generated. */ public void printPrompt(String flag) { _textArea.append(_prompt); // FIXME: There has to be a better way to get the end of the TextArea. _textArea.setCaretPosition(_textArea.getText().length()); _userTextStart = _textArea.getCaretPosition(); } public void run () { if (_debug > 8) System.out.println("TclShell: run"); repaint(); } /** Start the applet. */ public void start () { if (_debug > 8) System.out.println("TclShell: start"); _tclShellThread = new Thread(this); _tclShellThread.start(); super.start(); } /** Stop the applet. */ public void stop () { if (_debug > 8) System.out.println("TclShell: stop"); _tclShellThread.stop(); } ////////////////////////////////////////////////////////////////////// //// protected method //// /** Display the errorInfo Tcl variable. */ protected void _errorDisplay (Exception e, String userCommand) { displayResult("\nTclException during eval of:\n" + userCommand + "\nError was:\n" + e.getMessage() + "\n"); try { TclObject result = _interp.getVar("errorInfo",TCL.GLOBAL_ONLY); displayResult(result.toString() + "\n"); } catch (TclException e2) {} } /** Evaluate the string in the text area. * (Tycho method) */ protected void _evalTextEntry() { String userCommand = ""; Cursor oldCursor = _textArea.getCursor(); _textArea.setCursor(new Cursor(Cursor.WAIT_CURSOR)); try { userCommand = _textArea.getText().substring(_userTextStart); if (_debug > 10) System.out.println("TclShell evalTextEntry: " + userCommand); _interp.eval(userCommand); TclObject result = _interp.getResult(); displayResult(_newline + result.toString() + _newline); } catch (TclException e) { _errorDisplay(e, userCommand); } finally { _textArea.setCursor(oldCursor); } printPrompt(""); } // If non-zero, print out debugging messages. protected int _debug = 0; ////////////////////////////////////////////////////////////////////// //// private variables //// // Tcl Interpreter. private Interp _interp; // Platform dependent newline private String _newline; // TextArea where commands are displayed an such. private TextArea _textArea; // Prompt private String _prompt = "jacl % "; // Index in _textArea of the start of the User Text; private int _userTextStart; // Thread for this applet. private Thread _tclShellThread; }