Ignore:
Timestamp:
12/04/13 15:18:00 (11 years ago)
Author:
ngw8
Message:

Moved JfxBrowser page parsing method into WebParser class, added some minor improvements/features to the parsing

File:
1 edited

Legend:

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

    r566 r568  
    44import java.awt.Color;
    55import java.awt.Component;
    6 import java.awt.Font;
    76import java.awt.event.ActionEvent;
    87import java.awt.event.ActionListener;
     
    1615import java.awt.event.MouseListener;
    1716import java.awt.event.MouseMotionListener;
    18 import java.awt.font.TextAttribute;
    19 import java.io.File;
    20 import java.lang.reflect.Constructor;
    2117import java.lang.reflect.InvocationTargetException;
    22 import java.lang.reflect.Method;
    23 import java.net.URL;
    24 import java.net.URLClassLoader;
    25 import java.util.Arrays;
    26 import java.util.List;
    2718
    2819import javax.swing.JButton;
     
    3223import javax.swing.text.BadLocationException;
    3324
    34 import org.apache.xerces.impl.xs.opti.NamedNodeMapImpl;
    3525import org.expeditee.gui.DisplayIO;
    3626import org.expeditee.gui.FrameMouseActions;
     
    3828import org.expeditee.gui.MessageBay;
    3929import org.expeditee.gui.UserSettings;
     30import org.expeditee.io.WebParser;
    4031import org.expeditee.items.Item;
    41 import org.expeditee.items.Justification;
    4232import org.expeditee.items.Text;
    4333import org.expeditee.reflection.JavaFX;
    44 import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
    45 import org.w3c.dom.Document;
    46 import org.w3c.dom.Element;
    47 import org.w3c.dom.Node;
    48 import org.w3c.dom.NodeList;
    49 import org.w3c.dom.css.CSSStyleDeclaration;
    50 import org.w3c.dom.html.HTMLBodyElement;
    51 
    52 import com.sun.org.apache.xerces.internal.dom.TextImpl;
    5334
    5435/**
     
    8566
    8667                /**
    87                  * @return A JPanel that either contains a JavaFX Webview or an empty panel if the JFX panel can't be
    88                  *         initialized
     68                 * @return A JPanel that either contains a JavaFX Webview or an empty panel if the JFX panel can't be initialized
    8969                 */
    9070                public WebBrowserPanel(final String url) {
     
    378358                String urlLower = url.toLowerCase();
    379359                // check if protocol is missing
    380                 if(!(urlLower.startsWith("http://") || url.startsWith("https://") || urlLower.startsWith("ftp://"))) {
     360                if (!(urlLower.startsWith("http://") || url.startsWith("https://") || urlLower.startsWith("ftp://") || urlLower.startsWith("file://"))) {
    381361                        // check if it's a search
    382362                        int firstSpace = url.indexOf(" ");
     
    511491        public void getFrame() {
    512492                try {
    513                         // Reference to the webEngine, to make it easier/possible to get to it inside the run()
    514                         final Object webEngine = JavaFX.WebViewGetEngine.invoke(this._browser.webview);
    515 
    516                         try {
    517                                 JavaFX.PlatformRunLater.invoke(null, new Runnable() {
    518                                         @Override
    519                                         public void run() {
    520                                                 try {
    521                                                         HTMLBodyElement doc = (HTMLBodyElement) JavaFX.WebEngineExecuteScript.invoke(webEngine, "document.body");
    522 
    523                                                         Object window = JavaFX.WebEngineExecuteScript.invoke(webEngine, "window");
    524                                                         NodeList nodes = doc.getElementsByTagName("*");
    525 
    526                                                         // Using Javascript to get an array of all the text nodes (i.e. text contents of every HTML tag)
    527                                                         Object textNodes = JavaFX.WebEngineExecuteScript.invoke(webEngine,
    528                                                                         "function getTextNodes(rootNode){"
    529                                                                                 + "var node;"
    530                                                                                 + "var textNodes=[];"
    531                                                                                 + "var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);"
    532                                                                                 + "while(node=walk.nextNode()) {"
    533                                                                                         + " textNodes.push(node);"
    534                                                                                 + "}"
    535                                                                                 + "return textNodes;"
    536                                                                         + "}; "
    537                                                                         + "getTextNodes(document.body)");
    538 
    539                                                         int textNodesLength = (Integer) JavaFX.JSObjectGetMember.invoke(textNodes, "length");
    540 
    541                                                         for (int i = 0; i < textNodesLength; i++) {
    542                                                                 Node textItem = (Node) JavaFX.JSObjectGetSlot.invoke(textNodes, i);
    543 
    544                                                                 Element e = (Element) textItem.getParentNode();
    545 
    546                                                                 Object style = JavaFX.JSObjectCall.invoke(window, "getComputedStyle", new Object[] { e });
    547 
    548                                                                 // Checking if the element is actually visible on the page
    549                                                                 if ((Integer) JavaFX.JSObjectGetMember.invoke(e, "offsetWidth") > 0 && (Integer) JavaFX.JSObjectGetMember.invoke(e, "offsetHeight") > 0
    550                                                                                 && !(((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "visibility" })).equals("hidden"))
    551                                                                                 && !(((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "display" })).equals("none"))) {
    552 
    553                                                                         // Getting a rectangle that represents the area and position of the element
    554                                                                         Object bounds = JavaFX.JSObjectCall.invoke(e, "getBoundingClientRect", new Object[] {});
    555 
    556                                                                         // Bounding rectangle position is relative to the current view, so scroll position must be added to x/y
    557                                                                         float x = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "left").toString())
    558                                                                                         + Float.valueOf(JavaFX.WebEngineExecuteScript.invoke(webEngine, "window.pageXOffset").toString());
    559                                                                         float y = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "top").toString())
    560                                                                                         + Float.valueOf(JavaFX.WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset").toString());
    561 
    562                                                                         Float width = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "width").toString());
    563 
    564                                                                         String fontSize = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "font-size" }));
    565                                                                         fontSize = fontSize.substring(0, fontSize.length() - 2);
    566                                                                         String color = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "color" });
    567                                                                         String bgColor = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-color" });
    568                                                                         String align = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "text-align" });
    569                                                                         String typeface = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "font-family" });
    570                                                                         typeface = typeface.split(",")[0].trim();
    571                                                                         String weight = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "font-weight" });
    572 
    573                                                                         Text t;
    574                                                                        
    575                                                                         t = DisplayIO.getCurrentFrame().createNewText(textItem.getTextContent().replaceAll(" +", " "));
    576                                                                        
    577                                                                         t.setPosition(x, y);
    578                                                                         t.setWidth(Math.round(width));
    579                                                                        
    580                                                                         t.setColor(rgbStringToColor(color));
    581 
    582                                                                         t.setBackgroundColor(rgbStringToColor(bgColor));
    583 
    584                                                                         t.setFont(Font.decode(typeface + " " + weight));
    585 
    586                                                                         t.setSize(Integer.valueOf(fontSize));
    587 
    588                                                                         if (align.equals("left")) {
    589                                                                                 t.setJustification(Justification.left);
    590                                                                         } else if (align.equals("right")) {
    591                                                                                 t.setJustification(Justification.right);
    592                                                                         } else if (align.equals("center")) {
    593                                                                                 t.setJustification(Justification.center);
    594                                                                         } else if (align.equals("justify")) {
    595                                                                                 t.setJustification(Justification.full);
    596                                                                         }
    597                                                                 }
    598                                                         }
    599 
    600                                                 } catch (Exception e) {
    601                                                         e.printStackTrace();
    602                                                 }
    603                                         }
    604                                 });
    605                         } catch (Exception e) {
    606                                 e.printStackTrace();
    607                         }
     493                        WebParser.parsePage(JavaFX.WebViewGetEngine.invoke(this._browser.webview), DisplayIO.getCurrentFrame());
    608494                } catch (Exception e) {
    609495                        e.printStackTrace();
    610                 }
    611         }
    612        
    613         /**
    614          * @param rgbString
    615          *            string in the format <i>rgb(x,x,x)</i> or <i>rgba(x,x,x,x)</i>
    616          * @return A Color object that should match the rgb string passed int. Returns null if alpha is 0
    617          */
    618         private Color rgbStringToColor(String rgbString) {
    619                 // Splitting the string into 'rgb' and 'x, x, x'
    620                 String[] tmpStrings = rgbString.split("\\(|\\)");
    621 
    622                 // Splitting up the RGB(A) components into an array
    623                 tmpStrings = tmpStrings[1].split(",");
    624 
    625                 int[] components = new int[4];
    626                 Arrays.fill(components, 255);
    627 
    628                 for (int i = 0; i < tmpStrings.length; i++) {
    629                         components[i] = Integer.valueOf(tmpStrings[i].trim());
    630                 }
    631 
    632                 if (components[3] > 0) {
    633                         return new Color(components[0], components[1], components[2], components[3]);
    634                 } else {
    635                         return null;
    636496                }
    637497        }
Note: See TracChangeset for help on using the changeset viewer.