Changeset 733


Ignore:
Timestamp:
01/21/14 17:16:40 (10 years ago)
Author:
ngw8
Message:

Started work on v2 of webparser, currently adds snapshot of page on linked frames

Location:
trunk/src/org/expeditee
Files:
4 edited

Legend:

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

    r713 r733  
    33import java.awt.Color;
    44import java.awt.Font;
    5 import java.awt.FontMetrics;
     5import java.awt.Graphics;
    66import java.awt.image.BufferedImage;
    77import java.io.File;
     
    1414
    1515import javax.imageio.ImageIO;
     16import javax.swing.JComponent;
    1617
    1718import org.expeditee.gui.Frame;
     19import org.expeditee.gui.FrameCreator;
    1820import org.expeditee.gui.FrameIO;
    1921import org.expeditee.gui.FrameUtils;
     
    2527import org.expeditee.items.Text;
    2628import org.expeditee.reflection.JavaFX;
    27 import org.w3c.dom.Element;
    2829import org.w3c.dom.Node;
    2930import org.w3c.dom.html.HTMLBodyElement;
     
    214215                                                                        + "currentNode.parentElement.removeChild(currentNode);"
    215216                                                                       
    216                                                                         + "for (var i = 0; i < words.length; i++) {" + "                addToSpan(words[i]);"
     217                                                                        + "for (var i = 0; i < words.length; i++) {"
     218                                                                        + "             addToSpan(words[i]);"
    217219                                                                        + "}"
    218220                                                                       
     
    221223                                                                        + "             par.removeChild(prevSpan);"
    222224                                                                        + "}"
    223 );
     225                                                                        );
    224226
    225227                                                        // Will never reach 100% here, as the processing is not quite finished - progress is set to 100% at the end of
     
    261263                }
    262264    }
     265
     266        /**
     267         * Converts a loaded page to Expeditee frame(s)
     268         *
     269         * @param webEngine
     270         *            The JavaFX WebEngine in which the page to be converted is loaded
     271         * @param frame
     272         *            The Expeditee frame to output the converted page to
     273         */
     274        public static void parsePageSimple(final Object webEngine, final Object webView, final JComponent jfxPanel, final Frame frame) {
     275                try {
     276                        final Object notifier = new Object();
     277                        final MutableBool notAtBottom = new MutableBool(true);
     278
     279                        JavaFX.PlatformRunLater.invoke(null, new Runnable() {
     280                                @Override
     281                                public void run() {
     282                                        try {
     283                                                // Initializing the counter used when scrolling the page
     284                                                JavaFX.WebEngineExecuteScript.invoke(webEngine, "var scrollCounter = 0");
     285                                        } catch (Exception ex) {
     286                                                ex.printStackTrace();
     287                                        }
     288
     289                                        synchronized (notifier) {
     290                                                notifier.notify();
     291                                        }
     292                                }
     293                        });
     294
     295                        synchronized (notifier) {
     296                                try {
     297                                        // Waiting for the JavaFX thread to finish
     298                                        notifier.wait();
     299                                } catch (InterruptedException e) {
     300                                        // TODO Auto-generated catch block
     301                                        e.printStackTrace();
     302                                }
     303                        }
     304
     305                                int tick = 0;
     306                                Frame frameToAddTo = frame;
     307
     308                        while (notAtBottom.getValue()) {
     309                                JavaFX.PlatformRunLater.invoke(null, new Runnable() {
     310                                        @Override
     311                                        public void run() {
     312                                                try {
     313                                                        // Scrolling down the page
     314                                                        JavaFX.WebEngineExecuteScript.invoke(webEngine, ""
     315                                                                        + "window.scrollTo(0, scrollCounter * window.innerHeight);"
     316                                                                        + "scrollCounter = scrollCounter+1;");
     317                       
     318                                                        notAtBottom.setValue((boolean) JavaFX.WebEngineExecuteScript.invoke(webEngine, "(window.pageYOffset + window.innerHeight < document.documentElement.scrollHeight)"));
     319
     320                                                        synchronized (notifier) {
     321                                                                notifier.notify();
     322                                                        }
     323
     324                                                } catch (Exception e) {
     325                                                        e.printStackTrace();
     326                                                }
     327                                        }
     328                                });
     329
     330                                synchronized (notifier) {
     331                                        try {
     332                                                // Waiting for the JavaFX thread to finish
     333                                                notifier.wait();
     334                                        } catch (InterruptedException e) {
     335                                                // TODO Auto-generated catch block
     336                                                e.printStackTrace();
     337                                        }
     338                                }
     339
     340                                BufferedImage image = new BufferedImage(jfxPanel.getWidth(), jfxPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
     341
     342                                Graphics graphics = image.createGraphics();
     343
     344                                // Drawing the JfxPanel (containing the webview) to the image
     345                                jfxPanel.paint(graphics);
     346
     347                                try {
     348                                        int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null));
     349
     350                                        File out = new File(FrameIO.IMAGES_PATH + "webpage-" + Integer.toHexString(hashcode) + ".png");
     351                                        out.mkdirs();
     352                                        ImageIO.write(image, "png", out);
     353
     354                                        Text link = new Text("Next");
     355                                        link.setPosition(500, 20);
     356                                        frameToAddTo.addItem(link);
     357
     358                                        frameToAddTo = FrameIO.CreateFrame(frame.getFramesetName(), Integer.toHexString(hashcode), null);
     359
     360                                        link.setLink(frameToAddTo.getName());
     361
     362                                        // Adding the image
     363                                        frameToAddTo.addText(0, 0, "@i: " + out.getName(), null);
     364
     365                                        // Button to go to the next page
     366                                        Text nextButton = (Text) FrameCreator.createButton("Next", null, null, 10F, 10F);
     367                                        nextButton.setID(frameToAddTo.getNextItemID());
     368                                        nextButton.addAction("next");
     369                                        frameToAddTo.addItem(nextButton);
     370
     371                                        FrameIO.SaveFrame(frameToAddTo);
     372
     373                                } catch (IOException e) {
     374                                        e.printStackTrace();
     375                                }
     376                                graphics.dispose();
     377                                image.flush();
     378
     379                                System.out.println("C");
     380
     381                                synchronized (notifier) {
     382                                        notifier.notify();
     383                                }
     384
     385                                tick = 0;
     386
     387                        }
     388
     389                } catch (Exception ex) {
     390                        ex.printStackTrace();
     391                }
     392
     393
     394
     395        }
    263396
    264397        /**
     
    755888                pic.getSource().anchor();
    756889        }
     890
     891        private static class MutableBool {
     892                private boolean value;
     893
     894                public MutableBool(boolean value) {
     895                        this.value = value;
     896                }
     897
     898                public boolean getValue() {
     899                        return value;
     900                }
     901
     902                public void setValue(boolean value) {
     903                        this.value = value;
     904                }
     905        }
    757906}
  • trunk/src/org/expeditee/items/widgets/InteractiveWidget.java

    r725 r733  
    11211121         * @param parent
    11221122         */
    1123         private void layout(Component parent) {
     1123        protected void layout(Component parent) {
    11241124
    11251125                parent.validate();
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r655 r733  
    2020
    2121import javax.swing.JButton;
     22import javax.swing.JComponent;
    2223import javax.swing.JLabel;
    2324import javax.swing.JPanel;
     
    9293                                toolBar.setFloatable(false);
    9394
    94 
    9595                                JButton backButton = new JButton("Back");
    9696                                toolBar.add(backButton);
     
    105105                                toolBar.add(convertButton);
    106106
     107                                JButton convertButtonNew = new JButton("Convert (New Method)");
     108                                toolBar.add(convertButtonNew);
     109
    107110                                backButton.addActionListener(new ActionListener() {
    108111                                        @Override
     
    123126                                        public void actionPerformed(ActionEvent arg0) {
    124127                                                owner.getFrame();
     128                                        }
     129                                });
     130
     131                                convertButtonNew.addActionListener(new ActionListener() {
     132                                        @Override
     133                                        public void actionPerformed(ActionEvent arg0) {
     134                                                owner.getFrameNew();
    125135                                        }
    126136                                });
     
    408418                // Initial page is either the page stored in the arguments (if there is one stored) or the homepage
    409419                super(source, new WebBrowserPanel((args != null && args.length > 0) ? args[0] : NetworkSettings.HomePage.get()), -1, 500, -1, -1, 300, -1);
     420
    410421                _browser = (WebBrowserPanel) _swingComponent;
    411422                _browser.owner = this;
     
    555566        }
    556567
     568        public void getFrameNew() {
     569                // this.setSize(1000, 100000);
     570                // this._browser.setBounds(getX(), getY(), getWidth(), getHeight());
     571
     572                // this.layout(this._browser);
     573                try {
     574                        WebParser.parsePageSimple(JavaFX.WebViewGetEngine.invoke(this._browser.webview), this._browser.webview, (JComponent) this._browser.jfxPanel, DisplayIO.getCurrentFrame());
     575                } catch (Exception e) {
     576                        e.printStackTrace();
     577                }
     578        }
     579
    557580        /**
    558581         * Used to drop text items onto JfxBrowser widget. Does nothing if a text item is not attached to cursor. <br>
  • trunk/src/org/expeditee/reflection/JavaFX.java

    r628 r733  
    2626        public static Method WebViewGetEngine;
    2727        public static Method WebViewSetContextMenuEnabled;
     28        public static Method WebViewSnapshot;
     29
     30        public static Class<?> SnapshotParameters;
     31        public static Class<?> WritableImage;
     32
     33        public static Class<?> Image;
     34
     35        public static Class<?> SwingFXUtils;
     36        public static Method SwingFXUtilsFromFXImage;
    2837
    2938        public static Class<?> WebEngine;
     
    95104                        SceneConstructor = Scene.getConstructor(classLoader.loadClass("javafx.scene.Parent"));
    96105
     106                        SnapshotParameters = classLoader.loadClass("javafx.scene.SnapshotParameters");
     107                        WritableImage = classLoader.loadClass("javafx.scene.image.WritableImage");
     108
    97109                        WebView = classLoader.loadClass("javafx.scene.web.WebView");
    98110                        JFXPanelSetScene = JFXPanel.getMethod("setScene", Scene);
     
    100112                        WebViewGetEngine = WebView.getMethod("getEngine");
    101113                        WebViewSetContextMenuEnabled = WebView.getMethod("setContextMenuEnabled", boolean.class);
     114                        WebViewSnapshot = WebView.getMethod("snapshot", SnapshotParameters, WritableImage);
     115
     116                        Image = classLoader.loadClass("javafx.scene.image.Image");
     117
     118                        SwingFXUtils = classLoader.loadClass("javafx.embed.swing.SwingFXUtils");
     119                        SwingFXUtilsFromFXImage = SwingFXUtils.getMethod("fromFXImage", Image, java.awt.image.BufferedImage.class);
    102120
    103121                        WebEngine = classLoader.loadClass("javafx.scene.web.WebEngine");
Note: See TracChangeset for help on using the changeset viewer.