Changeset 808
- Timestamp:
- 02/03/14 11:16:55 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r804 r808 3 3 import java.awt.Color; 4 4 import java.awt.Font; 5 import java.awt.Graphics;6 5 import java.awt.image.BufferedImage; 7 6 import java.io.File; … … 11 10 import java.net.MalformedURLException; 12 11 import java.net.URL; 13 import java.nio.IntBuffer;14 12 import 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 52 13 /* 53 14 * 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. … … 60 21 import java.util.Arrays; 61 22 import java.util.Date; 23 24 import javafx.animation.AnimationTimer; 25 import javafx.application.Platform; 26 import javafx.beans.value.ChangeListener; 27 import javafx.beans.value.ObservableValue; 28 import javafx.concurrent.Worker.State; 29 import javafx.embed.swing.SwingFXUtils; 30 import javafx.scene.SnapshotParameters; 31 import javafx.scene.image.WritableImage; 32 import javafx.scene.web.WebEngine; 33 import javafx.scene.web.WebView; 34 35 import javax.imageio.ImageIO; 36 37 import netscape.javascript.JSObject; 38 39 import org.expeditee.gui.DisplayIO; 40 import org.expeditee.gui.Frame; 41 import org.expeditee.gui.FrameIO; 42 import org.expeditee.gui.FrameMouseActions; 43 import org.expeditee.gui.FrameUtils; 44 import org.expeditee.gui.MessageBay; 45 import org.expeditee.gui.MessageBay.Progress; 46 import org.expeditee.items.ItemUtils; 47 import org.expeditee.items.Justification; 48 import org.expeditee.items.Picture; 49 import org.expeditee.items.Text; 50 import org.w3c.dom.Node; 51 import org.w3c.dom.html.HTMLBodyElement; 62 52 63 53 /** … … 308 298 frameset.getTitleItem().setSize(14); 309 299 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); 316 301 link.setLink(frameset.getName()); 302 303 FrameMouseActions.pickup(link); 317 304 318 305 // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page, … … 338 325 339 326 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 344 331 HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body"); 345 332 … … 358 345 webEngine.executeScript("" 359 346 // 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));"); 365 349 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 374 354 // Getting a BufferedImage from the JavaFX image 375 BufferedImage image = SwingFXUtils.fromFXImage( tmp, null);355 BufferedImage image = SwingFXUtils.fromFXImage(img, null); 376 356 377 357 try { … … 381 361 out.mkdirs(); 382 362 ImageIO.write(image, "png", out); 383 384 385 // Adding the image 363 364 // Adding the image to the frame 386 365 frameToAddTo.addText(0, 0, "@i: " + out.getName(), null); 387 366 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); 391 369 thumb.setLink(frameToAddTo.getName()); 392 370 thumb.setBorderColor(Color.lightGray); … … 457 435 + "var scrollCounter = 0;"); 458 436 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 459 446 HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body"); 460 447 … … 549 536 // Will never reach 100% here, as the processing is not quite finished - progress is set to 100% at the end of 550 537 // the addPageToFrame loop below 551 progressBar.set(( 100 * (j)) / nodesLength);538 progressBar.set((50 * (j + 1)) / nodesLength); 552 539 } 553 540 … … 580 567 synchronized (notifier) { 581 568 try { 582 // Waiting for the JavaFX threadto finish569 // Waiting for the page setup (splitting into spans) to finish 583 570 notifier.wait(); 584 571 } catch (InterruptedException e) { … … 614 601 synchronized (notifier) { 615 602 try { 616 // Waiting for the JavaFX thread to finish603 // Waiting for the page to be scrolled 617 604 notifier.wait(); 618 605 } catch (InterruptedException e) { … … 635 622 636 623 } 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 }); 637 635 638 636 } catch (Exception ex) {
Note:
See TracChangeset
for help on using the changeset viewer.