Ignore:
Timestamp:
02/16/14 14:28:45 (10 years ago)
Author:
ngw8
Message:

Added an extremely basic 'stop' function to the web parser, so conversions can be cancelled. There's no button, as the JFX thread is occupied with running Javascript most of the time (which has to be done on the JFX thread), meaning it's blocked from receiving clicks. Instead it's using a Swing event handler, listening for the escape key being released anywhere over the widget. (the escape now also stops loading the page)

File:
1 edited

Legend:

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

    r894 r898  
    1010 */
    1111import java.awt.Point;
     12import java.awt.event.KeyListener;
    1213import java.io.BufferedReader;
    1314import java.io.IOException;
     
    105106        private StackPane _overlay;
    106107       
     108        private boolean _parserRunning;
     109       
    107110        private MouseButton _buttonDownId = MouseButton.NONE;
    108111        private MouseEvent _backupEvent = null;
     
    129132
    130133                _panel = (JFXPanel) _swingComponent;
     134
     135                // Quick & easy way of having a cancel function for the web parser.
     136                // Can't just have a JFX button, as the JFX thread is occupied with running JavaScript so it wouldn't receive the click event straight away
     137                _swingComponent.addKeyListener(new KeyListener() {
     138                       
     139                        @Override
     140                        public void keyReleased(java.awt.event.KeyEvent e) {
     141                                if(e.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE) {
     142                                        JfxBrowser.this.cancel();
     143                                }
     144                        }
     145                       
     146                        @Override
     147                        public void keyPressed(java.awt.event.KeyEvent e) {                             
     148                        }
     149                       
     150                        @Override
     151                        public void keyTyped(java.awt.event.KeyEvent e) {                               
     152                        }
     153                });
    131154               
    132155                Platform.runLater(new Runnable() {
     
    838861
    839862        public void getFrameNew() {
    840                 // this.setSize(1000, 1000);
    841                 // this._browser.setBounds(getX(), getY(), getWidth(), getHeight());
    842                 // this.layout(this._browser);
    843 
     863
     864                this._parserRunning = true;
     865               
    844866                try {
    845867                        // hack to make sure we don't try parsing the page from within the JavaFX thread,
     
    852874                } catch (Exception e) {
    853875                        e.printStackTrace();
     876                        this._parserRunning = false;
    854877                }
    855878        }
     
    11061129                return (urlLower.startsWith("http://") || url.startsWith("https://") || urlLower.startsWith("ftp://") || urlLower.startsWith("file://"));
    11071130        }
     1131       
     1132        /**
     1133         * @return Whether the parser is running. If this is true then the parser is running,
     1134         *  however even if it is false, the parser may still be running (but it has been requested to stop)
     1135         */
     1136        public boolean isParserRunning() {
     1137                return this._parserRunning;
     1138        }
     1139       
     1140        /**
     1141         * Should be called when the web parser has finished converting a page
     1142         */
     1143        public void parserFinished() {
     1144                this._parserRunning = false;
     1145        }
     1146       
     1147        /**
     1148         * Cancels the current action being performed by the browser, such as loading a page or converting a page
     1149         */
     1150        public void cancel() {
     1151                if(isParserRunning()) {
     1152                        this._parserRunning = false;
     1153                } else {
     1154                        Platform.runLater(new Runnable() {
     1155                               
     1156                                @Override
     1157                                public void run() {
     1158                                        JfxBrowser.this._webEngine.getLoadWorker().cancel();                                   
     1159                                }
     1160                        });
     1161                }
     1162        }
    11081163}
Note: See TracChangeset for help on using the changeset viewer.