diff options
Diffstat (limited to 'embedding')
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java | 74 | ||||
-rw-r--r-- | embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java | 93 |
2 files changed, 167 insertions, 0 deletions
diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java b/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java new file mode 100644 index 0000000..b44c05e --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleFrame.java @@ -0,0 +1,74 @@ +package org.uscxml.tests.ioprocessor.console; + +import java.awt.Frame; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.JLabel; +import javax.swing.JPanel; + +import org.uscxml.Factory; +import org.uscxml.Interpreter; +import org.uscxml.InterpreterException; + +public class ConsoleFrame extends Frame { + + private static final long serialVersionUID = 3682378173372160680L; + public static Map<Interpreter, Frame> perInterpreter = new HashMap<Interpreter, Frame>(); + + 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); + + 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=\"key.released\" cond=\"_event.data.keyChar == 10\">" + + " <log expr=\"charSeq\" />" + + " <script>" + + " charSeq = \"\";" + + " </script>" + + " </transition>" + + " <transition event=\"*\">" + + " <log label=\"event\" expr=\"dump(_event.data)\" />" + + " <script>charSeq += String.fromCharCode(_event.data.keyChar);</script>" + + " </transition>" + + " </state>" + + "</scxml>"); + + + perInterpreter.put(interpreter, this); + + Thread intrerpreterThread = new Thread(new Runnable() { + @Override + public void run() { + try { + interpreter.interpret(); + } catch (InterpreterException e) { + e.printStackTrace(); + } + } + }); + intrerpreterThread.start(); + + setVisible(true); + } + + public static void main(String[] args) throws InterpreterException { + System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64.jnilib"); + + ConsoleIOProc ioProc = new ConsoleIOProc(); + Factory.getInstance().registerIOProcessor(ioProc); + + ConsoleFrame frame = new ConsoleFrame(); + + } + +} + diff --git a/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java b/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java new file mode 100644 index 0000000..f0ad491 --- /dev/null +++ b/embedding/java/src/org/uscxml/tests/ioprocessor/console/ConsoleIOProc.java @@ -0,0 +1,93 @@ +package org.uscxml.tests.ioprocessor.console; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import org.uscxml.Data; +import org.uscxml.Event; +import org.uscxml.IOProcessor; +import org.uscxml.Interpreter; +import org.uscxml.SendRequest; +import org.uscxml.StringList; + +public class ConsoleIOProc extends IOProcessor implements KeyListener { + + /** IOProcessor */ + @Override + public IOProcessor create(Interpreter interpreter) { + ConsoleIOProc ioProc = new ConsoleIOProc(); + ioProc.swigReleaseOwnership(); + + if (ConsoleFrame.perInterpreter.containsKey(interpreter)) { + ConsoleFrame.perInterpreter.get(interpreter).addKeyListener(ioProc); + } else { + System.err.println("No data for interpreter specific instances"); + } + return ioProc; + } + + /** IOProcessor */ + @Override + public StringList getNames() { + StringList ss = new StringList(); + ss.add("console"); + return ss; + } + + /** IOProcessor */ + @Override + public Data getDataModelVariables() { + // return anything for _ioprocessor['console'] + Data data = Data.fromJSON("{ foo: \"bar\", test: [1,2,3,4,5,6] }"); + return data; + } + + /** IOProcessor */ + @Override + public void send(SendRequest req) { + // interpreter wants to send something, just print on console + System.out.println(req); + } + + /** KeyListener */ + @Override + public void keyPressed(KeyEvent e) { + Event evt = new Event("key.pressed"); + evt.setData(keyEventToData(e)); + returnEvent(evt, true); + } + + /** KeyListener */ + @Override + public void keyReleased(KeyEvent e) { + Event evt = new Event("key.released"); + evt.setData(keyEventToData(e)); + returnEvent(evt, true); + } + + /** KeyListener */ + @Override + public void keyTyped(KeyEvent e) { + Event evt = new Event("key.typed"); + evt.setData(keyEventToData(e)); + returnEvent(evt, true); + } + + static Data keyEventToData(KeyEvent e) { + Data data = new Data(); + data.put("id", new Data(e.getID())); + data.put("keyChar", new Data(e.getKeyChar())); + data.put("keyLocation", new Data(e.getKeyLocation())); + data.put("modifiers", new Data(e.getModifiers())); + data.put("modifiersEx", new Data(e.getModifiersEx())); + data.put("when", new Data(e.getWhen())); + data.put("actionKey", new Data(e.isActionKey())); + data.put("altDown", new Data(e.isAltDown())); + data.put("altGraphDown", new Data(e.isAltGraphDown())); + data.put("ctrlDown", new Data(e.isControlDown())); + data.put("metaDown", new Data(e.isMetaDown())); + data.put("shiftDown", new Data(e.isShiftDown())); + + return data; + } +}
\ No newline at end of file |