Ignore:
Timestamp:
12/02/13 14:51:52 (11 years ago)
Author:
ngw8
Message:

Progress on Web >> Expeditee - Text items representing all elements of a webpage can be added to a frame from the JfxBrowser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r557 r560  
    3030import javax.swing.text.BadLocationException;
    3131
     32import org.apache.xerces.impl.xs.opti.NamedNodeMapImpl;
    3233import org.expeditee.gui.DisplayIO;
    3334import org.expeditee.gui.FrameMouseActions;
     
    3738import org.expeditee.items.Item;
    3839import org.expeditee.items.Text;
     40import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
     41import org.w3c.dom.Document;
     42import org.w3c.dom.Element;
     43import org.w3c.dom.Node;
     44import org.w3c.dom.NodeList;
     45import org.w3c.dom.css.CSSStyleDeclaration;
    3946
    4047/**
     
    7582        static Method WebEngineGetHistory;
    7683        static Method WebEngineReload;
    77         //static Method WebEngineGetDocument;
     84        static Method WebEngineExecuteScript;
    7885
    7986        static Class WebHistory;
     
    9299        static Class State;
    93100        static List<Object> StateConstants;
     101
     102        static Class JSObject;
     103        static Method JSObejctEval;
     104        static Method JSObejctCall;
     105        static Method JSObejctGetMember;
    94106
    95107        static {
     
    130142                        WebEngineGetHistory = WebEngine.getMethod("getHistory");
    131143                        WebEngineReload = WebEngine.getMethod("reload");
     144                        WebEngineExecuteScript = WebEngine.getMethod("executeScript", String.class);
    132145                       
    133146                        WebHistory = classLoader.loadClass("javafx.scene.web.WebHistory");
     
    146159                        State = classLoader.loadClass("javafx.concurrent.Worker$State");
    147160                        StateConstants = Arrays.asList(State.getEnumConstants());
     161
     162                        JSObject = classLoader.loadClass("netscape.javascript.JSObject");
     163                        JSObejctEval = JSObject.getMethod("eval", String.class);
     164                        JSObejctCall = JSObject.getMethod("call", String.class, Object[].class);
     165                        JSObejctGetMember = JSObject.getMethod("getMember", String.class);
    148166
    149167                } catch (Exception e) {
     
    194212                                toolBar.add(urlField);
    195213
     214                                JButton convertButton = new JButton("Convert");
     215                                toolBar.add(convertButton);
     216
    196217                                backButton.addActionListener(new ActionListener() {
    197218                                        @Override
     
    205226                                        public void actionPerformed(ActionEvent arg0) {
    206227                                                owner.navigateForward();
     228                                        }
     229                                });
     230                               
     231                                convertButton.addActionListener(new ActionListener() {
     232                                        @Override
     233                                        public void actionPerformed(ActionEvent arg0) {
     234                                                owner.getFrame();
    207235                                        }
    208236                                });
     
    585613         */
    586614        public void getFrame() {
    587                 MessageBay.displayMessage("I haven't been implemented yet");
    588615                try {
     616                        // Reference to the webEngine, to make it easier/possible to get to it inside the run()
    589617                        final Object webEngine = WebViewGetEngine.invoke(this._browser.webview);
    590                        
     618
    591619                        try {
    592620                                PlatformRunLater.invoke(null, new Runnable() {
     
    594622                                        public void run() {
    595623                                                try {
    596                                                         // TODO: parse html to get expeditee frame
     624                                                        Document doc = (Document) WebEngineExecuteScript.invoke(webEngine, "document");
     625                                                        Object window = WebEngineExecuteScript.invoke(webEngine, "window");
     626                                                        NodeList nodes = doc.getElementsByTagName("*");
     627                                                        System.out.println(nodes.getLength());
     628                                                       
     629                                                        Element e;
     630                                                        Object bounds;
     631                                                        float x, y;
     632
     633                                                        for (int i = 0; i < nodes.getLength(); i++) {
     634                                                                e = (Element) nodes.item(i);
     635                                                                bounds = JSObejctCall.invoke(e, "getBoundingClientRect", new Object[] {});
     636
     637                                                                x = Float.valueOf(JSObejctGetMember.invoke(bounds, "left").toString()) + Float.valueOf(WebEngineExecuteScript.invoke(webEngine, "window.pageXOffset").toString());
     638                                                                y = Float.valueOf(JSObejctGetMember.invoke(bounds, "top").toString()) + Float.valueOf(WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset").toString());
     639
     640                                                                System.out.println(y + "    " + e.toString());
     641
     642                                                                Object style = JSObejctCall.invoke(window, "getComputedStyle", new Object[] { e });
     643                                                                String color = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "color" });
     644
     645                                                                color = color.substring(4, color.length() - 1);
     646
     647                                                                String[] colors = color.split(", ");
     648                                                               
     649
     650
     651                                                                Text t = DisplayIO.getCurrentFrame().createNewText("x");
     652                                                                t.setPosition(x, y);
     653
     654                                                                t.setColor(new Color(Integer.valueOf(colors[0]), Integer.valueOf(colors[1]), Integer.valueOf(colors[2])));
     655
     656                                                        }
     657
     658                                                        // Object img = JSObejctCall.invoke(logo, "getBoundingClientRect", new Object[] {});
     659                                                        // System.out.println((int) WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset")
     660                                                        // + (int) WebEngineExecuteScript.invoke(webEngine,
     661                                                        // "document.getElementById('hplogo').getBoundingClientRect().top"));
     662
    597663                                                } catch (Exception e) {
    598664                                                        e.printStackTrace();
    599665                                                }
     666                                               
     667//                                              while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
     668//                                               xl_ += elem.offsetLeft - elem.scrollLeft;
     669//                                               yt_ += elem.offsetTop - elem.scrollTop;
     670//                                               lem = elem.offsetParent;
     671//                                               }
    600672                                        }
    601673                                });
     
    609681       
    610682        /**
    611          * Used to drop text items onto JfxBrowser widget. Does nothing if a text item is not attached to cursor.
    612          *              "back"          -> navigates back a page in browser's session history
    613          *              "forward"       -> navigates forward a page in browser's session history
    614          *              "refresh"       -> reloads current page
    615          *              "getFrame"      -> attempts to parse page into an expeditee frame
    616          *               url            -> all other text is assumed to be a url which browser attempts to navigate to
    617          * return Whether a JfxBrowser specific event is run.
     683         * Used to drop text items onto JfxBrowser widget. Does nothing if a text item is not attached to cursor. <br>
     684         * "back" -> navigates back a page in browser's session history <br>
     685         * "forward" -> navigates forward a page in browser's session history <br>
     686         * "refresh" -> reloads current page <br>
     687         * "getFrame" -> attempts to parse page into an expeditee frame <br>
     688         * url -> all other text is assumed to be a url which browser attempts to navigate to
     689         *
     690         * @return Whether a JfxBrowser specific event is run.
     691         *
    618692         */
    619693        @Override
Note: See TracChangeset for help on using the changeset viewer.