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

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

Add global variable to widgets so they can share state. Tried to make widgets all use the same ScriptEngine with different ScriptContexts, but there doesn't seem to be a way to explicitly invoke a method within a specific script context, which was required for save/load. Also changed save() method so it takes a List<String> as a parameter and populates that rather than having to create one itself.

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