Changeset 796


Ignore:
Timestamp:
01/30/14 17:10:41 (10 years ago)
Author:
ngw8
Message:

Web parser changes: added overview thumbnail page, hidden elements no longer added, although first converted page is currently broken (text is still in bg image)

File:
1 edited

Legend:

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

    r767 r796  
    1212import java.net.URL;
    1313import java.text.SimpleDateFormat;
    14 import java.util.Arrays;
     14
     15import javafx.animation.AnimationTimer;
     16import javafx.application.Platform;
     17import javafx.beans.value.ChangeListener;
     18import javafx.beans.value.ObservableValue;
     19import javafx.concurrent.Worker.State;
     20import javafx.scene.web.WebEngine;
     21
     22import javax.imageio.ImageIO;
     23import javax.swing.JComponent;
     24
     25import netscape.javascript.JSObject;
     26
     27import org.expeditee.gui.DisplayIO;
     28import org.expeditee.gui.Frame;
     29import org.expeditee.gui.FrameIO;
     30import org.expeditee.gui.FrameUtils;
     31import org.expeditee.gui.MessageBay;
     32import org.expeditee.gui.MessageBay.Progress;
     33import org.expeditee.items.ItemUtils;
     34import org.expeditee.items.Justification;
     35import org.expeditee.items.Picture;
     36import org.expeditee.items.Text;
     37import org.w3c.dom.Node;
     38import org.w3c.dom.html.HTMLBodyElement;
    1539
    1640/*
     
    2246 * If this fails, 'ant build-nojfx' will build with the JfxBrowser, WebParser and JfxbrowserActions excluded from the build path.
    2347 */
    24 
    25 
     48import java.util.Arrays;
    2649import java.util.Date;
    27 
    28 import javafx.animation.AnimationTimer;
    29 import javafx.application.Platform;
    30 import javafx.beans.value.ChangeListener;
    31 import javafx.beans.value.ObservableValue;
    32 import javafx.concurrent.Worker.State;
    33 import javafx.scene.transform.Rotate;
    34 import javafx.scene.web.WebEngine;
    35 
    36 import javax.imageio.ImageIO;
    37 import javax.swing.JComponent;
    38 
    39 import netscape.javascript.JSObject;
    40 
    41 import org.expeditee.gui.DisplayIO;
    42 import org.expeditee.gui.Frame;
    43 import org.expeditee.gui.FrameCreator;
    44 import org.expeditee.gui.FrameGraphics;
    45 import org.expeditee.gui.FrameIO;
    46 import org.expeditee.gui.FrameUtils;
    47 import org.expeditee.gui.MessageBay;
    48 import org.expeditee.gui.MessageBay.Progress;
    49 import org.expeditee.importer.FrameDNDTransferHandler;
    50 import org.expeditee.items.ItemUtils;
    51 import org.expeditee.items.Justification;
    52 import org.expeditee.items.Picture;
    53 import org.expeditee.items.Text;
    54 import org.w3c.dom.Node;
    55 import org.w3c.dom.html.HTMLBodyElement;
    56 
    57 import com.sun.org.apache.bcel.internal.generic.NEW;
    5850
    5951/**
     
    290282                try {
    291283                        final Object notifier = new Object();
     284                       
    292285                        final MutableBool bottomReached = new MutableBool(false);
    293 
    294                         String pageTitle = webEngine.getTitle();
    295 
    296                         String framesetName = FrameIO.ConvertToValidFramesetName((new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(new Date())) + pageTitle);
     286                       
     287                        final MutableInt pageCount = new MutableInt(0);
     288
     289                        final String pageTitle = webEngine.getTitle();
    297290
    298291                        final Progress progressBar = MessageBay.displayProgress("Converting web page");
    299292
    300                         final Frame frameset = FrameIO.CreateNewFrameset(framesetName);
     293                        final Frame frameset = FrameIO.CreateNewFrameset(FrameIO.ConvertToValidFramesetName((new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(new Date())) + pageTitle));
    301294
    302295                        frameset.setTitle(pageTitle);
     
    309302                        DisplayIO.getCurrentFrame().addItem(link);
    310303
    311                         link.setLink(framesetName + "1");
     304                        link.setLink(frameset.getName());
    312305
    313306                        // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page,
     
    316309
    317310                                int frameCount = 0;
     311
    318312                                Frame frameToAddTo = frameset;
     313                                int thumbWidth = 100;
    319314
    320315                                @Override
     
    322317                                        System.out.println(frameCount);
    323318                                        // Must wait 2 frames before taking a snapshot of the webview, otherwise JavaFX won't have redrawn
    324                                         if (frameCount++ > 3) {
     319                                        if (frameCount++ > 1) {
    325320                                                frameCount = 0;
    326321                                                this.stop();
     322                                               
     323                                                pageCount.setValue(pageCount.getValue() + 1);
     324
     325                                                frameToAddTo = FrameIO.CreateFrame(frameToAddTo.getFramesetName(), pageTitle + " " + pageCount.getValue(), null);
     326
     327                                                try {
     328                                                        if(pageCount.getValue() > 1) {
     329                                                                webEngine.executeScript(""
     330                                                                                + "document.getElementsByTagName('head')[0].removeChild(cssHide);");
     331                                                        }
     332                                                        HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
     333
     334                                                        JSObject window = (JSObject) webEngine.executeScript("window");
     335
     336                                                        int visibleWidth = (int) webEngine.executeScript("window.innerWidth");
     337                                                        int visibleHeight = (int) webEngine.executeScript("window.innerHeight");
     338
     339                                                        WebParser.addTextToFrame(doc, visibleWidth, visibleHeight, window, webEngine, frameToAddTo);
     340                                                        System.out.println("added text");
     341                                                        FrameIO.SaveFrame(frameToAddTo);
     342                                                } catch (Exception ex) {
     343                                                        ex.printStackTrace();
     344                                                }
     345
     346                                                webEngine.executeScript(""
     347                                                                // Setting all text to be hidden before taking the screenshot
     348                                                                + "var cssHide = document.createElement('style');"
     349                                                                + "cssHide.type = 'text/css';"
     350                                                                + "var style = 'WordSpan { visibility: hidden; color: red }';"
     351                                                                + "cssHide.appendChild(document.createTextNode(style));"
     352                                                                + "document.getElementsByTagName('head')[0].appendChild(cssHide);");
     353                                               
     354                                                System.out.println("hidden");
    327355
    328356                                                BufferedImage image = new BufferedImage(jfxPanel.getWidth(), jfxPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    329357
    330358                                                Graphics graphics = image.createGraphics();
    331 
     359                                                System.out.println("painted");
    332360                                                // Drawing the JfxPanel (containing the webview) to the image
    333361                                                jfxPanel.paint(graphics);
    334362
    335363                                                try {
    336                                                         frameToAddTo = FrameIO.CreateFrame(frameToAddTo.getFramesetName(), null, null);
    337 
    338364                                                        int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null));
    339365
     
    341367                                                        out.mkdirs();
    342368                                                        ImageIO.write(image, "png", out);
     369                                                       
    343370
    344371                                                        // Adding the image
    345372                                                        frameToAddTo.addText(0, 0, "@i: " + out.getName(), null);
    346 
    347                                                         Text thumb = frameset.addText(100, 100, "@i: " + out.getName() + " " + 200, null);
     373                                                       
     374                                                        Text thumb = frameset.addText(100, (int) (((((float) thumbWidth / image.getWidth()) * image.getHeight()) + 10) * pageCount.getValue()), "@i: " + out.getName() + " "
     375                                                                        + thumbWidth,
     376                                                                        null);
    348377                                                        thumb.setLink(frameToAddTo.getName());
    349 
    350                                                         // Button to go to the next page
    351                                                         Text nextButton = (Text) FrameCreator.createButton("Next", null, null, 10F, 10F);
     378                                                        thumb.setBorderColor(Color.lightGray);
     379                                                        thumb.setThickness(1);
     380
     381                                                        // Button to go to the next frame/page
     382                                                        Text nextButton = new Text("Next");
     383
     384                                                        nextButton.addAction("next");
     385                                                        nextButton.setBorderColor(new Color(0.7f, 0.7f, 0.7f));
     386                                                        nextButton.setBackgroundColor(new Color(0.9f, 0.9f, 0.9f));
     387                                                        nextButton.setThickness(1);
     388                                                        nextButton.setActionMark(false);
     389                                                        nextButton.setWidth(70);
     390                                                        nextButton.setFamily("Roboto Condensed Light");
     391                                                        nextButton.setJustification(Justification.center);
     392                                                        nextButton.setAnchorRight(10f);
     393                                                        nextButton.setAnchorBottom(10f);
     394
    352395                                                        nextButton.setID(frameToAddTo.getNextItemID());
    353                                                         nextButton.addAction("next");
    354396                                                        frameToAddTo.addItem(nextButton);
     397
     398                                                        // Button to go to the previous frame/page
     399                                                        if (pageCount.getValue() > 1) {
     400                                                                Text previousButton = nextButton.copy();
     401                                                                previousButton.setText("Previous");
     402                                                                previousButton.setAction("previous");
     403                                                                previousButton.setAnchorRight(180f);
     404                                                                previousButton.setAnchorBottom(10f);
     405
     406                                                                frameToAddTo.addItem(previousButton);
     407                                                        }
     408
     409                                                        // Button to return to the index/overview page
     410                                                        Text indexButton = nextButton.copy();
     411                                                        indexButton.setText("Index");
     412                                                        indexButton.setAction(null);
     413                                                        indexButton.setLink(frameset.getName());
     414                                                        indexButton.setLinkMark(false);
     415                                                        indexButton.setAnchorRight(95f);
     416                                                        indexButton.setAnchorBottom(10f);
     417
     418                                                        frameToAddTo.addItem(indexButton);
    355419
    356420                                                        FrameIO.SaveFrame(frameToAddTo);
    357421                                                        FrameIO.SaveFrame(frameset);
    358 
    359                                                         System.out.println("Screenshot taken and added " + frameToAddTo.getName());
    360422
    361423                                                } catch (IOException e) {
     
    366428                                                image.flush();
    367429
    368                                                 try {
    369                                                         Platform.runLater(new Runnable() {
    370                                                                 @Override
    371                                                                 public void run() {
    372                                                                         try {
    373                                                                                 HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
    374 
    375                                                                                 JSObject window = (JSObject) webEngine.executeScript("window");
    376 
    377                                                                                 int visibleWidth = (int) webEngine.executeScript("window.innerWidth");
    378                                                                                 int visibleHeight = (int) webEngine.executeScript("window.innerHeight");
    379 
    380                                                                                 System.out.println("Adding text " + frameToAddTo.getName());
    381                                                                                 WebParser.addTextToFrame(doc, visibleWidth, visibleHeight, window, webEngine, frameToAddTo);
    382                                                                                 FrameIO.SaveFrame(frameToAddTo);
    383                                                                                 System.out.println("Added text " + frameToAddTo.getName());
    384                                                                         } catch (Exception ex) {
    385                                                                                 ex.printStackTrace();
    386                                                                         }
    387 
    388                                                                         synchronized (notifier) {
    389                                                                                 notifier.notify();
    390                                                                         }
    391                                                                 }
    392                                                         });
    393                                                 } catch (Exception ex) {
    394                                                         ex.printStackTrace();
     430                                                synchronized (notifier) {
     431                                                        // Notifying that the timer has finished
     432                                                        notifier.notify();
    395433                                                }
    396434                                        }
     
    404442                                                webEngine.executeScript(""
    405443                                                                // Initializing the counter used when scrolling the page
    406                                                                 + "var scrollCounter = 0;"
    407                                                                
    408                                                                 // Setting all text to be hidden
    409                                                                 + "var css = document.createElement('style');"
    410                                                                 + "css.type = 'text/css';"
    411                                                                 + "var style = 'WordSpan { visibility: hidden }';"
    412                                                                 + "css.appendChild(document.createTextNode(style));"
    413                                                                 + "document.getElementsByTagName('head')[0].appendChild(css);");
     444                                                                + "var scrollCounter = 0;");
    414445
    415446                                                HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
     
    11121143                        JSObject bounds;
    11131144
    1114 
    11151145                        // CSS style for the element
    11161146                        style = (JSObject) window.call("getComputedStyle", new Object[] { currentNode.getParentNode() });
     
    11311161                        // Checking if the element is actually visible on the page
    11321162                        if (width > 0 && height > 0 && x + width > 0 && y + height > 0 && x <= visibleWidth && y <= visibleHeight
    1133                                         && !(((String) style.call("getPropertyValue", new Object[] { "display" })).equals("none"))) {
     1163                                        && !(((String) style.call("getPropertyValue", new Object[] { "display" })).equals("none"))
     1164                                        && !(((String) style.call("getPropertyValue", new Object[] { "visibility" })).equals("hidden"))) {
    11341165
    11351166                                String fontSize = ((String) style.call("getPropertyValue", new Object[] { "font-size" }));
     
    13081339                }
    13091340        }
     1341
     1342        private static class MutableInt {
     1343                private int value;
     1344
     1345                public MutableInt(int value) {
     1346                        this.value = value;
     1347                }
     1348
     1349                public int getValue() {
     1350                        return value;
     1351                }
     1352
     1353                public void setValue(int value) {
     1354                        this.value = value;
     1355                }
     1356        }
    13101357}
Note: See TracChangeset for help on using the changeset viewer.