Changeset 887


Ignore:
Timestamp:
02/13/14 22:59:29 (10 years ago)
Author:
ngw8
Message:

JFXBrowser - right clicking on a link attaches a text item to the cursor with an action to create a new frame containing a browser pointing to the linked page. Also works for linked images.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/JfxBrowserActions.java

    r876 r887  
    153153         * @throws Exception
    154154         */
    155         public static void createFrameWithBrowser(Text link, String url) throws Exception {
     155        public static void createFrameWithBrowser(Item link, String url) throws Exception {
    156156                Frame frame = FrameIO.CreateNewFrame(link);
    157157                frame.removeAllItems(frame.getItems());
     158
    158159                link.setLink("" + frame.getNumber());
    159160                link.setAction(null);
    160161
    161162                // Create widget via text annotation
    162                 Text wt = frame.addText(10, 10, "@iw: org.expeditee.items.widgets.JfxBrowser " + (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.9) + " "
     163                frame.addText(10, 10, "@iw: org.expeditee.items.widgets.JfxBrowser " + (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.9) + " "
    163164                                + (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.9) + " : " + url, null);
    164165
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r886 r887  
    543543                                                        JSObject window = (JSObject) JfxBrowser.this._webEngine.executeScript("window");
    544544                                                        Object o = JfxBrowser.this._webEngine.executeScript("document.elementFromPoint(" + e.getX() + "," + e.getY() + ");");
     545                                                       
    545546                                                        if(o instanceof org.w3c.dom.Node) {
    546547                                                                org.w3c.dom.Node node = (org.w3c.dom.Node) o;
    547548                                                                JSObject style = (JSObject) window.call("getComputedStyle", node);
     549                                                               
    548550                                                                if(node.getNodeName().toLowerCase().equals("img") ||
    549551                                                                                ((String) style.call("getPropertyValue", "background-image")).startsWith("url")) {
    550                                                                 try {
    551    
     552                                                               
     553                                                                        try {
    552554                                                                        JSObject bounds = (JSObject) ((JSObject) node).call("getBoundingClientRect", new Object[] {});
    553555                                                                        float width = Float.valueOf(bounds.getMember("width").toString());
     
    559561                                                                                pic = WebParser.getBackgroundImageFromNode(node, style, DisplayIO.getCurrentFrame(), null,
    560562                                                                                        (float) FrameMouseActions.getX(), (float) FrameMouseActions.getY(), width, height);
     563                                                                               
    561564                                                                        } else {
    562565                                                                                String imgSrc;
     
    569572                                                                                }
    570573                                                                        }
     574                                                                       
     575                                                                        String linkUrl;
     576                                                                       
     577                                                                        // Check the image and its immediate parent for links
     578                                                                        if ((node.getNodeName().toLowerCase().equals("a") && (linkUrl = (String) ((JSObject)node).getMember("href")) != null)
     579                                                                                        || (node.getParentNode().getNodeName().toLowerCase().equals("a") && (linkUrl = (String)((JSObject)node.getParentNode()).getMember("href")) != null)) {
     580                                                                               
     581                                                                                if(hasValidProtocol(linkUrl)) {
     582                                                                                        pic.getSource().setAction("createFrameWithBrowser "  + linkUrl);
     583                                                                                }
     584                                                                        }
     585                                                                       
    571586                                                                        pic.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
    572587                                                FrameMouseActions.pickup(pic);
     
    575590                                        e1.printStackTrace();
    576591                                    }
     592                                                                       
    577593                                                                } else if(node.getNodeName().toLowerCase().equals("video")) {
    578594                                                                        String src = ((JSObject)node).getMember("src").toString();
     
    601617                                                                        JfxMedia media = new JfxMedia(t, new String[] { src });
    602618                                                                        FrameMouseActions.pickup(media.getItems());
     619                                                                       
     620                                                                } else if(node.getNodeName().toLowerCase().equals("a") && ((JSObject)node).getMember("href") != null) {
     621                                                                        // If a link is right clicked, copy the text content and give it an action to create
     622                                                                        // a new frame containing a browser pointing to the linked page
     623                                                                        Text t = DisplayIO.getCurrentFrame().createNewText(((String) ((JSObject)node).getMember("textContent")).trim());
     624                                                                        t.addAction("createFrameWithBrowser " + (String) ((JSObject)node).getMember("href"));
     625                                                                        t.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
     626                                                                        FrameMouseActions.pickup(t);
    603627                                                                }
    604628                                                        }
    605629                                                } else {
    606630                                                        // Copy text and attach to cursor
    607                                                         Text t = new Text(selection);
    608                                                         t.setParent(DisplayIO.getCurrentFrame());
     631                                                        Text t = DisplayIO.getCurrentFrame().createNewText();                                                   
    609632                                                        t.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
    610633                                                        FrameMouseActions.pickup(t);
     
    622645        public void navigate(String url) {
    623646                final String actualURL;
    624                 String urlLower = url.toLowerCase();
     647
    625648                // check if protocol is missing
    626                 if (!(urlLower.startsWith("http://") || url.startsWith("https://") || urlLower.startsWith("ftp://") || urlLower.startsWith("file://"))) {
     649                if (!hasValidProtocol(url)) {
    627650                        // check if it's a search
    628651                        int firstSpace = url.indexOf(" ");
     
    973996                return stringBuilder.toString();
    974997        }
     998       
     999        /**
     1000         * Checks if a URL string starts with a protocol that can be loaded by the webview
     1001         * @param url URL string to check
     1002         * @return
     1003         */
     1004        private static boolean hasValidProtocol(String url) {
     1005                String urlLower = url.toLowerCase();
     1006               
     1007                // check if protocol is present
     1008                return (urlLower.startsWith("http://") || url.startsWith("https://") || urlLower.startsWith("ftp://") || urlLower.startsWith("file://"));
     1009        }
    9751010}
Note: See TracChangeset for help on using the changeset viewer.