001: /* Copyright 2005-2008 Wojciech Kocjan, David N. Welton
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl.applet;
017:
018: import java.applet.Applet;
019: import java.awt.Color;
020: import java.awt.Font;
021: import java.awt.GridBagConstraints;
022: import java.awt.GridBagLayout;
023: import java.awt.TextArea;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.util.Date;
027:
028: import org.hecl.Command;
029: import org.hecl.HeclException;
030: import org.hecl.Interp;
031: import org.hecl.StringThing;
032: import org.hecl.Thing;
033:
034: import org.hecl.java.HeclJavaCmd;
035:
036: /**
037: * <code>HeclApplet</code> implements an applet that lets you try out
038: * hecl.
039: *
040: * @author zoro
041: * @version 1.0
042: */
043: public class HeclApplet extends Applet implements ActionListener {
044: TextArea output = new TextArea();
045: GridBagLayout gb;
046: GridBagConstraints gbc = new GridBagConstraints();
047: Interp interp;
048: Runner runner;
049:
050: String script = null;
051:
052: /**
053: * The <code>runScript</code> method is called from a bit of
054: * Javascript. It sets the script and runs it. We call it via
055: * javascript so as to minimize the actual applet.
056: *
057: * @param s a <code>String</code> value
058: */
059: public void runScript(String s) {
060: script = s;
061: runHecl();
062: }
063:
064: public HeclApplet() {
065: gb = new GridBagLayout();
066: this .setLayout(gb);
067:
068: gbc.fill = GridBagConstraints.BOTH;
069: gbc.gridx = 0;
070: gbc.gridy = 1;
071: gbc.gridheight = 1;
072: gbc.gridwidth = 1;
073: gbc.weightx = 1;
074: gbc.weighty = 1;
075: this .add(output);
076: gb.setConstraints(output, gbc);
077:
078: gbc.gridx = 0;
079: gbc.gridy = 2;
080: gbc.gridheight = 1;
081: gbc.gridwidth = 1;
082: gbc.weightx = 1;
083: gbc.weighty = 0;
084:
085: Font fixed = new Font("Monospaced", Font.PLAIN, 10);
086: output.setFont(fixed);
087: }
088:
089: public void start() {
090: }
091:
092: public void stop() {
093: runner = null;
094: interp = null;
095: }
096:
097: String resultString = "";
098:
099: public void runHecl() {
100: output.setText("");
101: try {
102: interp = new Interp();
103: HeclJavaCmd.load(interp);
104: interp.addCommand("puts", new PutsCommand());
105: } catch (HeclException error) {
106: error.printStackTrace();
107: output.setForeground(Color.red);
108: output.setText("Error while initializing Hecl:\n"
109: + error.getMessage());
110: return;
111: }
112:
113: runner = null;
114: runner = new Runner();
115: runner.setCode(new Thing(new StringThing(script)));
116: runner.start();
117: return;
118: }
119:
120: public void actionPerformed(ActionEvent action) {
121: }
122:
123: class PutsCommand implements Command {
124: public Thing cmdCode(Interp interp, Thing[] argv)
125: throws HeclException {
126: if (argv.length != 2) {
127: throw HeclException.createWrongNumArgsException(argv,
128: 1, "string");
129: }
130: synchronized (resultString) {
131: resultString += argv[1].toString() + "\n";
132: output.setText(resultString);
133: }
134: return null;
135: }
136: }
137:
138: /**
139: * The <code>Runner</code> class provides a way to perform
140: * long-running scripts without blocking the GUI.
141: *
142: */
143: class Runner extends Thread {
144: Thing code;
145:
146: public Runner() {
147: }
148:
149: public void setCode(Thing c) {
150: code = c;
151: }
152:
153: public void run() {
154: long t0, t1;
155: String str = "";
156: try {
157: synchronized (resultString) {
158: resultString = "";
159: }
160: t0 = new Date().getTime();
161: interp.eval(code);
162: t1 = new Date().getTime();
163: } catch (HeclException error) {
164: error.printStackTrace();
165: output.setForeground(Color.red);
166: output.setText("Error while running script:\n"
167: + error.getMessage());
168: return;
169: }
170: synchronized (resultString) {
171: str = "Execution result: (time: " + (t1 - t0)
172: + " ms)\n" + resultString;
173: }
174: output.setForeground(Color.black);
175: output.setText(str);
176: return;
177: }
178: }
179: }
|