Search in sources :

Example 1 with Worker

use of javafx.concurrent.Worker in project intellij-plugins by StepicOrg.

the class AuthDialog method makeProgressBarWithListener.

@NotNull
private ProgressBar makeProgressBarWithListener() {
    final ProgressBar progress = new ProgressBar();
    Worker<Void> loadWorker = engine.getLoadWorker();
    progress.progressProperty().bind(loadWorker.progressProperty());
    loadWorker.stateProperty().addListener(new ChangeListener<Worker.State>() {

        @Override
        public void changed(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) {
            if (newState == Worker.State.CANCELLED) {
                return;
            }
            if (newState == Worker.State.FAILED) {
                Map<String, Object> map = new HashMap<>();
                map.put("url", engine.getLocation());
                String content = Templater.processTemplate("error", map);
                engine.loadContent(content);
                return;
            }
            String location = engine.getLocation();
            if (location != null) {
                if (location.startsWith(Urls.STEPIK_URL + "/#")) {
                    String paramString = location.split("#")[1];
                    String[] params = paramString.split("&");
                    map.clear();
                    Arrays.stream(params).forEach(param -> {
                        String[] entry = param.split("=");
                        String value = "";
                        if (entry.length > 1) {
                            value = entry[1];
                        }
                        map.put(entry[0], value);
                    });
                    hide();
                    return;
                } else if ((Urls.STEPIK_URL + "/?error=access_denied").equals(location)) {
                    map.put("error", "access_denied");
                    hide();
                    return;
                }
            }
            progressBar.setVisible(newState == Worker.State.RUNNING);
            if (newState == Worker.State.SUCCEEDED) {
                AuthDialog.this.setTitle(engine.getTitle());
            }
        }

        private void hide() {
            loadWorker.cancel();
            setVisible(false);
        }
    });
    return progress;
}
Also used : Button(javafx.scene.control.Button) Worker(javafx.concurrent.Worker) Pos(javafx.geometry.Pos) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine) Arrays(java.util.Arrays) AllIcons(com.intellij.icons.AllIcons) HashMap(java.util.HashMap) JFXPanel(javafx.embed.swing.JFXPanel) CookieManager(org.stepik.plugin.auth.webkit.network.CookieManager) Insets(javafx.geometry.Insets) ProgressBar(javafx.scene.control.ProgressBar) CookieHandler(java.net.CookieHandler) Map(java.util.Map) Tooltip(javafx.scene.control.Tooltip) HBox(javafx.scene.layout.HBox) WebView(javafx.scene.web.WebView) WebHistory(javafx.scene.web.WebHistory) BufferedImage(java.awt.image.BufferedImage) Node(javafx.scene.Node) WritableImage(javafx.scene.image.WritableImage) Platform(javafx.application.Platform) java.awt(java.awt) Urls(org.stepik.api.urls.Urls) ImageView(javafx.scene.image.ImageView) SwingFXUtils(javafx.embed.swing.SwingFXUtils) ObservableValue(javafx.beans.value.ObservableValue) Templater(org.stepik.core.templates.Templater) BorderPane(javafx.scene.layout.BorderPane) StepikAuthManager(org.stepik.core.stepik.StepikAuthManager) ChangeListener(javafx.beans.value.ChangeListener) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) Worker(javafx.concurrent.Worker) ProgressBar(javafx.scene.control.ProgressBar) HashMap(java.util.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AllIcons (com.intellij.icons.AllIcons)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 CookieHandler (java.net.CookieHandler)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Platform (javafx.application.Platform)1 ChangeListener (javafx.beans.value.ChangeListener)1 ObservableValue (javafx.beans.value.ObservableValue)1 Worker (javafx.concurrent.Worker)1 JFXPanel (javafx.embed.swing.JFXPanel)1 SwingFXUtils (javafx.embed.swing.SwingFXUtils)1 Insets (javafx.geometry.Insets)1 Pos (javafx.geometry.Pos)1 Node (javafx.scene.Node)1 Scene (javafx.scene.Scene)1 Button (javafx.scene.control.Button)1 ProgressBar (javafx.scene.control.ProgressBar)1 Tooltip (javafx.scene.control.Tooltip)1