Example usage for javafx.scene.web WebView WebView

List of usage examples for javafx.scene.web WebView WebView

Introduction

In this page you can find the example usage for javafx.scene.web WebView WebView.

Prototype

public WebView() 

Source Link

Document

Creates a WebView object.

Usage

From source file:com.offbynull.voip.ui.UiWebRegion.java

private UiWebRegion(Bus busFromGateway, Bus busToGateway) {
    Validate.notNull(busFromGateway);/*from   w ww.j av a2s  .  c om*/
    Validate.notNull(busToGateway);

    this.busFromGateway = busFromGateway;
    this.busToGateway = busToGateway;

    webView = new WebView();
    webEngine = webView.getEngine();

    webView.setContextMenuEnabled(false);
    webEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
        @Override
        public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
            System.out.println("Received exception: " + t1.getMessage());
        }
    });

    getChildren().add(webView);

    lock = new ReentrantLock();
}

From source file:de.bayern.gdi.gui.Controller.java

private void displayHTMLFileAsPopup(String popuptitle, String pathToFile) throws IOException {
    WebView web = new WebView();
    InputStream htmlPage = Misc.getResource(pathToFile);
    String content = IOUtils.toString(htmlPage, "UTF-8");
    web.getEngine().loadContent(content);
    WebViewWindow wvw = new WebViewWindow(web, popuptitle);
    wvw.popup();/*from  w  w  w. j a v  a  2 s  . c  o  m*/
}

From source file:com.vladsch.idea.multimarkdown.editor.MultiMarkdownFxPreviewEditor.java

/**
 * Build a new instance of {@link MultiMarkdownFxPreviewEditor}.
 *
 * @param project the {@link Project} containing the document
 * @param doc     the {@link Document} previewed in this editor.
 *//* w  ww.j av  a2  s  .c  om*/
public MultiMarkdownFxPreviewEditor(@NotNull final Project project, @NotNull Document doc, boolean isRawHtml) {
    this.isRawHtml = isRawHtml;
    this.document = doc;
    this.project = project;
    this.isWikiDocument = isWikiDocument(document);
    containingFile = FileDocumentManager.getInstance().getFile(document);
    resolver = containingFile == null ? null : new GitHubLinkResolver(containingFile, project);

    // Listen to the document modifications.
    this.document.addDocumentListener(new DocumentAdapter() {
        @Override
        public void documentChanged(DocumentEvent e) {
            delayedHtmlPreviewUpdate(false);
        }
    });

    // Listen to settings changes
    MultiMarkdownGlobalSettings.getInstance()
            .addListener(globalSettingsListener = new MultiMarkdownGlobalSettingsListener() {
                public void handleSettingsChanged(@NotNull final MultiMarkdownGlobalSettings newSettings) {
                    if (project.isDisposed())
                        return;
                    processor = null;
                    updateEditorTabIsVisible();
                    updateLinkRenderer();
                    delayedHtmlPreviewUpdate(true);
                    checkNotifyUser();
                }
            });

    MultiMarkdownProjectComponent projectComponent = MultiMarkdownPlugin.getProjectComponent(project);
    if (projectComponent != null) {
        projectComponent.addListener(projectFileListener = new ReferenceChangeListener() {
            @Override
            public void referenceChanged(@Nullable String name) {
                if (project.isDisposed())
                    return;
                delayedHtmlPreviewUpdate(false);
            }
        });
    }

    project.getMessageBus().connect(this).subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
        @Override
        public void enteredDumbMode() {
        }

        @Override
        public void exitDumbMode() {
            // need to re-evaluate class link accessibility
            if (project.isDisposed())
                return;
            delayedHtmlPreviewUpdate(false);
        }
    });

    updateLinkRenderer();

    if (isRawHtml) {
        jEditorPane = null;
        jfxPanel = null;
        webView = null;
        webEngine = null;
        Language language = Language.findLanguageByID("HTML");
        FileType fileType = language != null ? language.getAssociatedFileType() : null;
        Document myDocument = EditorFactory.getInstance().createDocument("");
        myTextViewer = (EditorImpl) EditorFactory.getInstance().createViewer(myDocument, project);
        if (fileType != null)
            myTextViewer.setHighlighter(
                    EditorHighlighterFactory.getInstance().createEditorHighlighter(project, fileType));
    } else {
        // Setup the editor pane for rendering HTML.
        myTextViewer = null;
        jEditorPane = new JPanel(new BorderLayout(), false);
        jfxPanel = new JFXPanel(); // initializing javafx
        jEditorPane.add(jfxPanel, BorderLayout.CENTER);
        Platform.setImplicitExit(false);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                if (project.isDisposed())
                    return;

                webView = new WebView();
                webEngine = webView.getEngine();

                anchorPane = new AnchorPane();
                AnchorPane.setTopAnchor(webView, 0.0);
                AnchorPane.setLeftAnchor(webView, 0.0);
                AnchorPane.setBottomAnchor(webView, 0.0);
                AnchorPane.setRightAnchor(webView, 0.0);
                anchorPane.getChildren().add(webView);
                jfxPanel.setScene(new Scene(anchorPane));

                webEngine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
                    @Override
                    public WebEngine call(PopupFeatures config) {
                        // return a web engine for the new browser window or null to block popups
                        return null;
                    }
                });

                addStateChangeListener();
            }
        });
    }

    checkNotifyUser();
}

From source file:org.wandora.application.gui.topicpanels.webview.WebViewPanel.java

private void initFX(final JFXPanel fxPanel) {
    Group group = new Group();
    Scene scene = new Scene(group);
    fxPanel.setScene(scene);//w  w  w  . java2s  .c  om

    webView = new WebView();

    if (javaFXVersionInt >= 8) {
        webView.setScaleX(1.0);
        webView.setScaleY(1.0);
        //webView.setFitToHeight(false);
        //webView.setFitToWidth(false);
        //webView.setZoom(javafx.stage.Screen.getPrimary().getDpi() / 96);
    }

    group.getChildren().add(webView);

    int w = this.getWidth();
    int h = this.getHeight() - 34;

    webView.setMinSize(w, h);
    webView.setMaxSize(w, h);
    webView.setPrefSize(w, h);

    // Obtain the webEngine to navigate
    webEngine = webView.getEngine();

    webEngine.locationProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue,
                final String newValue) {
            if (newValue.endsWith(".pdf")) {
                try {
                    int a = WandoraOptionPane.showConfirmDialog(Wandora.getWandora(),
                            "Open PDF document in external application?",
                            "Open PDF document in external application?", WandoraOptionPane.YES_NO_OPTION);
                    if (a == WandoraOptionPane.YES_OPTION) {
                        Desktop dt = Desktop.getDesktop();
                        dt.browse(new URI(newValue));
                    }
                } catch (Exception e) {
                }
            } else {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        urlTextField.setText(newValue);
                    }
                });
            }
        }
    });
    webEngine.titleProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue,
                final String newValue) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    title = newValue;
                }
            });
        }
    });
    webEngine.setOnAlert(new EventHandler<WebEvent<java.lang.String>>() {
        @Override
        public void handle(WebEvent<String> t) {
            if (t != null) {
                String str = t.getData();
                if (str != null && str.length() > 0) {
                    WandoraOptionPane.showMessageDialog(Wandora.getWandora(), str, "Javascript Alert",
                            WandoraOptionPane.PLAIN_MESSAGE);
                }
            }
        }
    });
    webEngine.setConfirmHandler(new Callback<String, Boolean>() {
        @Override
        public Boolean call(String msg) {
            int a = WandoraOptionPane.showConfirmDialog(Wandora.getWandora(), msg, "Javascript Alert",
                    WandoraOptionPane.YES_NO_OPTION);
            return (a == WandoraOptionPane.YES_OPTION);
        }
    });
    webEngine.setPromptHandler(new Callback<PromptData, String>() {
        @Override
        public String call(PromptData data) {
            String a = WandoraOptionPane.showInputDialog(Wandora.getWandora(), data.getMessage(),
                    data.getDefaultValue(), "Javascript Alert", WandoraOptionPane.QUESTION_MESSAGE);
            return a;
        }
    });

    webEngine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
        @Override
        public WebEngine call(PopupFeatures features) {
            if (informPopupBlocking) {
                WandoraOptionPane.showMessageDialog(Wandora.getWandora(),
                        "A javascript popup has been blocked. Wandora doesn't allow javascript popups in Webview topic panel.",
                        "Javascript popup blocked", WandoraOptionPane.PLAIN_MESSAGE);
            }
            informPopupBlocking = false;
            return null;
        }
    });
    webEngine.setOnVisibilityChanged(new EventHandler<WebEvent<Boolean>>() {
        @Override
        public void handle(WebEvent<Boolean> t) {
            if (t != null) {
                Boolean b = t.getData();
                if (informVisibilityChanges) {
                    WandoraOptionPane.showMessageDialog(Wandora.getWandora(),
                            "A browser window visibility change has been blocked. Wandora doesn't allow visibility changes of windows in Webview topic panel.",
                            "Javascript visibility chnage blocked", WandoraOptionPane.PLAIN_MESSAGE);
                    informVisibilityChanges = false;
                }
            }
        }
    });
    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        @Override
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SCHEDULED) {
                //System.out.println("Scheduled!");
                startLoadingAnimation();
            }
            if (newState == Worker.State.SUCCEEDED) {
                Document doc = webEngine.getDocument();
                try {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

                    // transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

                    StringWriter stringWriter = new StringWriter();
                    transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
                    webSource = stringWriter.toString();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                stopLoadingAnimation();
            } else if (newState == Worker.State.CANCELLED) {
                //System.out.println("Cancelled!");
                stopLoadingAnimation();
            } else if (newState == Worker.State.FAILED) {
                webEngine.loadContent(failedToOpenMessage);
                stopLoadingAnimation();
            }
        }
    });

}