Changeset 564 for trunk/src/org/expeditee/items/widgets
- Timestamp:
- 12/03/13 17:03:37 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/JfxBrowser.java
r560 r564 4 4 import java.awt.Color; 5 5 import java.awt.Component; 6 import java.awt.Font; 6 7 import java.awt.event.ActionEvent; 7 8 import java.awt.event.ActionListener; … … 15 16 import java.awt.event.MouseListener; 16 17 import java.awt.event.MouseMotionListener; 18 import java.awt.font.TextAttribute; 17 19 import java.io.File; 18 20 import java.lang.reflect.Constructor; … … 37 39 import org.expeditee.gui.UserSettings; 38 40 import org.expeditee.items.Item; 41 import org.expeditee.items.Justification; 39 42 import org.expeditee.items.Text; 40 43 import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator; … … 44 47 import org.w3c.dom.NodeList; 45 48 import org.w3c.dom.css.CSSStyleDeclaration; 49 import org.w3c.dom.html.HTMLBodyElement; 50 51 import com.sun.org.apache.xerces.internal.dom.TextImpl; 46 52 47 53 /** … … 104 110 static Method JSObejctCall; 105 111 static Method JSObejctGetMember; 112 static Method JSObejctGetSlot; 106 113 107 114 static { … … 164 171 JSObejctCall = JSObject.getMethod("call", String.class, Object[].class); 165 172 JSObejctGetMember = JSObject.getMethod("getMember", String.class); 173 JSObejctGetSlot = JSObject.getMethod("getSlot", int.class); 166 174 167 175 } catch (Exception e) { … … 622 630 public void run() { 623 631 try { 624 Document doc = (Document) WebEngineExecuteScript.invoke(webEngine, "document"); 632 HTMLBodyElement doc = (HTMLBodyElement) WebEngineExecuteScript.invoke(webEngine, "document.body"); 633 625 634 Object window = WebEngineExecuteScript.invoke(webEngine, "window"); 626 635 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(); 641 656 642 657 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 } 656 709 } 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 710 663 711 } catch (Exception e) { 664 712 e.printStackTrace(); 665 713 } 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 // }672 714 } 673 715 }); … … 680 722 } 681 723 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 682 750 /** 683 751 * 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.