Changeset 796
- Timestamp:
- 01/30/14 17:10:41 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r767 r796 12 12 import java.net.URL; 13 13 import java.text.SimpleDateFormat; 14 import java.util.Arrays; 14 15 import javafx.animation.AnimationTimer; 16 import javafx.application.Platform; 17 import javafx.beans.value.ChangeListener; 18 import javafx.beans.value.ObservableValue; 19 import javafx.concurrent.Worker.State; 20 import javafx.scene.web.WebEngine; 21 22 import javax.imageio.ImageIO; 23 import javax.swing.JComponent; 24 25 import netscape.javascript.JSObject; 26 27 import org.expeditee.gui.DisplayIO; 28 import org.expeditee.gui.Frame; 29 import org.expeditee.gui.FrameIO; 30 import org.expeditee.gui.FrameUtils; 31 import org.expeditee.gui.MessageBay; 32 import org.expeditee.gui.MessageBay.Progress; 33 import org.expeditee.items.ItemUtils; 34 import org.expeditee.items.Justification; 35 import org.expeditee.items.Picture; 36 import org.expeditee.items.Text; 37 import org.w3c.dom.Node; 38 import org.w3c.dom.html.HTMLBodyElement; 15 39 16 40 /* … … 22 46 * If this fails, 'ant build-nojfx' will build with the JfxBrowser, WebParser and JfxbrowserActions excluded from the build path. 23 47 */ 24 25 48 import java.util.Arrays; 26 49 import 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;58 50 59 51 /** … … 290 282 try { 291 283 final Object notifier = new Object(); 284 292 285 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(); 297 290 298 291 final Progress progressBar = MessageBay.displayProgress("Converting web page"); 299 292 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)); 301 294 302 295 frameset.setTitle(pageTitle); … … 309 302 DisplayIO.getCurrentFrame().addItem(link); 310 303 311 link.setLink(frameset Name + "1");304 link.setLink(frameset.getName()); 312 305 313 306 // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page, … … 316 309 317 310 int frameCount = 0; 311 318 312 Frame frameToAddTo = frameset; 313 int thumbWidth = 100; 319 314 320 315 @Override … … 322 317 System.out.println(frameCount); 323 318 // 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) { 325 320 frameCount = 0; 326 321 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"); 327 355 328 356 BufferedImage image = new BufferedImage(jfxPanel.getWidth(), jfxPanel.getHeight(), BufferedImage.TYPE_INT_ARGB); 329 357 330 358 Graphics graphics = image.createGraphics(); 331 359 System.out.println("painted"); 332 360 // Drawing the JfxPanel (containing the webview) to the image 333 361 jfxPanel.paint(graphics); 334 362 335 363 try { 336 frameToAddTo = FrameIO.CreateFrame(frameToAddTo.getFramesetName(), null, null);337 338 364 int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null)); 339 365 … … 341 367 out.mkdirs(); 342 368 ImageIO.write(image, "png", out); 369 343 370 344 371 // Adding the image 345 372 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); 348 377 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 352 395 nextButton.setID(frameToAddTo.getNextItemID()); 353 nextButton.addAction("next");354 396 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); 355 419 356 420 FrameIO.SaveFrame(frameToAddTo); 357 421 FrameIO.SaveFrame(frameset); 358 359 System.out.println("Screenshot taken and added " + frameToAddTo.getName());360 422 361 423 } catch (IOException e) { … … 366 428 image.flush(); 367 429 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(); 395 433 } 396 434 } … … 404 442 webEngine.executeScript("" 405 443 // 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;"); 414 445 415 446 HTMLBodyElement doc = (HTMLBodyElement) webEngine.executeScript("document.body"); … … 1112 1143 JSObject bounds; 1113 1144 1114 1115 1145 // CSS style for the element 1116 1146 style = (JSObject) window.call("getComputedStyle", new Object[] { currentNode.getParentNode() }); … … 1131 1161 // Checking if the element is actually visible on the page 1132 1162 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"))) { 1134 1165 1135 1166 String fontSize = ((String) style.call("getPropertyValue", new Object[] { "font-size" })); … … 1308 1339 } 1309 1340 } 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 } 1310 1357 }
Note:
See TracChangeset
for help on using the changeset viewer.