source: trunk/src/org/expeditee/items/widgets/JSWidget.java@ 784

Last change on this file since 784 was 784, checked in by jts21, 10 years ago

Add JSItem class, which like JSWidget is an item controlled by javascript, but with an Expeditee item rather than Swing.
TODO: Add the ability to set input and click handlers for the item.

File size: 3.3 KB
Line 
1package org.expeditee.items.widgets;
2
3import java.awt.BorderLayout;
4import java.util.LinkedList;
5import java.util.List;
6
7import javax.script.Invocable;
8import javax.script.ScriptEngine;
9import javax.swing.JComponent;
10import javax.swing.JPanel;
11
12import org.expeditee.actions.Javascript2;
13import org.expeditee.gui.DisplayIO;
14import org.expeditee.items.Text;
15
16/**
17 * Class for extending Expeditee with widgets coded in Javascript
18 *
19 * @author jts21
20 *
21 */
22public class JSWidget extends DataFrameWidget {
23
24 private static final Object global = Javascript2.eval("new Object()");
25
26 // a method to run that will set up and return the root JComponent for this Widget
27 private final String init;
28 // a method to run to populate a List<String> with our state
29 private final String save;
30 // a method to run that will load state from a String[]
31 private final String load;
32
33 // we have our own script context since it needs to have some global variables which are specific to each widget
34 private final ScriptEngine scriptEngine;
35 private final Invocable invocable;
36 // component created by running our constructor
37 private final JComponent component;
38 // container for our component
39 private final JPanel container;
40
41 private static Text getSauce() {
42 Text source = new Text(DisplayIO.getCurrentFrame().getNextItemID(), "@iw: org.expeditee.items.widgets.JSWidget");
43 source.setParent(DisplayIO.getCurrentFrame());
44 return source;
45 }
46
47 private JSWidget(Text source, String init, String save, String load) throws Exception {
48 super(source, new JPanel(new BorderLayout()), -1, 100, -1, -1, 100, -1);
49 this.init = init;
50 this.save = save;
51 this.load = load;
52 this.container = (JPanel) super._swingComponent;
53 this.scriptEngine = Javascript2.scriptEngineManager.getEngineByMimeType("application/javascript");
54 this.invocable = (Invocable) this.scriptEngine;
55 this.scriptEngine.put("global", global);
56 this.scriptEngine.put("invocable", this.invocable);
57 this.scriptEngine.put("widget", this);
58 this.scriptEngine.put("container", this.container);
59 System.out.println(this.init);
60 this.component = (JComponent) this.scriptEngine.eval("var init = " + this.init + "\ninit()");
61 this.container.add(component);
62 this.scriptEngine.put("component", this.component);
63 this.scriptEngine.eval("save = " + this.save);
64 this.scriptEngine.eval("load = " + this.load);
65 }
66
67 public JSWidget(Text source, String[] args) throws Exception {
68 this(source, source.getData().get(0).replaceAll("\\\\n", "\n"),
69 source.getData().get(1).replaceAll("\\\\n", "\n"),
70 source.getData().get(2).replaceAll("\\\\n", "\n"));
71 this.invocable.invokeFunction("load", (Object) args);
72 }
73
74 public JSWidget(String init, String save, String load) throws Exception {
75 this(getSauce(), init, save, load);
76 }
77
78 @Override
79 protected List<String> getData() {
80 List<String> value = new LinkedList<String>();
81 value.add(this.init.replaceAll("[\n\r]", "\\\\n"));
82 value.add(this.save.replaceAll("[\n\r]", "\\\\n"));
83 value.add(this.load.replaceAll("[\n\r]", "\\\\n"));
84 return value;
85 }
86
87 @Override
88 protected String[] getArgs() {
89 try {
90 List<String> args = new LinkedList<String>();
91 this.invocable.invokeFunction("save", (Object)args);
92 return args.toArray(new String[0]);
93 } catch (Exception e) {
94 e.printStackTrace();
95 return null;
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.