@Override public void handleEvent(Event event) { HTMLAnchorElement anchorElement = (HTMLAnchorElement) event.getCurrentTarget(); String href = anchorElement.getHref(); boolean done = false; try { if(Desktop.isDesktopSupported()) { openLinkInSystemBrowser(href); done = true; } else { LOGGER.warn("OS does not support desktop operations like browsing. Cannot open link '{}'.", href); } } catch(URISyntaxException | IOException e) { LOGGER.warn("OS does not support desktop operations like browsing. Failed open link '{}'.", href, e); } if(done) { event.preventDefault(); } }
@Override public void handleEvent(Event event) { HTMLAnchorElement anchorElement = (HTMLAnchorElement) event.getCurrentTarget(); String href = anchorElement.getHref(); if (Desktop.isDesktopSupported()) { openLinkInSystemBrowser(href); } else { LOGGER.warning("OS does not support desktop operations like browsing. Cannot open link '{" + href + "}'."); } event.preventDefault(); }
@Override public void handleEvent(Event event) { HTMLAnchorElement anchorElement = (HTMLAnchorElement)event.getCurrentTarget(); String href = anchorElement.getHref(); if (isDesktopSupported()) { openLinkInSystemBrowser(href); } else { Log.warn(APPLICATION, "OS does not support desktop operations like browsing. Cannot open link '"+href+"'!"); } event.preventDefault(); }
private static void redirectLinksToBrowser(WebEngine webEngine) { webEngine.getLoadWorker().stateProperty().addListener( (ov, oldState, newState) -> { // adjust link handling if (webEngine.getDocument() == null) return; NodeList nodeList = webEngine.getDocument().getElementsByTagName("a"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); EventTarget eventTarget = (EventTarget) node; eventTarget.addEventListener("click", evt -> { EventTarget target = evt.getCurrentTarget(); HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; String href = anchorElement.getHref(); //handle opening URL outside JavaFX WebView try { Desktop.getDesktop().browse(new URI(href)); } catch (IOException | URISyntaxException e) { e.printStackTrace(); } evt.preventDefault(); }, false); } }); }
@Override public void handleEvent(Event evt) { final EventTarget target = evt.getCurrentTarget(); final HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; final String href = anchorElement.getHref(); evt.preventDefault(); JavaFXUtils.openWebpage(href); }
@Override public void handleEvent(Event evt) { EventTarget target = evt.getCurrentTarget(); HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; String url = anchorElement.getHref(); // If the clicked link is downloadable // then we prevent the browser to follow the link // and instead we perform the download ourselves if (downloader.isDownloadable(url)) { downloader.download(url); evt.preventDefault(); } }
/** * Tests if is passed Element an element which may be followed. * * @param element Element to be tested. * @return True, if we can follow passed element. */ private static boolean isLinkableElement(Element element) { if (element instanceof HTMLAnchorElement) { return true; } else if (element instanceof HTMLAreaElement) { return true; } return false; }
/** * Returns target attribute from the linkable element. * * @param element Linkable element. * @return Target attribute. */ private static String getTargetFromElement(Element element) { String targetAttr = null; if (element instanceof HTMLAnchorElement) { targetAttr = ((HTMLAnchorElement)element).getTarget(); } else if (element instanceof HTMLAreaElement) { targetAttr = ((HTMLAreaElement)element).getTarget(); } return targetAttr; }
/** * Returns href attribute from the linkable element. * * @param element Linkable element. * @return Href attribute. */ private static String getHrefFromElement(Element element) { String targetAttr = null; if (element instanceof HTMLAnchorElement) { targetAttr = ((HTMLAnchorElement)element).getHref(); } else if (element instanceof HTMLAreaElement) { targetAttr = ((HTMLAreaElement)element).getHref(); } return targetAttr; }
@Override public void handleEvent(Event event) { HTMLAnchorElement anchorElement = (HTMLAnchorElement) event.getCurrentTarget(); String href = anchorElement.getHref(); try { JabRefDesktop.openBrowser(href); } catch (IOException e) { LOGGER.error("Problem opening browser", e); } event.preventDefault(); }
private static void initFX(final JFXPanel fxPanel) { final WebView webView = new WebView(); fxPanel.setScene(new Scene(webView)); // Obtain the webEngine to navigate final WebEngine webEngine = webView.getEngine(); webEngine.loadContent("Escapists Map Editor\n" + "Written by jselby\nhttp://redd.it/2wacp2\n\n" + "You don't have a map loaded currently - \nGo to File in the top left, and press a button there!\n" + "Loading..."); webEngine.load("http://escapists.jselby.net/welcome/"); webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { @Override public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) { if (newValue == Worker.State.SUCCEEDED) { final Document document = webEngine.getDocument(); NodeList nodeList = document.getElementsByTagName("a"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); EventTarget eventTarget = (EventTarget) node; eventTarget.addEventListener("click", new org.w3c.dom.events.EventListener() { @Override public void handleEvent(Event evt) { EventTarget target = evt.getCurrentTarget(); HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; String href = anchorElement.getHref(); Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { try { desktop.browse(new URL(href).toURI()); } catch (Exception e) { e.printStackTrace(); } } evt.preventDefault(); } }, false); } } } }); }