Changeset 765


Ignore:
Timestamp:
01/28/14 11:58:55 (10 years ago)
Author:
jts21
Message:

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.

Location:
trunk/src/org/expeditee
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Javascript2.java

    r752 r765  
    66import java.util.List;
    77
     8import javax.script.Bindings;
    89import javax.script.Invocable;
     10import javax.script.ScriptContext;
    911import javax.script.ScriptEngine;
    1012import javax.script.ScriptEngineManager;
    1113import javax.script.ScriptException;
     14import javax.script.SimpleScriptContext;
    1215
    1316import org.expeditee.gui.Frame;
     
    1821import org.expeditee.items.Item;
    1922import org.expeditee.items.ItemUtils;
    20 import org.expeditee.items.Line;
    2123import org.expeditee.items.Text;
    2224
     
    3335       
    3436        public static final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    35         private static ScriptEngine se = scriptEngineManager.getEngineByMimeType("application/javascript");
     37        public static final ScriptEngine scriptEngine = scriptEngineManager.getEngineByMimeType("application/javascript");
    3638        static {
    37                 se.put("invocable", (Invocable) se);
     39                scriptEngine.put("invocable", (Invocable) scriptEngine);
    3840        }
    3941       
     
    8486        public static Object eval(String code) {
    8587                try {
    86                 return se.eval(code);
     88                return scriptEngine.eval(code);
    8789        } catch (ScriptException e) {
    8890                e.printStackTrace();
     
    9597                try {
    9698                try {
    97                 se.eval(js.toString());
     99                scriptEngine.eval(js.toString());
    98100            } catch (ScriptException e) {
    99101                        js.handleError(e.getMessage(), e.getLineNumber());
  • trunk/src/org/expeditee/items/widgets/JSWidget.java

    r758 r765  
    55import java.util.List;
    66
     7import javax.script.Bindings;
    78import javax.script.Invocable;
     9import javax.script.ScriptContext;
    810import javax.script.ScriptEngine;
    9 import javax.script.ScriptException;
    1011import javax.swing.JComponent;
    1112import javax.swing.JPanel;
     
    2324public class JSWidget extends DataFrameWidget {
    2425       
     26        private static final Object global = Javascript2.eval("new Object()");
     27       
    2528        // a method to run that will set up and return the root JComponent for this Widget
    2629        private final String init;
    27         // a method to run to generate a List<String> to save the state
     30        // a method to run to populate a List<String> with our state
    2831        private final String save;
    2932        // a method to run that will load state from a String[]
    3033        private final String load;
    3134       
    32         // we have our own script engine since it needs to have some global variables which are specific to each widget
     35        // we have our own script context since it needs to have some global variables which are specific to each widget
    3336        private final ScriptEngine scriptEngine;
     37        private final Invocable invocable;
    3438        // component created by running our constructor
    3539        private final JComponent component;
     
    3842       
    3943        private static Text getSauce() {
    40                 Text source = new Text("@iw: org.expeditee.items.widgets.JSWidget");
     44                Text source = new Text(DisplayIO.getCurrentFrame().getNextItemID(), "@iw: org.expeditee.items.widgets.JSWidget");
    4145                source.setParent(DisplayIO.getCurrentFrame());
    4246                return source;
     
    5054                this.container = (JPanel) super._swingComponent;
    5155                this.scriptEngine = Javascript2.scriptEngineManager.getEngineByMimeType("application/javascript");
    52                 this.scriptEngine.put("invocable", (Invocable) this.scriptEngine);
     56                this.invocable = (Invocable) this.scriptEngine;
     57                this.scriptEngine.put("global", global);
     58                this.scriptEngine.put("invocable", this.invocable);
    5359                this.scriptEngine.put("widget", this);
    5460                this.scriptEngine.put("container", this.container);
     
    6369                                source.getData().get(1).replaceAll("\\\\n", "\n"),
    6470                                source.getData().get(2).replaceAll("\\\\n", "\n"));
    65                 this.scriptEngine.eval("var load = " + this.load);
    66                 ((Invocable)this.scriptEngine).invokeFunction("load", (Object) args);
     71                this.scriptEngine.eval("load = " + this.load);
     72                this.invocable.invokeFunction("load", (Object) args);
    6773        }
    6874       
     
    8086        }
    8187
    82         @SuppressWarnings("unchecked")
    8388    @Override
    8489        protected String[] getArgs() {
    8590                try {
    86                         List<String> data = (List<String>)this.scriptEngine.eval("var save = " + this.save + "\nsave()");
    87                 return data.toArray(new String[0]);
    88         } catch (ScriptException e) {
     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) {
    8999                e.printStackTrace();
    90100                return null;
Note: See TracChangeset for help on using the changeset viewer.