Changeset 899
- Timestamp:
- 02/16/14 15:37:46 (10 years ago)
- Location:
- trunk/src/org/expeditee/items
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/JSItem.java
r799 r899 13 13 import org.expeditee.items.widgets.InteractiveWidget; 14 14 15 public class JSItem extends XRayable {15 public class JSItem extends XRayable implements JSThreadable { 16 16 17 17 private static final Object global = Javascript2.eval("new Object()"); … … 177 177 } 178 178 179 private final class JSThread {180 private Thread thread;181 private String code;182 183 public JSThread(String code) {184 this.code = code;185 this.thread = null;186 }187 188 public void kill() {189 if(thread != null) {190 thread.interrupt();191 try {192 thread.join();193 } catch (InterruptedException e) {194 e.printStackTrace();195 }196 thread = null;197 }198 }199 200 public void start() {201 if(thread != null) {202 this.kill();203 }204 thread = new Thread(new Runnable() {205 @Override206 public void run() {207 try {208 scriptEngine.eval("thread = " + code + "\nthread()");209 } catch (Exception e) {210 if(e.getCause() instanceof InterruptedException) {211 // if the thread was interrupted exit quietly212 return;213 }214 e.printStackTrace();215 }216 }217 });218 thread.start();219 }220 }221 222 179 private List<JSThread> threads = new LinkedList<JSThread>(); 223 180 224 public void addThread(String code) { 225 this.threads.add(new JSThread(code)); 181 public JSThread addThread(String code) { 182 JSThread t = new JSThread(scriptEngine, code); 183 this.threads.add(t); 184 return t; 226 185 } 227 186 … … 242 201 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY: 243 202 for(JSThread t : this.threads) { 244 t.start();203 t.resume(); 245 204 } 246 205 break; -
trunk/src/org/expeditee/items/widgets/JSWidget.java
r784 r899 2 2 3 3 import java.awt.BorderLayout; 4 import java.awt.Component; 5 import java.awt.Graphics2D; 4 6 import java.util.LinkedList; 5 7 import java.util.List; … … 7 9 import javax.script.Invocable; 8 10 import javax.script.ScriptEngine; 9 import javax.swing.JComponent;10 11 import javax.swing.JPanel; 11 12 12 13 import org.expeditee.actions.Javascript2; 13 14 import org.expeditee.gui.DisplayIO; 15 import org.expeditee.items.ItemParentStateChangedEvent; 16 import org.expeditee.items.JSThreadable; 14 17 import org.expeditee.items.Text; 15 18 … … 20 23 * 21 24 */ 22 public class JSWidget extends DataFrameWidget {25 public class JSWidget extends DataFrameWidget implements JSThreadable { 23 26 24 27 private static final Object global = Javascript2.eval("new Object()"); … … 35 38 private final Invocable invocable; 36 39 // component created by running our constructor 37 private final JComponent component;40 private final Component component; 38 41 // container for our component 39 42 private final JPanel container; … … 45 48 } 46 49 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);50 private JSWidget(Text source, int width, int height, String init, String save, String load) throws Exception { 51 super(source, new JPanel(new BorderLayout()), -1, width, -1, -1, height, -1); 49 52 this.init = init; 50 53 this.save = save; … … 58 61 this.scriptEngine.put("container", this.container); 59 62 System.out.println(this.init); 60 this.component = ( JComponent) this.scriptEngine.eval("var init = " + this.init + "\ninit()");63 this.component = (Component) this.scriptEngine.eval("var init = " + this.init + "\ninit()"); 61 64 this.container.add(component); 62 65 this.scriptEngine.put("component", this.component); 63 66 this.scriptEngine.eval("save = " + this.save); 64 67 this.scriptEngine.eval("load = " + this.load); 68 } 69 70 private JSWidget(Text source, String init, String save, String load) throws Exception { 71 this(source, 100, 100, init, save, load); 65 72 } 66 73 … … 72 79 } 73 80 81 public JSWidget(int width, int height, String init, String save, String load) throws Exception { 82 this(getSauce(), width, height, init, save, load); 83 } 84 74 85 public JSWidget(String init, String save, String load) throws Exception { 75 this( getSauce(), init, save, load);86 this(100, 100, init, save, load); 76 87 } 77 88 … … 96 107 } 97 108 } 109 110 private List<JSThread> threads = new LinkedList<JSThread>(); 111 112 public JSThread addThread(String code) { 113 JSThread t = new JSThread(scriptEngine, code); 114 this.threads.add(t); 115 return t; 116 } 117 118 @Override 119 public void onParentStateChanged(int e) { 120 switch (e) { 121 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED: 122 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY: 123 case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN: 124 for(JSThread t : this.threads) { 125 t.kill(); 126 } 127 break; 128 129 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED: 130 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY: 131 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN: 132 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY: 133 for(JSThread t : this.threads) { 134 t.resume(); 135 } 136 break; 137 } 138 } 139 140 @Override 141 protected void paintLink(Graphics2D g) { 142 return; 143 } 98 144 }
Note:
See TracChangeset
for help on using the changeset viewer.