Ignore:
Timestamp:
12/03/13 17:03:37 (11 years ago)
Author:
ngw8
Message:

Webpage to Expeditee working (to some extent) for text

File:
1 edited

Legend:

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

    r560 r564  
    44import java.awt.Color;
    55import java.awt.Component;
     6import java.awt.Font;
    67import java.awt.event.ActionEvent;
    78import java.awt.event.ActionListener;
     
    1516import java.awt.event.MouseListener;
    1617import java.awt.event.MouseMotionListener;
     18import java.awt.font.TextAttribute;
    1719import java.io.File;
    1820import java.lang.reflect.Constructor;
     
    3739import org.expeditee.gui.UserSettings;
    3840import org.expeditee.items.Item;
     41import org.expeditee.items.Justification;
    3942import org.expeditee.items.Text;
    4043import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
     
    4447import org.w3c.dom.NodeList;
    4548import org.w3c.dom.css.CSSStyleDeclaration;
     49import org.w3c.dom.html.HTMLBodyElement;
     50
     51import com.sun.org.apache.xerces.internal.dom.TextImpl;
    4652
    4753/**
     
    104110        static Method JSObejctCall;
    105111        static Method JSObejctGetMember;
     112        static Method JSObejctGetSlot;
    106113
    107114        static {
     
    164171                        JSObejctCall = JSObject.getMethod("call", String.class, Object[].class);
    165172                        JSObejctGetMember = JSObject.getMethod("getMember", String.class);
     173                        JSObejctGetSlot = JSObject.getMethod("getSlot", int.class);
    166174
    167175                } catch (Exception e) {
     
    622630                                        public void run() {
    623631                                                try {
    624                                                         Document doc = (Document) WebEngineExecuteScript.invoke(webEngine, "document");
     632                                                        HTMLBodyElement doc = (HTMLBodyElement) WebEngineExecuteScript.invoke(webEngine, "document.body");
     633
    625634                                                        Object window = WebEngineExecuteScript.invoke(webEngine, "window");
    626635                                                        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());
     636
     637                                                        // Using Javascript to get an array of all the text nodes (i.e. text contents of every HTML tag)
     638                                                        Object textNodes = WebEngineExecuteScript.invoke(webEngine,
     639                                                                        "function getTextNodes(rootNode){"
     640                                                                                + "var node;"
     641                                                                                + "var textNodes=[];"
     642                                                                                + "var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);"
     643                                                                                + "while(node=walk.nextNode()) {"
     644                                                                                        + " textNodes.push(node);"
     645                                                                                + "}"
     646                                                                                + "return textNodes;"
     647                                                                        + "}; "
     648                                                                        + "getTextNodes(document.body)");
     649
     650                                                        int textNodesLength = (int) JSObejctGetMember.invoke(textNodes, "length");
     651
     652                                                        for (int i = 0; i < textNodesLength; i++) {
     653                                                                Node textItem = (Node) JSObejctGetSlot.invoke(textNodes, i);
     654
     655                                                                Element e = (Element) textItem.getParentNode();
    641656
    642657                                                                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 
     658
     659                                                                // Checking if the element is actually visible on the page
     660                                                                if ((int) JSObejctGetMember.invoke(e, "offsetWidth") > 0 && (int) JSObejctGetMember.invoke(e, "offsetHeight") > 0
     661                                                                                && !(((String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "visibility" })).equals("hidden"))
     662                                                                                && !(((String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "display" })).equals("none"))) {
     663
     664                                                                        // Getting a rectangle that represents the area and position of the element
     665                                                                        Object bounds = JSObejctCall.invoke(e, "getBoundingClientRect", new Object[] {});
     666
     667                                                                        // Bounding rectangle position is relative to the current view, so scroll position must be added to x/y
     668                                                                        float x = Float.valueOf(JSObejctGetMember.invoke(bounds, "left").toString())
     669                                                                                        + Float.valueOf(WebEngineExecuteScript.invoke(webEngine, "window.pageXOffset").toString());
     670                                                                        float y = Float.valueOf(JSObejctGetMember.invoke(bounds, "top").toString())
     671                                                                                        + Float.valueOf(WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset").toString());
     672
     673                                                                        Float width = Float.valueOf(JSObejctGetMember.invoke(bounds, "width").toString());
     674
     675                                                                        String fontSize = ((String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "font-size" }));
     676                                                                        fontSize = fontSize.substring(0, fontSize.length() - 2);
     677                                                                        String color = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "color" });
     678                                                                        String bgColor = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "background-color" });
     679                                                                        String align = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "text-align" });
     680                                                                        String typeface = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "font-family" });
     681                                                                        typeface = typeface.split(",")[0].trim();
     682                                                                        String weight = (String) JSObejctCall.invoke(style, "getPropertyValue", new Object[] { "font-weight" });
     683
     684                                                                        Text t;
     685                                                                       
     686                                                                        t = DisplayIO.getCurrentFrame().createNewText(textItem.getTextContent().replaceAll(" +", " "));
     687                                                                       
     688                                                                        t.setPosition(x, y);
     689                                                                        t.setWidth(Math.round(width));
     690                                                                       
     691                                                                        t.setColor(rgbStringToColor(color));
     692
     693                                                                        t.setBackgroundColor(rgbStringToColor(bgColor));
     694
     695                                                                        t.setFont(Font.decode(typeface + " " + weight));
     696
     697                                                                        t.setSize(Integer.valueOf(fontSize));
     698
     699                                                                        if (align.equals("left")) {
     700                                                                                t.setJustification(Justification.left);
     701                                                                        } else if (align.equals("right")) {
     702                                                                                t.setJustification(Justification.right);
     703                                                                        } else if (align.equals("center")) {
     704                                                                                t.setJustification(Justification.center);
     705                                                                        } else if (align.equals("justify")) {
     706                                                                                t.setJustification(Justification.full);
     707                                                                        }
     708                                                                }
    656709                                                        }
    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"));
    662710
    663711                                                } catch (Exception e) {
    664712                                                        e.printStackTrace();
    665713                                                }
    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 //                                               }
    672714                                        }
    673715                                });
     
    680722        }
    681723       
     724        /**
     725         * @param rgbString
     726         *            string in the format <i>rgb(x,x,x)</i> or <i>rgba(x,x,x,x)</i>
     727         * @return A Color object that should match the rgb string passed int. Returns null if alpha is 0
     728         */
     729        private Color rgbStringToColor(String rgbString) {
     730                // Splitting the string into 'rgb' and 'x, x, x'
     731                String[] tmpStrings = rgbString.split("\\(|\\)");
     732
     733                // Splitting up the RGB(A) components into an array
     734                tmpStrings = tmpStrings[1].split(",");
     735
     736                int[] components = new int[4];
     737                Arrays.fill(components, 255);
     738
     739                for (int i = 0; i < tmpStrings.length; i++) {
     740                        components[i] = Integer.valueOf(tmpStrings[i].trim());
     741                }
     742
     743                if (components[3] > 0) {
     744                        return new Color(components[0], components[1], components[2], components[3]);
     745                } else {
     746                        return null;
     747                }
     748        }
     749
    682750        /**
    683751         * Used to drop text items onto JfxBrowser widget. Does nothing if a text item is not attached to cursor. <br>
Note: See TracChangeset for help on using the changeset viewer.