Changeset 898


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)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/io/WebParser.java

    r897 r898  
    513513                                       
    514514                                                // Using Javascript to get an array of all the text nodes in the document so they can be wrapped in spans. Have to
    515                                                 // loop through twice (once to build the array and once actually going through the array, otherwise when the
     515                                                // loop through twice (once here to build the array and once later actually going through the array, otherwise when the
    516516                                                // textnode is removed from the document items end up being skipped)
    517                                                 JSObject textNodes = (JSObject) webEngine.executeScript(""
    518                                                         + "function getTextNodes(rootNode){"
    519                                                                 + "var node;"
    520                                                                 + "var textNodes=[];"
    521                                                                 + "var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);"
    522                                                                 + "while(node=walk.nextNode()) {"
     517                                                webEngine.executeScript(""
     518                                                                        + "var node;"
     519                                                                        + "var textNodes=[];"
     520                                                                        + "var walk = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);"
     521                                                                        );                                     
     522                                               
     523                                                while(webEngine.executeScript("node=walk.nextNode()") != null && browserWidget.isParserRunning()) {
     524                                                       
     525                                                        webEngine.executeScript(""
    523526                                                                        + "if((node.textContent.trim().length > 0)) { "
    524527                                                                                + "textNodes.push(node);"
    525                                                                         + "}"
    526                                                                 + "}"
    527                                                                 + "return textNodes;"
    528                                                         + "}; "
    529                                                         + "getTextNodes(document.body)"
     528                                                                        + "}"
    530529                                                        );
    531                                                
     530                                                }
     531                                               
     532                                                JSObject textNodes = (JSObject) webEngine.executeScript("textNodes");
     533                       
    532534                                                int nodesLength = (Integer) textNodes.getMember("length");
    533535
    534536                                                // Looping through all the text nodes in the document
    535                                                 for (int j = 0; j < nodesLength; j++) {
     537                                                for (int j = 0; j < nodesLength && browserWidget.isParserRunning(); j++) {
    536538                                                        Node currentNode = (Node) textNodes.getSlot(j);
    537539
     
    608610
    609611                        // Loop that scrolls the page horizontally
    610                         for(int i = 0; i < pagesHorizontal.getValue(); i++) {
     612                        for(int i = 0; i < pagesHorizontal.getValue() && browserWidget.isParserRunning(); i++) {
    611613                                                               
    612614                                Platform.runLater(new Runnable() {
     
    627629                               
    628630                                // Loop that scrolls the page vertically (for each horizontal scroll position)
    629                                 for(int j = 0; j < pagesVertical.getValue(); j++) {
     631                                for(int j = 0; j < pagesVertical.getValue()  && browserWidget.isParserRunning(); j++) {
    630632                                       
    631633                                        try {
     
    682684                        }
    683685
    684                         progressBar.set(100);
     686                        if(browserWidget.isParserRunning()) {
     687                                progressBar.set(100);
     688                        } else {
     689                                MessageBay.displayMessage("Web page conversion cancelled");
     690                        }
     691                       
     692                        browserWidget.parserFinished();
    685693
    686694                        Platform.runLater(new Runnable() {
  • 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.