Changeset 899


Ignore:
Timestamp:
02/16/14 15:37:46 (10 years ago)
Author:
jts21
Message:

Improvements to JSItem/JSWidget (better thread and drawing handling, required for an example pong game I made for the presentation)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/JSItem.java

    r799 r899  
    1313import org.expeditee.items.widgets.InteractiveWidget;
    1414
    15 public class JSItem extends XRayable {
     15public class JSItem extends XRayable implements JSThreadable {
    1616       
    1717        private static final Object global = Javascript2.eval("new Object()");
     
    177177        }
    178178       
    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                         @Override
    206                         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 quietly
    212                                                 return;
    213                                         }
    214                                         e.printStackTrace();
    215                     }
    216                         }
    217                 });
    218                         thread.start();
    219                 }
    220         }
    221        
    222179        private List<JSThread> threads = new LinkedList<JSThread>();
    223180       
    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;
    226185        }
    227186       
     
    242201                case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
    243202                        for(JSThread t : this.threads) {
    244                                 t.start();
     203                                        t.resume();
    245204                        }
    246205                        break;
  • trunk/src/org/expeditee/items/widgets/JSWidget.java

    r784 r899  
    22
    33import java.awt.BorderLayout;
     4import java.awt.Component;
     5import java.awt.Graphics2D;
    46import java.util.LinkedList;
    57import java.util.List;
     
    79import javax.script.Invocable;
    810import javax.script.ScriptEngine;
    9 import javax.swing.JComponent;
    1011import javax.swing.JPanel;
    1112
    1213import org.expeditee.actions.Javascript2;
    1314import org.expeditee.gui.DisplayIO;
     15import org.expeditee.items.ItemParentStateChangedEvent;
     16import org.expeditee.items.JSThreadable;
    1417import org.expeditee.items.Text;
    1518
     
    2023 *
    2124 */
    22 public class JSWidget extends DataFrameWidget {
     25public class JSWidget extends DataFrameWidget implements JSThreadable {
    2326       
    2427        private static final Object global = Javascript2.eval("new Object()");
     
    3538        private final Invocable invocable;
    3639        // component created by running our constructor
    37         private final JComponent component;
     40        private final Component component;
    3841        // container for our component
    3942        private final JPanel container;
     
    4548        }
    4649       
    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);
    4952                this.init = init;
    5053                this.save = save;
     
    5861                this.scriptEngine.put("container", this.container);
    5962        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()");
    6164        this.container.add(component);
    6265        this.scriptEngine.put("component", this.component);
    6366        this.scriptEngine.eval("save = " + this.save);
    6467        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);
    6572        }
    6673       
     
    7279        }
    7380       
     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       
    7485        public JSWidget(String init, String save, String load) throws Exception {
    75                 this(getSauce(), init, save, load);
     86                this(100, 100, init, save, load);
    7687        }
    7788       
     
    96107        }
    97108        }
     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        }
    98144}
Note: See TracChangeset for help on using the changeset viewer.