summaryrefslogtreecommitdiffstats
path: root/embedding/java/src/org/uscxml/tests/ioprocessor/adhoc/console/ConsoleFrame.java
blob: 0dbe5289cbbc4825adbfdc2725a029799776ecc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.uscxml.tests.ioprocessor.adhoc.console;

import java.awt.Frame;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.uscxml.Interpreter;
import org.uscxml.InterpreterException;

public class ConsoleFrame extends Frame {
	private static final long serialVersionUID = 3682378173372160680L;
	private ConsoleIOProc ioProc;

	public ConsoleFrame() throws InterpreterException {
		super("Input Frame");
		JPanel p = new JPanel();
		JLabel label = new JLabel("Key Listener!");
		p.add(label);
		add(p);
		setSize(200, 100);

		// instantiate SCXML interpreter
		final Interpreter interpreter = Interpreter.fromXML(
				"<scxml datamodel=\"ecmascript\">"
						+ "	<script src=\"http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js\" />"
						+ " <script>var charSeq = \"\";</script>"
						+ "	<state id=\"idle\">"
						+ "   <transition event=\"error\" target=\"quit\" />"
						+ "   <transition event=\"key.released\" cond=\"_event.data.keyChar == 10\">"
						+ "     <send type=\"console\">"
						+ "       <param name=\"foo\" expr=\"charSeq\" />"
						+ "     </send>"
						+ "     <script>"
						+ "       charSeq = \"\"; // reset string"
						+ "     </script>"
						+ "   </transition>"
						+ "   <transition event=\"*\">"
						+ "     <log label=\"event\" expr=\"dump(_event.data)\" />"
						+ "     <script>charSeq += String.fromCharCode(_event.data.keyChar);</script>"
						+ "   </transition>"
						+ "	</state>"
						+ " <final id=\"quit\" />"
						+ "</scxml>");

		/**
		 *  ConsoleIOProc will register as a keyboard listener and send events to the interpreter.
		 *  
		 *   This circumvents the factory and registers instances directly. You will have to promise
		 *   that the ioProc instance exists for the complete lifetime of the interpreter, otherwise
		 *   finalize() will call the C++ destructor and SegFaults will occur.
		 */
		ioProc = new ConsoleIOProc(this);
		interpreter.addIOProcessor(ioProc);

		// Start the interpreter in a separate thread
		Thread intrerpreterThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					interpreter.interpret();
				} catch (InterpreterException e) {
					e.printStackTrace();
				}				
			}
		});
		intrerpreterThread.start();		

		// show the user interface
		setVisible(true);
	}
	
	public static void main(String[] args) throws InterpreterException {
		System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib");
		ConsoleFrame frame = new ConsoleFrame();
	}
}