/* Simple Tcl Applet @Author: Christopher Hylands @Version: @(#)HelloTcl.java 1.5 02/13/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.io.*; import java.awt.*; import java.net.*; // Need URL ////////////////////////////////////////////////////////////////////////// //// HelloTcl.java /** Simple Applet that evaluates the contents of a file and displays * the results. * @author Christopher Hylands * @version @(#)HelloTcl.java 1.5 02/13/98 */ public class HelloTcl extends Applet implements Runnable{ /** Return a string describing this applet. */ public String getAppletInfo() { return "Hello World, a Jacl Applet\n" + "By: Christopher Hylands, cxh@eecs.berkeley.edu\n" + " Edward A. Lee, eal@eecs.berkeley.edu\n" + "@(#)HelloTcl.java 1.5 02/13/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("HelloTcl: init"); int width,height; _font = new java.awt.Font("TimesRoman", Font.PLAIN, 24); //show(); // Process the documentBase applet parameter. // Need the catch here because applets used as components have // no parameters. URL documentBase = null; try { documentBase = getDocumentBase(); } catch (NullPointerException e) { System.err.println("HelloTcl: 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("HelloTcl: 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) {} super.init(); _interp = new Interp(); if (scripturl != null) { if (_debug > 8) System.out.println("HelloTcl: init: documentBase:" + documentBase.toString() + "scripturl: " + scripturl); String scriptString = new String(); URL url = null; try { url = new URL(documentBase, scripturl); } catch (MalformedURLException e) { System.out.println("MalformedURLException during "+scripturl+": "+e); } if (1 == 0) { // This block is based on evalURL(), a non-public method // in from tcl.lang.Interp // // However, this code fails under Netscape4.0x because the // content Object is a netscape.net.URLInputStream // So, we don't run this. Object content = null; try { System.out.println("Content Type: " + url.openConnection().getContentType()); content = url.getContent(); } catch (IOException e2) { System.out.println("IOException during getContent"+ scripturl+": "+e2); } catch (SecurityException sec_e) { System.out.println("SecurityException during getContent"+ scripturl+": "+sec_e); } System.out.println(content.toString()); if (content instanceof String) { scriptString = (String)content; } else if (content instanceof InputStream) { InputStream fs = (InputStream)content; try { byte charArray[] = new byte[fs.available()]; fs.read(charArray); scriptString = new String(charArray); } catch (IOException e) { } finally { try { fs.close(); } catch (IOException e) {} } } } // FIXME: this is slow and stupid try { BufferedReader d = new BufferedReader(new InputStreamReader(url.openStream())); String line = d.readLine(); while (line != null) { scriptString = scriptString.concat(line + "\n"); line = d.readLine(); } } catch (IOException e ) { System.out.println("IOException while reading in " + scripturl+": "+e); } try { if (scriptString != null ) _interp.eval(scriptString); } catch (TclException e) { System.out.println("TclException during "+scripturl+": "+e); } } } /** Run this applet as an application. */ public static void main(String args[]) { HelloTcl hellotcl; hellotcl = new HelloTcl(); hellotcl.init(); hellotcl.start(); } /** Paint the screen with the return value from the Tcl interpreter. */ public void paint(Graphics g) { if (_debug > 8) System.out.println("HelloTcl: paint"); int x = 0, y = _font.getSize(); g.setFont(_font); g.drawString("Result was:\n",x ,y); y += _font.getSize(); TclObject result = _interp.getResult(); String resultString = result.toString(); int oldidx = 0; for ( int idx = 0; idx >= 0 && idx < resultString.length(); oldidx = idx) { idx = resultString.indexOf('\n',idx+1); if (idx >= 0 ) { g.drawString(resultString.substring(oldidx,idx) + "\n", x, y); y += _font.getSize(); } } super.paint(g); } public void run () { if (_debug > 8) System.out.println("HelloTcl: run"); repaint(); } /** Start the applet. */ public void start () { if (_debug > 8) System.out.println("HelloTcl: start"); _helloThread = new Thread(this); _helloThread.start(); super.start(); } /** Stop the applet. */ public void stop () { if (_debug > 8) System.out.println("HelloTcl: stop"); _helloThread.stop(); } /////////////////////////////////////////////////////////////////////// //// protected variables //// // If non-zero, print out debugging messages. protected int _debug = 0; /////////////////////////////////////////////////////////////////////// //// private variables //// private Font _font; private Interp _interp; // Thread for this applet. private Thread _helloThread; }