Ignore:
Timestamp:
05/10/18 16:04:51 (6 years ago)
Author:
davidb
Message:

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File:
1 edited

Legend:

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

    r919 r1102  
    1919package org.expeditee.io;
    2020
    21 import java.awt.Color;
    22 import java.awt.Font;
    23 import java.awt.image.BufferedImage;
    2421import java.io.File;
    2522import java.io.IOException;
     
    4542import javafx.beans.value.ObservableValue;
    4643import javafx.concurrent.Worker.State;
    47 import javafx.embed.swing.SwingFXUtils;
    4844import javafx.scene.SnapshotParameters;
    4945import javafx.scene.image.WritableImage;
     
    5147import javafx.scene.web.WebView;
    5248
    53 import javax.imageio.ImageIO;
    54 
    5549import netscape.javascript.JSObject;
    5650
    57 import org.expeditee.gui.DisplayIO;
     51import org.expeditee.core.Colour;
     52import org.expeditee.core.Font;
     53import org.expeditee.core.Image;
     54import org.expeditee.core.InOutReference;
     55import org.expeditee.gio.EcosystemManager;
     56import org.expeditee.gio.gesture.StandardGestureActions;
     57import org.expeditee.gio.swing.SwingMiscManager;
     58import org.expeditee.gui.DisplayController;
    5859import org.expeditee.gui.Frame;
    59 import org.expeditee.gui.FrameGraphics;
    6060import org.expeditee.gui.FrameIO;
    61 import org.expeditee.gui.FrameMouseActions;
    6261import org.expeditee.gui.FrameUtils;
    6362import org.expeditee.gui.MessageBay;
     
    303302                try {
    304303                       
    305                         final int verticalScrollPerPage = (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.85);
    306                         final int horizontalScrollPerPage = (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.85);
     304                        final int verticalScrollPerPage = (int) (DisplayController.getFramePaintArea().getHeight() * 0.85);
     305                        final int horizontalScrollPerPage = (int) (DisplayController.getFramePaintArea().getWidth() * 0.85);
    307306                                               
    308307                        Platform.runLater(new Runnable() {
     
    320319                        final Object notifier = new Object();
    321320                                               
    322                         final MutableInt verticalCount = new MutableInt(0);
    323                         final MutableInt horizontalCount = new MutableInt(0);
     321                        final InOutReference<Integer> verticalCount = new InOutReference<Integer>(0);
     322                        final InOutReference<Integer> horizontalCount = new InOutReference<Integer>(0);
    324323                       
    325                         final MutableInt pagesVertical = new MutableInt(1);
    326                         final MutableInt pagesHorizontal = new MutableInt(1);
     324                        final InOutReference<Integer> pagesVertical = new InOutReference<Integer>(1);
     325                        final InOutReference<Integer> pagesHorizontal = new InOutReference<Integer>(1);
    327326
    328327                        final String pageTitle;
     
    341340                        frameset.getTitleItem().setSize(14);
    342341
    343                         WebParser.addButton("Return to original frame", frame.getName(), null, 200, frameset, null, 0f, 10f, null);
    344 
    345                         Text link = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(), pageTitle, null);
     342                        WebParser.addButton("Return to original frame", frame.getName(), null, 200, frameset, null, 0, 10, null);
     343
     344                        Text link = DisplayController.getCurrentFrame().addText(DisplayController.getMouseX(), DisplayController.getMouseY(), pageTitle, null);
    346345                        link.setLink(frameset.getName());
    347346
    348                         FrameMouseActions.pickup(link);
     347                        StandardGestureActions.pickup(link);
    349348
    350349                        // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page,
     
    364363                                                this.stop();
    365364                                               
    366                                                 verticalCount.setValue(verticalCount.getValue() + 1);
     365                                                verticalCount.set(verticalCount.get() + 1);
    367366
    368367                                                frameToAddTo = FrameIO.CreateFrame(frameToAddTo.getFramesetName(), pageTitle, null);
     
    395394
    396395                                                // Getting a BufferedImage from the JavaFX image
    397                                                 BufferedImage image = SwingFXUtils.fromFXImage(img, null);
     396                                                //BufferedImage image = SwingFXUtils.fromFXImage(img, null);
     397                                                Image image = SwingMiscManager.getImageForJavaFXImage(img);
    398398
    399399                                                try {
    400                                                         int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null));
    401 
     400                                                        // TODO: tidy. cts16
     401                                                        //int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null));
     402                                                        int hashcode = image.hashCode();
     403                                                       
    402404                                                        File out = new File(FrameIO.IMAGES_PATH + "webpage-" + Integer.toHexString(hashcode) + ".png");
    403405                                                        out.mkdirs();
    404                                                         ImageIO.write(image, "png", out);
     406                                                        image.writeToDisk("png", out);
    405407
    406408                                                        // Adding the image to the frame
     
    408410                                                       
    409411                                                        // Adding thumbnail to the overview page
    410                                                         Text thumb = frameset.addText((int) (thumbWidth * 1.1 * horizontalCount.getValue()) + 10,
    411                                                                         (int) ((((float) thumbWidth / image.getWidth()) * image.getHeight()) * 1.1 * verticalCount.getValue()),
     412                                                        Text thumb = frameset.addText((int) (thumbWidth * 1.1 * horizontalCount.get()) + 10,
     413                                                                        (int) ((((float) thumbWidth / image.getWidth()) * image.getHeight()) * 1.1 * verticalCount.get()),
    412414                                                                        "@i: " + out.getName() + " " + thumbWidth,
    413415                                                                        null);
    414416                                                       
    415417                                                        thumb.setLink(frameToAddTo.getName());
    416                                                         thumb.setBorderColor(Color.lightGray);
     418                                                        thumb.setBorderColor(Colour.LIGHT_GREY);
    417419                                                        thumb.setThickness(1);
    418420
    419421                                                        // Button to go to the next frame/page
    420                                                         WebParser.addButton("Next", null, "next", 70, frameToAddTo, null, 0f, 10f, null);
     422                                                        WebParser.addButton("Next", null, "next", 70, frameToAddTo, null, 0, 10, null);
    421423
    422424                                                        // Button to go to the previous frame/page
    423                                                         if (verticalCount.getValue() > 1 || horizontalCount.getValue() > 0) {
    424                                                                 WebParser.addButton("Previous", null, "previous", 70, frameToAddTo, null, 85f, 10f, null);
     425                                                        if (verticalCount.get() > 1 || horizontalCount.get() > 0) {
     426                                                                WebParser.addButton("Previous", null, "previous", 70, frameToAddTo, null, 85, 10, null);
    425427                                                        }
    426428
    427429                                                        // Button to return to the index/overview page
    428                                                         WebParser.addButton("Index", frameset.getName(), null, 70, frameToAddTo, null, null, 10f, 5f);
     430                                                        WebParser.addButton("Index", frameset.getName(), null, 70, frameToAddTo, null, null, 10, 5);
    429431
    430432                                                        FrameIO.SaveFrame(frameToAddTo);
     
    435437                                                }
    436438
    437                                                 image.flush();
     439                                                image.releaseImage();
    438440
    439441                                                synchronized (notifier) {
     
    468470                                                // but if the webpage fits in a single converted page, there's no need for any overlap, so just use 1 as the number of pages
    469471                                                if((Boolean) webEngine.executeScript("document.documentElement.scrollHeight > window.innerHeight")) {
    470                                                         pagesVertical.setValue((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollHeight") / (float) verticalScrollPerPage));
     472                                                        pagesVertical.set((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollHeight") / (float) verticalScrollPerPage));
    471473                                                }
    472474                                               
    473475                                                if((Boolean) webEngine.executeScript("document.documentElement.scrollWidth > window.innerWidth")) {
    474                                                         pagesHorizontal.setValue((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollWidth") / (float) horizontalScrollPerPage));
     476                                                        pagesHorizontal.set((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollWidth") / (float) horizontalScrollPerPage));
    475477                                                }
    476478                                               
    477479                                                System.out.println(webEngine.executeScript("document.documentElement.scrollWidth") + "/" + horizontalScrollPerPage);
    478                                                 System.out.println(pagesVertical.getValue() + "x" + pagesHorizontal.getValue());
     480                                                System.out.println(pagesVertical.get() + "x" + pagesHorizontal.get());
    479481                                               
    480482                                                // Setting up the element that contains the CSS to hide all text. Also hiding readability mode buttons.
     
    628630
    629631                        // Loop that scrolls the page horizontally
    630                         for(int i = 0; i < pagesHorizontal.getValue() && browserWidget.isParserRunning(); i++) {
     632                        for(int i = 0; i < pagesHorizontal.get() && browserWidget.isParserRunning(); i++) {
    631633                                                               
    632634                                Platform.runLater(new Runnable() {
     
    647649                               
    648650                                // Loop that scrolls the page vertically (for each horizontal scroll position)
    649                                 for(int j = 0; j < pagesVertical.getValue()  && browserWidget.isParserRunning(); j++) {
     651                                for(int j = 0; j < pagesVertical.get()  && browserWidget.isParserRunning(); j++) {
    650652                                       
    651653                                        try {
    652                                                 progressBar.set((int) (50 + ((float)(j+1)/(pagesVertical.getValue() * pagesHorizontal.getValue())  + ((float)(i) / pagesHorizontal.getValue())) * 50));
     654                                                progressBar.set((int) (50 + ((float)(j+1)/(pagesVertical.get() * pagesHorizontal.get())  + ((float)(i) / pagesHorizontal.get())) * 50));
    653655                                        } catch (Exception e) {
    654656                                                e.printStackTrace();
     
    698700                                }
    699701
    700                                 horizontalCount.setValue(horizontalCount.getValue() + 1);
    701                                 verticalCount.setValue(0);
     702                                horizontalCount.set(horizontalCount.get() + 1);
     703                                verticalCount.set(0);
    702704                        }
    703705
     
    742744         * @return A Color object that should match the rgb string passed int. Returns null if alpha is 0
    743745         */
    744         private static Color rgbStringToColor(String rgbString) {
     746        private static Colour rgbStringToColor(String rgbString) {
    745747
    746748                if (rgbString == null) {
     
    764766
    765767                if (components[3] > 0) {
    766                         return new Color(components[0], components[1], components[2], components[3]);
     768                        return Colour.FromRGBA255(components[0], components[1], components[2], components[3]);
    767769                } else {
    768770                        return null;
     
    854856
    855857                                        Boolean fontFound = false;
    856                                         Font font = new Font(null);
     858                                        Font font = null;
    857859
    858860                                        // Looping through all font-families listed in the element's CSS until one that is installed is
     
    869871                                               
    870872                                                // Regex will remove any inverted commas surrounding multi-word typeface names
    871                                                 font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12);
    872                                                
    873                                                 // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found
    874                                                 if (!(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) {
     873                                                String familyName = typefaces[j].replaceAll("^'|'$", "");
     874                                                font = new Font(familyName);
     875                                                font.setStyle(Font.Style.PLAIN);
     876                                                font.setSize(12);
     877                                               
     878                                                // Check whether the font was found
     879                                                if (EcosystemManager.getFontManager().getActualFont(font).getFamilyName().toLowerCase().equals(familyName.toLowerCase())) {
    875880                                                        fontFound = true;
    876881                                                }
    877882                                        }
    878883
    879                                         if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) {
    880                                                 font = new Font("Times New Roman", Font.PLAIN, 12);
     884                                        if (!fontFound) {
     885                                                font = new Font("Times New Roman");
     886                                                font.setStyle(Font.Style.PLAIN);
     887                                                font.setSize(12);
    881888                                        }
    882889
     
    975982                                        String bgColorString = (String) style.call("getPropertyValue", new Object[] { "background-color" });
    976983
    977                                         Color bgColor = rgbStringToColor(bgColorString);
     984                                        Colour bgColor = rgbStringToColor(bgColorString);
    978985
    979986                                        // If the element has a background color then add it (to Expeditee) as a rectangle with that background color
     
    10771084                HttpURLConnection connection = (HttpURLConnection) (imgUrl.openConnection());
    10781085
    1079                 // Spoofing a widely accepted User Agent, since some sites refuse to serve non-webbrowser clients
    1080                 connection.setRequestProperty("User-Agent", "Mozilla/5.0");
    1081 
    1082                 BufferedImage img = ImageIO.read(connection.getInputStream());
    1083 
    1084                 int hashcode = Arrays.hashCode(img.getData().getPixels(0, 0, img.getWidth(), img.getHeight(), (int[]) null));
     1086                Image img = Image.getImage(connection);
     1087               
     1088                int hashcode = img.hashCode();
     1089               
    10851090                File out = new File(FrameIO.IMAGES_PATH + Integer.toHexString(hashcode) + ".png");
    10861091                out.mkdirs();
    1087                 ImageIO.write(img, "png", out);
     1092                img.writeToDisk("png", out);
    10881093
    10891094                if (repeat == null && cropEndX == null && cropStartX == null && cropEndY == null && cropStartY == null) {
     
    11531158                text.setPosition(x, y);
    11541159
    1155                 Picture pic = ItemUtils.CreatePicture(text, frame);
     1160                Picture pic = ItemUtils.CreatePicture(text);
    11561161               
    11571162                float invScale = 1 / pic.getScale();
     
    13241329
    13251330                                Boolean fontFound = false;
    1326                                 Font font = new Font(null);
     1331                                Font font = null;
    13271332
    13281333                                // Looping through all font-families listed in the element's CSS until one that is installed is
     
    13421347
    13431348                                        // Regex will remove any inverted commas surrounding multi-word typeface names
    1344                                         font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12);
     1349                                        String familyName = typefaces[j].replaceAll("^'|'$", "");
     1350                                        font = new Font(familyName);
     1351                                        font.setStyle(Font.Style.PLAIN);
     1352                                        font.setSize(12);
    13451353
    13461354                                        // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found
    1347                                         if (!(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) {
     1355                                        if (EcosystemManager.getFontManager().getActualFont(font).getFamilyName().toLowerCase().equals(familyName.toLowerCase())) {
    13481356                                                fontFound = true;
    13491357                                        }
    13501358                                }
    13511359
    1352                                 if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) {
    1353                                         font = new Font("Times New Roman", Font.PLAIN, 12);
     1360                                if (!fontFound) {
     1361                                        font = new Font("Times New Roman");
     1362                                        font.setStyle(Font.Style.PLAIN);
     1363                                        font.setSize(12);
    13541364                                }
    13551365
     
    14641474         * @param anchorLeft
    14651475         */
    1466         private static void addButton(String text, String link, String action, int width, Frame toAddTo, Float anchorTop, Float anchorRight, Float anchorBottom, Float anchorLeft) {
     1476        private static void addButton(String text, String link, String action, int width, Frame toAddTo, Integer anchorTop, Integer anchorRight, Integer anchorBottom, Integer anchorLeft) {
    14671477                // Button to go to the next frame/page
    14681478                Text button = new Text(text);
     
    14701480                button.setLink(link);
    14711481                button.addAction(action);
    1472                 button.setBorderColor(new Color(0.7f, 0.7f, 0.7f));
    1473                 button.setBackgroundColor(new Color(0.9f, 0.9f, 0.9f));
     1482                button.setBorderColor(new Colour(0.7f, 0.7f, 0.7f));
     1483                button.setBackgroundColor(new Colour(0.9f, 0.9f, 0.9f));
    14741484                button.setThickness(1);
    14751485                button.setLinkMark(false);
     
    14991509
    15001510        }
    1501 
    1502 
    1503         private static class MutableBool {
    1504                 private boolean value;
    1505 
    1506                 public MutableBool(boolean value) {
    1507                         this.value = value;
    1508                 }
    1509 
    1510                 public boolean getValue() {
    1511                         return value;
    1512                 }
    1513 
    1514                 public void setValue(boolean value) {
    1515                         this.value = value;
    1516                 }
    1517         }
    1518 
    1519         private static class MutableInt {
    1520                 private int value;
    1521 
    1522                 public MutableInt(int value) {
    1523                         this.value = value;
    1524                 }
    1525 
    1526                 public int getValue() {
    1527                         return value;
    1528                 }
    1529 
    1530                 public void setValue(int value) {
    1531                         this.value = value;
    1532                 }
    1533         }
    15341511}
Note: See TracChangeset for help on using the changeset viewer.