Search in sources :

Example 6 with WebEngine

use of javafx.scene.web.WebEngine in project intellij-community by JetBrains.

the class IpnbJfxUtils method createHtmlPanel.

public static JComponent createHtmlPanel(@NotNull final String source, int width) {
    final JFXPanel javafxPanel = new JFXPanel() {

        @Override
        protected void processMouseWheelEvent(MouseWheelEvent e) {
            final Container parent = getParent();
            final MouseEvent parentEvent = SwingUtilities.convertMouseEvent(this, e, parent);
            parent.dispatchEvent(parentEvent);
        }
    };
    javafxPanel.setBackground(IpnbEditorUtil.getBackground());
    Platform.runLater(() -> {
        final WebView webView = new WebView();
        webView.setContextMenuEnabled(false);
        webView.setOnDragDetected(event -> {
        });
        final WebEngine engine = webView.getEngine();
        initHyperlinkListener(engine);
        final boolean hasMath = source.contains("$");
        if (hasMath) {
            engine.setOnStatusChanged(event -> adjustHeight(webView, javafxPanel, source));
        } else {
            engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue == Worker.State.SUCCEEDED) {
                    adjustHeight(webView, javafxPanel, source);
                }
            });
        }
        final BorderPane pane = new BorderPane(webView);
        final String prefix;
        if (hasMath) {
            prefix = String.format(ourMathJaxPrefix, width - 500, EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
        } else {
            prefix = String.format(ourPrefix, width - 500);
        }
        final String content = prefix + convertToHtml(source) + ourPostfix;
        engine.loadContent(content);
        final Scene scene = new Scene(pane, 0, 0);
        javafxPanel.setScene(scene);
        updateLaf(LafManager.getInstance().getCurrentLookAndFeel() instanceof DarculaLookAndFeelInfo, engine, javafxPanel);
    });
    return javafxPanel;
}
Also used : JFXPanel(javafx.embed.swing.JFXPanel) BorderPane(javafx.scene.layout.BorderPane) MouseEvent(java.awt.event.MouseEvent) MouseWheelEvent(java.awt.event.MouseWheelEvent) DarculaLookAndFeelInfo(com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo) WebView(javafx.scene.web.WebView) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine)

Example 7 with WebEngine

use of javafx.scene.web.WebEngine in project Gargoyle by callakrsos.

the class FxUtil method openBrowser.

public static WebView openBrowser(Node parent, String content, boolean isLink) {
    WebView view = new WebView();
    WebEngine engine = view.getEngine();
    engine.setOnError(err -> {
        String message = err.getMessage();
        DialogUtil.showMessageDialog(FxUtil.getWindow(parent), message);
    });
    view.setOnKeyPressed(key -> {
        if (key.getCode() == KeyCode.F12) {
            FxUtil.createStageAndShow("Simple Web Console", new WebViewConsole(view));
        }
    });
    engine.setJavaScriptEnabled(true);
    engine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {

        @Override
        public WebEngine call(PopupFeatures p) {
            // Stage stage = new Stage();
            // WebView wv2 = new WebView();
            //
            // wv2.getEngine().setJavaScriptEnabled(true);
            //
            // stage.setScene(new Scene(wv2, BROWSER_WIDTH,
            // BROWSER_HEIGHT));
            // stage.initOwner(parent == null ? (Window) null :
            // parent.getScene().getWindow());
            // stage.show();
            WebView openBrowser = openBrowser(view, "", false);
            return openBrowser.getEngine();
        }
    });
    engine.setOnAlert(ev -> {
        DialogUtil.showMessageDialog(ev.getData());
    });
    engine.setConfirmHandler(new Callback<String, Boolean>() {

        @Override
        public Boolean call(String param) {
            Optional<Pair<String, String>> showYesOrNoDialog = DialogUtil.showYesOrNoDialog("Confirm.", param);
            if (showYesOrNoDialog.isPresent()) {
                Pair<String, String> pair = showYesOrNoDialog.get();
                if (pair == null)
                    return false;
                return "Y".equals(pair.getValue());
            }
            return false;
        }
    });
    engine.setOnAlert((WebEvent<String> wEvent) -> {
        System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });
    if (isLink)
        engine.load(content);
    else
        engine.loadContent(content);
    BorderPane root = new BorderPane(view);
    TextField txtLink = new TextField(content);
    txtLink.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
        if (KeyCode.ENTER == ev.getCode())
            engine.load(txtLink.getText());
    });
    root.setTop(txtLink);
    engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {

        @Override
        public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
            // if (newValue == State.SUCCEEDED) {
            String location = engine.getLocation();
            txtLink.setText(location);
        // }
        }
    });
    FxUtil.createStageAndShow(new Scene(root, BROWSER_WIDTH, BROWSER_HEIGHT), stage -> {
        stage.initOwner(parent == null ? (Window) null : parent.getScene().getWindow());
    });
    return view;
}
Also used : Window(javafx.stage.Window) BorderPane(javafx.scene.layout.BorderPane) Optional(java.util.Optional) PopupFeatures(javafx.scene.web.PopupFeatures) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine) State(javafx.concurrent.Worker.State) TextField(javafx.scene.control.TextField) WebView(javafx.scene.web.WebView) WebEvent(javafx.scene.web.WebEvent) WebViewConsole(com.kyj.fx.voeditor.visual.component.console.WebViewConsole) Pair(javafx.util.Pair)

Example 8 with WebEngine

use of javafx.scene.web.WebEngine in project Gargoyle by callakrsos.

the class MimeViewerExam method start.

@Override
public void start(Stage primaryStage) throws Exception {
    WebView center = new WebView();
    WebEngine engine = center.getEngine();
    URL resource = MimeViewerExam.class.getResource("Sample.html");
    // String externalForm = resource.toExternalForm();
    String readToString = FileUtil.readToString(resource.openStream());
    System.out.println("##########################################");
    System.out.println("mime string");
    System.out.println(readToString);
    System.out.println("##########################################");
    final MessageBuilder builder = new DefaultMessageBuilder();
    final Message message = builder.parseMessage(resource.openStream());
    Body body = ((Entity) message).getBody();
    StringBuilder sb = new StringBuilder();
    extracted(sb, body);
    Document parse = Jsoup.parse(sb.toString());
    Elements select = parse.select("img");
    select.forEach(e -> {
        String attr = e.attr("src");
        if (attr.startsWith("cid:")) {
            String cid = attr.replace("cid:", "");
            String string = meta.get(cid);
            e.attr("src", "data:image/jpg;base64,".concat(string));
        }
    });
    System.out.println("##########################################");
    System.out.println("html string");
    System.out.println(sb.toString());
    System.out.println("##########################################");
    System.out.println(parse.toString());
    engine.loadContent(sb.toString(), "text/html");
    primaryStage.setScene(new Scene(new BorderPane(center)));
    primaryStage.show();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) BorderPane(javafx.scene.layout.BorderPane) Message(org.apache.james.mime4j.dom.Message) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine) URL(java.net.URL) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) WebView(javafx.scene.web.WebView) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody) BinaryBody(org.apache.james.mime4j.dom.BinaryBody)

Example 9 with WebEngine

use of javafx.scene.web.WebEngine in project Gargoyle by callakrsos.

the class HtmlEditorExam method start.

@Override
public void start(Stage stage) {
    stage.setTitle("HTMLEditor Sample");
    stage.setWidth(500);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());
    VBox root = new VBox();
    root.setPadding(new Insets(8, 8, 8, 8));
    root.setSpacing(5);
    root.setAlignment(Pos.BOTTOM_LEFT);
    final HTMLEditor htmlEditor = new HTMLEditor();
    htmlEditor.setPrefHeight(245);
    htmlEditor.setHtmlText(INITIAL_TEXT);
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.getStyleClass().add("noborder-scroll-pane");
    scrollPane.setStyle("-fx-background-color: white");
    scrollPane.setContent(browser);
    scrollPane.setFitToWidth(true);
    scrollPane.setPrefHeight(180);
    Button showHTMLButton = new Button("Load Content in Browser");
    root.setAlignment(Pos.CENTER);
    showHTMLButton.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0) {
            webEngine.loadContent(htmlEditor.getHtmlText());
        }
    });
    root.getChildren().addAll(htmlEditor, showHTMLButton, scrollPane);
    scene.setRoot(root);
    stage.setScene(scene);
    stage.show();
}
Also used : Group(javafx.scene.Group) Insets(javafx.geometry.Insets) Button(javafx.scene.control.Button) ScrollPane(javafx.scene.control.ScrollPane) ActionEvent(javafx.event.ActionEvent) HTMLEditor(javafx.scene.web.HTMLEditor) Scene(javafx.scene.Scene) WebView(javafx.scene.web.WebView) VBox(javafx.scene.layout.VBox) WebEngine(javafx.scene.web.WebEngine)

Aggregations

WebEngine (javafx.scene.web.WebEngine)9 Scene (javafx.scene.Scene)7 WebView (javafx.scene.web.WebView)7 BorderPane (javafx.scene.layout.BorderPane)4 ObservableValue (javafx.beans.value.ObservableValue)2 State (javafx.concurrent.Worker.State)2 JFXPanel (javafx.embed.swing.JFXPanel)2 Button (javafx.scene.control.Button)2 VBox (javafx.scene.layout.VBox)2 PopupFeatures (javafx.scene.web.PopupFeatures)2 WebEvent (javafx.scene.web.WebEvent)2 DarculaLookAndFeelInfo (com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo)1 WebViewConsole (com.kyj.fx.voeditor.visual.component.console.WebViewConsole)1 LoginListener (com.pokegoapi.api.listener.LoginListener)1 CaptchaSolveHelper (com.pokegoapi.util.CaptchaSolveHelper)1 WebPage (com.sun.webkit.WebPage)1 WCSize (com.sun.webkit.graphics.WCSize)1 MouseEvent (java.awt.event.MouseEvent)1 MouseWheelEvent (java.awt.event.MouseWheelEvent)1 WindowAdapter (java.awt.event.WindowAdapter)1