Example usage for org.w3c.dom.html HTMLAnchorElement getHref

List of usage examples for org.w3c.dom.html HTMLAnchorElement getHref

Introduction

In this page you can find the example usage for org.w3c.dom.html HTMLAnchorElement getHref.

Prototype

public String getHref();

Source Link

Document

The URI of the linked resource.

Usage

From source file:org.pdfsam.ui.news.NewsStage.java

static void wrapHrefToOpenNative(Document document) {
    // TODO find a better way
    NodeList nodeList = document.getElementsByTagName("a");
    for (int i = 0; i < nodeList.getLength(); i++) {
        EventTarget eventTarget = (EventTarget) nodeList.item(i);
        eventTarget.addEventListener("click", e -> {
            HTMLAnchorElement anchorElement = (HTMLAnchorElement) e.getCurrentTarget();
            eventStudio().broadcast(new OpenUrlRequest(anchorElement.getHref()));
            e.preventDefault();//from  www  . j  a v  a2  s.c  om
        }, false);
    }
}

From source file:utybo.branchingstorytree.swing.visuals.NodePanel.java

public NodePanel(BranchingStory story, StoryPanel parent, final IMGClient imageClient) {
    OpenBSTGUI.getInstance().addDarkModeCallback(callback);
    callback.accept(OpenBSTGUI.getInstance().isDark());
    this.parent = parent;
    this.imageClient = imageClient;
    setLayout(new BorderLayout());

    JFXPanel panel = new JFXPanel();
    add(panel, BorderLayout.CENTER);

    CountDownLatch cdl = new CountDownLatch(1);
    Platform.runLater(() -> {//from  w  w w. j  a v  a2 s . c om
        try {
            view = new WebView();
            view.getEngine().setOnAlert(e -> SwingUtilities
                    .invokeLater(() -> Messagers.showMessage(OpenBSTGUI.getInstance(), e.getData())));
            view.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
                if (newState == State.SUCCEEDED) {
                    Document doc = view.getEngine().getDocument();
                    NodeList nl = doc.getElementsByTagName("a");
                    for (int i = 0; i < nl.getLength(); i++) {
                        Node n = nl.item(i);
                        HTMLAnchorElement a = (HTMLAnchorElement) n;
                        if (a.getHref() != null) {
                            ((EventTarget) a).addEventListener("click", ev -> {
                                if (!hrefEnabled) {
                                    ev.preventDefault();
                                }
                            }, false);
                        }
                    }
                }
            });
            Scene sc = new Scene(view);
            try {
                view.getEngine()
                        .loadContent(IOUtils
                                .toString(
                                        NodePanel.class.getResourceAsStream(
                                                "/utybo/branchingstorytree/swing/html/error.html"),
                                        StandardCharsets.UTF_8)
                                .replace("$MSG", Lang.get("story.problem")).replace("$STYLE", FONT_UBUNTU));
            } catch (IOException e) {
                OpenBST.LOG.warn("Error on trying to load error HTML file", e);
            }
            panel.setScene(sc);
        } finally {
            cdl.countDown();
        }
    });
    try {
        cdl.await();
    } catch (InterruptedException e) {
        OpenBST.LOG.warn("Synchronization failed", e);
    }
}