Changeset 808


Ignore:
Timestamp:
02/03/14 11:16:55 (10 years ago)
Author:
ngw8
Message:

Fixed webparser bug where first converted page had text visible on the bg image

File:
1 edited

Legend:

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

    r804 r808  
    33import java.awt.Color;
    44import java.awt.Font;
    5 import java.awt.Graphics;
    65import java.awt.image.BufferedImage;
    76import java.io.File;
     
    1110import java.net.MalformedURLException;
    1211import java.net.URL;
    13 import java.nio.IntBuffer;
    1412import java.text.SimpleDateFormat;
    15 
    16 import javafx.animation.AnimationTimer;
    17 import javafx.application.Platform;
    18 import javafx.beans.value.ChangeListener;
    19 import javafx.beans.value.ObservableValue;
    20 import javafx.concurrent.Worker.State;
    21 import javafx.embed.swing.SwingFXUtils;
    22 import javafx.scene.SnapshotParameters;
    23 import javafx.scene.image.WritableImage;
    24 import javafx.scene.image.WritablePixelFormat;
    25 import javafx.scene.web.WebEngine;
    26 import javafx.scene.web.WebView;
    27 
    28 import javax.imageio.ImageIO;
    29 import javax.swing.JComponent;
    30 
    31 import netscape.javascript.JSObject;
    32 
    33 import org.expeditee.gui.DisplayIO;
    34 import org.expeditee.gui.Frame;
    35 import org.expeditee.gui.FrameIO;
    36 import org.expeditee.gui.FrameUtils;
    37 import org.expeditee.gui.MessageBay;
    38 import org.expeditee.gui.MessageBay.Progress;
    39 import org.expeditee.items.ItemUtils;
    40 import org.expeditee.items.Justification;
    41 import org.expeditee.items.Picture;
    42 import org.expeditee.items.Text;
    43 import org.w3c.dom.Node;
    44 import org.w3c.dom.html.HTMLBodyElement;
    45 
    46 
    47 
    48 
    49 
    50 
    51 
    5213/*
    5314 * JavaFX is not on the default java classpath until Java 8 (but is still included with Java 7), so your IDE will probably complain that the imports below can't be resolved.
     
    6021import java.util.Arrays;
    6122import java.util.Date;
     23
     24import javafx.animation.AnimationTimer;
     25import javafx.application.Platform;
     26import javafx.beans.value.ChangeListener;
     27import javafx.beans.value.ObservableValue;
     28import javafx.concurrent.Worker.State;
     29import javafx.embed.swing.SwingFXUtils;
     30import javafx.scene.SnapshotParameters;
     31import javafx.scene.image.WritableImage;
     32import javafx.scene.web.WebEngine;
     33import javafx.scene.web.WebView;
     34
     35import javax.imageio.ImageIO;
     36
     37import netscape.javascript.JSObject;
     38
     39import org.expeditee.gui.DisplayIO;
     40import org.expeditee.gui.Frame;
     41import org.expeditee.gui.FrameIO;
     42import org.expeditee.gui.FrameMouseActions;
     43import org.expeditee.gui.FrameUtils;
     44import org.expeditee.gui.MessageBay;
     45import org.expeditee.gui.MessageBay.Progress;
     46import org.expeditee.items.ItemUtils;
     47import org.expeditee.items.Justification;
     48import org.expeditee.items.Picture;
     49import org.expeditee.items.Text;
     50import org.w3c.dom.Node;
     51import org.w3c.dom.html.HTMLBodyElement;
    6252
    6353/**
     
    308298                        frameset.getTitleItem().setSize(14);
    309299
    310                         Text link = new Text(DisplayIO.getCurrentFrame().getNextItemID(), webEngine.getTitle());
    311 
    312                         link.setPosition(100, 100);
    313 
    314                         DisplayIO.getCurrentFrame().addItem(link);
    315 
     300                        Text link = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(), webEngine.getTitle(), null);
    316301                        link.setLink(frameset.getName());
     302
     303                        FrameMouseActions.pickup(link);
    317304
    318305                        // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page,
     
    338325
    339326                                                try {
    340                                                         if(pageCount.getValue() > 1) {
    341                                                                 webEngine.executeScript(""
    342                                                                                 + "document.getElementsByTagName('head')[0].removeChild(cssHide);");
    343                                                         }
     327                                                        // removing the CSS that hides the text (otherwise the text would not pass the visibility check that is run on
     328                                                        // it before adding it to the frame)
     329                                                        webEngine.executeScript("cssHide.innerHTML = '';");
     330
    344331                                                        HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
    345332
     
    358345                                                webEngine.executeScript(""
    359346                                                                // Setting all text to be hidden before taking the screenshot
    360                                                                 + "var cssHide = document.createElement('style');"
    361                                                                 + "cssHide.type = 'text/css';"
    362                                                                 + "var style = 'WordSpan { visibility: hidden; color: red }';"
    363                                                                 + "cssHide.appendChild(document.createTextNode(style));"
    364                                                                 + "document.getElementsByTagName('head')[0].appendChild(cssHide);");
     347                                                                + "var style = 'WordSpan { visibility: hidden !important;}';"
     348                                                                + "cssHide.appendChild(document.createTextNode(style));");
    365349                                               
    366                                                 System.out.println("hidden");
    367 
    368                                                 WritableImage tmp = new WritableImage((int)webView.getWidth(), (int)webView.getHeight());
    369 
    370                                                 System.out.println("painted");
    371                                                 // Drawing the JfxPanel (containing the webview) to the image
    372                                                 webView.snapshot(new SnapshotParameters(), tmp);
    373                                                
     350                                                WritableImage img = new WritableImage((int)webView.getWidth(), (int)webView.getHeight());
     351
     352                                                webView.snapshot(new SnapshotParameters(), img);
     353
    374354                                                // Getting a BufferedImage from the JavaFX image
    375                                                 BufferedImage image = SwingFXUtils.fromFXImage(tmp, null);
     355                                                BufferedImage image = SwingFXUtils.fromFXImage(img, null);
    376356
    377357                                                try {
     
    381361                                                        out.mkdirs();
    382362                                                        ImageIO.write(image, "png", out);
    383                                                        
    384 
    385                                                         // Adding the image
     363
     364                                                        // Adding the image to the frame
    386365                                                        frameToAddTo.addText(0, 0, "@i: " + out.getName(), null);
    387366                                                       
    388                                                         Text thumb = frameset.addText(100, (int) (((((float) thumbWidth / image.getWidth()) * image.getHeight()) + 10) * pageCount.getValue()), "@i: " + out.getName() + " "
    389                                                                         + thumbWidth,
    390                                                                         null);
     367                                                        // Adding thumbnail to the overview page
     368                                                        Text thumb = frameset.addText(100, (int) (((((float) thumbWidth / image.getWidth()) * image.getHeight()) + 10) * pageCount.getValue()), "@i: " + out.getName() + " " + thumbWidth, null);
    391369                                                        thumb.setLink(frameToAddTo.getName());
    392370                                                        thumb.setBorderColor(Color.lightGray);
     
    457435                                                                + "var scrollCounter = 0;");
    458436
     437                                                // Setting up the element that contains the CSS to hide all text.
     438                                                // This is wiped before the text is converted, then re-added before taking the screenshot
     439                                                webEngine.executeScript(""
     440                                                                + "var cssHide = document.createElement('style');"
     441                                                                + "cssHide.type = 'text/css';"
     442                                                                + "var style = 'WordSpan { visibility: hidden !important;}';"
     443                                                                + "cssHide.appendChild(document.createTextNode(style));"
     444                                                                + "document.getElementsByTagName('head')[0].appendChild(cssHide);");
     445                                               
    459446                                                HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body");
    460447
     
    549536                                                        // Will never reach 100% here, as the processing is not quite finished - progress is set to 100% at the end of
    550537                                                        // the addPageToFrame loop below
    551                                                         progressBar.set((100 * (j)) / nodesLength);
     538                                                        progressBar.set((50 * (j + 1)) / nodesLength);
    552539                                                }
    553540
     
    580567                        synchronized (notifier) {
    581568                                try {
    582                                         // Waiting for the JavaFX thread to finish
     569                                        // Waiting for the page setup (splitting into spans) to finish
    583570                                        notifier.wait();
    584571                                } catch (InterruptedException e) {
     
    614601                                synchronized (notifier) {
    615602                                        try {
    616                                                 // Waiting for the JavaFX thread to finish
     603                                                // Waiting for the page to be scrolled
    617604                                                notifier.wait();
    618605                                        } catch (InterruptedException e) {
     
    635622
    636623                        }
     624
     625                        progressBar.set(100);
     626
     627                        Platform.runLater(new Runnable() {
     628                                @Override
     629                                public void run() {
     630                                        // Reloading the page once the parsing is done - only realistic way to reset (i.e. remove all the added WordSpan tags)
     631                                        // the page
     632                                        webEngine.reload();
     633                                }
     634                        });
    637635
    638636                } catch (Exception ex) {
Note: See TracChangeset for help on using the changeset viewer.