Changeset 765
- Timestamp:
- 01/28/14 11:58:55 (10 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/actions/Javascript2.java
r752 r765 6 6 import java.util.List; 7 7 8 import javax.script.Bindings; 8 9 import javax.script.Invocable; 10 import javax.script.ScriptContext; 9 11 import javax.script.ScriptEngine; 10 12 import javax.script.ScriptEngineManager; 11 13 import javax.script.ScriptException; 14 import javax.script.SimpleScriptContext; 12 15 13 16 import org.expeditee.gui.Frame; … … 18 21 import org.expeditee.items.Item; 19 22 import org.expeditee.items.ItemUtils; 20 import org.expeditee.items.Line;21 23 import org.expeditee.items.Text; 22 24 … … 33 35 34 36 public static final ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); 35 p rivate static ScriptEngine se = scriptEngineManager.getEngineByMimeType("application/javascript");37 public static final ScriptEngine scriptEngine = scriptEngineManager.getEngineByMimeType("application/javascript"); 36 38 static { 37 s e.put("invocable", (Invocable) se);39 scriptEngine.put("invocable", (Invocable) scriptEngine); 38 40 } 39 41 … … 84 86 public static Object eval(String code) { 85 87 try { 86 return s e.eval(code);88 return scriptEngine.eval(code); 87 89 } catch (ScriptException e) { 88 90 e.printStackTrace(); … … 95 97 try { 96 98 try { 97 s e.eval(js.toString());99 scriptEngine.eval(js.toString()); 98 100 } catch (ScriptException e) { 99 101 js.handleError(e.getMessage(), e.getLineNumber()); -
trunk/src/org/expeditee/items/widgets/JSWidget.java
r758 r765 5 5 import java.util.List; 6 6 7 import javax.script.Bindings; 7 8 import javax.script.Invocable; 9 import javax.script.ScriptContext; 8 10 import javax.script.ScriptEngine; 9 import javax.script.ScriptException;10 11 import javax.swing.JComponent; 11 12 import javax.swing.JPanel; … … 23 24 public class JSWidget extends DataFrameWidget { 24 25 26 private static final Object global = Javascript2.eval("new Object()"); 27 25 28 // a method to run that will set up and return the root JComponent for this Widget 26 29 private final String init; 27 // a method to run to generate a List<String> to save thestate30 // a method to run to populate a List<String> with our state 28 31 private final String save; 29 32 // a method to run that will load state from a String[] 30 33 private final String load; 31 34 32 // we have our own script enginesince it needs to have some global variables which are specific to each widget35 // we have our own script context since it needs to have some global variables which are specific to each widget 33 36 private final ScriptEngine scriptEngine; 37 private final Invocable invocable; 34 38 // component created by running our constructor 35 39 private final JComponent component; … … 38 42 39 43 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"); 41 45 source.setParent(DisplayIO.getCurrentFrame()); 42 46 return source; … … 50 54 this.container = (JPanel) super._swingComponent; 51 55 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); 53 59 this.scriptEngine.put("widget", this); 54 60 this.scriptEngine.put("container", this.container); … … 63 69 source.getData().get(1).replaceAll("\\\\n", "\n"), 64 70 source.getData().get(2).replaceAll("\\\\n", "\n")); 65 this.scriptEngine.eval(" varload = " + this.load);66 ((Invocable)this.scriptEngine).invokeFunction("load", (Object) args);71 this.scriptEngine.eval("load = " + this.load); 72 this.invocable.invokeFunction("load", (Object) args); 67 73 } 68 74 … … 80 86 } 81 87 82 @SuppressWarnings("unchecked")83 88 @Override 84 89 protected String[] getArgs() { 85 90 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) { 89 99 e.printStackTrace(); 90 100 return null;
Note:
See TracChangeset
for help on using the changeset viewer.