Changeset 785


Ignore:
Timestamp:
01/29/14 16:45:41 (10 years ago)
Author:
jts21
Message:

Tidier thread handling for JSItem (although it does assume that threads should always be restarted, and there is no way of disabling a thread after adding it... TODO: add a flag for disabling a thread, and automatically set that flag if the thread finishes execution successfully)

File:
1 edited

Legend:

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

    r784 r785  
    2525        // a method to run that will paint our item using a Graphics2D object
    2626        private final String paint;
    27         // a method to run when the item's parent's state changes
    28         private final String state;
    2927       
    3028        // we have our own script context since it needs to have some global variables which are specific to each widget
     
    4038        }
    4139
    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 {
    4341                super(source);
    4442                this.init = init;
     
    4644                this.load = load;
    4745                this.paint = paint;
    48                 this.state = state;
    4946                this.scriptEngine = Javascript2.scriptEngineManager.getEngineByMimeType("application/javascript");
    5047                this.invocable = (Invocable) this.scriptEngine;
     
    5855        this.scriptEngine.eval("save = " + this.save);
    5956        this.scriptEngine.eval("paint = " + this.paint);
    60         this.scriptEngine.eval("state = " + this.state);
    6157        this.updateSource();
    6258        }
     
    6662                                source.getData().get(1).replaceAll("\\\\n", "\n"),
    6763                                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);
    7873        }
    7974
     
    9085        @Override
    9186        public void paint(Graphics2D g) {
    92                 this.scriptEngine.put("polygon", this._poly);
    9387                try {
    9488                this.invocable.invokeFunction("paint", (Object)g);
    9589        } catch (Exception e) {
    9690                e.printStackTrace();
    97         }
    98         }
    99        
    100         @Override
    101         public void onParentStateChanged(ItemParentStateChangedEvent e) {
    102                 try {
    103                 this.invocable.invokeFunction("state", (Object)e);
    104         } catch (Exception e1) {
    105                 e1.printStackTrace();
    10691        }
    10792        }
     
    168153                data.add(this.load.replaceAll("[\n\r]", "\\\\n"));
    169154                data.add(this.paint.replaceAll("[\n\r]", "\\\\n"));
    170                 data.add(this.state.replaceAll("[\n\r]", "\\\\n"));
    171155                _source.setData(data);
    172156               
     
    182166                _source.setText(newText.toString());
    183167        }
     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        }
    184242
    185243}
Note: See TracChangeset for help on using the changeset viewer.