Changeset 785
- Timestamp:
- 01/29/14 16:45:41 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/JSItem.java
r784 r785 25 25 // a method to run that will paint our item using a Graphics2D object 26 26 private final String paint; 27 // a method to run when the item's parent's state changes28 private final String state;29 27 30 28 // we have our own script context since it needs to have some global variables which are specific to each widget … … 40 38 } 41 39 42 private JSItem(Text source, String init, String save, String load, String paint , String state) throws Exception {40 private JSItem(Text source, String init, String save, String load, String paint) throws Exception { 43 41 super(source); 44 42 this.init = init; … … 46 44 this.load = load; 47 45 this.paint = paint; 48 this.state = state;49 46 this.scriptEngine = Javascript2.scriptEngineManager.getEngineByMimeType("application/javascript"); 50 47 this.invocable = (Invocable) this.scriptEngine; … … 58 55 this.scriptEngine.eval("save = " + this.save); 59 56 this.scriptEngine.eval("paint = " + this.paint); 60 this.scriptEngine.eval("state = " + this.state);61 57 this.updateSource(); 62 58 } … … 66 62 source.getData().get(1).replaceAll("\\\\n", "\n"), 67 63 source.getData().get(2).replaceAll("\\\\n", "\n"), 68 source.getData().get(3).replaceAll("\\\\n", "\n"), 69 source.getData().get(4).replaceAll("\\\\n", "\n")); 70 } 71 72 public JSItem(int width, int height, String init, String save, String load, String paint, String state) throws Exception { 73 this(getSauce(width, height), init, save, load, paint, state); 74 } 75 76 public JSItem(String init, String save, String load, String paint, String state) throws Exception { 77 this(100, 100, init, save, load, paint, state); 64 source.getData().get(3).replaceAll("\\\\n", "\n")); 65 } 66 67 public JSItem(int width, int height, String init, String save, String load, String paint) throws Exception { 68 this(getSauce(width, height), init, save, load, paint); 69 } 70 71 public JSItem(String init, String save, String load, String paint) throws Exception { 72 this(100, 100, init, save, load, paint); 78 73 } 79 74 … … 90 85 @Override 91 86 public void paint(Graphics2D g) { 92 this.scriptEngine.put("polygon", this._poly);93 87 try { 94 88 this.invocable.invokeFunction("paint", (Object)g); 95 89 } catch (Exception e) { 96 90 e.printStackTrace(); 97 }98 }99 100 @Override101 public void onParentStateChanged(ItemParentStateChangedEvent e) {102 try {103 this.invocable.invokeFunction("state", (Object)e);104 } catch (Exception e1) {105 e1.printStackTrace();106 91 } 107 92 } … … 168 153 data.add(this.load.replaceAll("[\n\r]", "\\\\n")); 169 154 data.add(this.paint.replaceAll("[\n\r]", "\\\\n")); 170 data.add(this.state.replaceAll("[\n\r]", "\\\\n"));171 155 _source.setData(data); 172 156 … … 182 166 _source.setText(newText.toString()); 183 167 } 168 169 @Override 170 public Integer getWidth() { 171 return this._width; 172 } 173 174 @Override 175 public int getHeight() { 176 return this._height; 177 } 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 thread = null; 192 } 193 } 194 195 public void start() { 196 thread = new Thread(new Runnable() { 197 @Override 198 public void run() { 199 try { 200 scriptEngine.eval("thread = " + code + "\nthread()"); 201 } catch (Exception e) { 202 if(e.getCause() instanceof InterruptedException) { 203 // if the thread was interrupted exit quietly 204 return; 205 } 206 e.printStackTrace(); 207 } 208 } 209 }); 210 thread.start(); 211 } 212 } 213 214 private List<JSThread> threads = new LinkedList<JSThread>(); 215 216 public void addThread(String code) { 217 this.threads.add(new JSThread(code)); 218 } 219 220 @Override 221 public void onParentStateChanged(ItemParentStateChangedEvent e) { 222 switch (e.getEventType()) { 223 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED: 224 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY: 225 case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN: 226 for(JSThread t : this.threads) { 227 t.kill(); 228 } 229 break; 230 231 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED: 232 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY: 233 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN: 234 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY: 235 for(JSThread t : this.threads) { 236 t.start(); 237 } 238 break; 239 240 } 241 } 184 242 185 243 }
Note:
See TracChangeset
for help on using the changeset viewer.