Example usage for javafx.concurrent Task getException

List of usage examples for javafx.concurrent Task getException

Introduction

In this page you can find the example usage for javafx.concurrent Task getException.

Prototype

@Override
    public final Throwable getException() 

Source Link

Usage

From source file:poe.trade.assist.Main.java

public void manualTaskRun(Search search) {
    String url = search.getUrl();
    if (isNotBlank(url)) {
        Task<Search> task = new Task<Search>() {
            @Override/*from   w  w  w .  jav a  2 s  . c  om*/
            protected Search call() throws Exception {
                String html = AutoSearchService.doDownload(search.getUrl(), search.getSort());
                search.setHtml(html);
                search.parseHtml();
                return search;
            }
        };
        resultPane.progressIndicator.visibleProperty().unbind();
        resultPane.progressIndicator.visibleProperty().bind(task.runningProperty());
        task.setOnSucceeded(e -> {
            resultPane.setSearch(task.getValue());
            //            refreshResultColumn();
        });
        task.setOnFailed(e -> {
            Dialogs.showError(task.getException());
            //            refreshResultColumn();
        });
        new Thread(task).start();
    } else {
        resultPane.setSearch(search);
    }
}

From source file:snpviewer.SnpViewer.java

public void saveRegion(final String chromosome, final double startCoordinate, final double endCoordinate) {
    final Task<RegionSummary> saveSelectionTask = new Task<RegionSummary>() {
        @Override/*ww w .jav  a2 s  . co  m*/
        protected RegionSummary call() throws Exception {
            try {
                updateProgress(-1, -1);
                updateTitle("Finding flanking SNPs");
                updateMessage("Searching for nearest SNP in all files...");

                /* read SnpFiles to find closest SNPs - use binary search
                 * to find nearby SNP and refine to closest
                 */
                List<SnpFile.SnpLine> startAndEndSnps = searchCoordinate(chromosome, (int) startCoordinate,
                        (int) endCoordinate);
                if (startAndEndSnps == null) {
                    System.out.println("Start and End SNPS ARE NULL!");
                    //DISPLAY ERROR HERE?
                    return null;
                }
                RegionSummary region = new RegionSummary(chromosome, startAndEndSnps.get(0).getPosition(),
                        startAndEndSnps.get(1).getPosition(), 0, 0, startAndEndSnps.get(0).getId(),
                        startAndEndSnps.get(1).getId());
                return region;

            } catch (NumberFormatException ex) {
                Dialogs.showErrorDialog(null,
                        "Can't display flanking SNP IDs"
                                + " - missing required componant!\n\nPlease report this error.",
                        "Error!", "SNP Viewer", ex);
            }
            return null;
        }
    };
    setProgressMode(true);
    progressBar.progressProperty().bind(saveSelectionTask.progressProperty());
    progressMessage.textProperty().unbind();
    progressMessage.textProperty().bind(saveSelectionTask.messageProperty());
    progressTitle.textProperty().unbind();
    progressTitle.textProperty().bind(saveSelectionTask.titleProperty());
    saveSelectionTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            RegionSummary result = (RegionSummary) e.getSource().getValue();
            savedRegions.add(result);
            RegionSummary sorter = new RegionSummary();
            sorter.mergeRegionsByPosition(savedRegions);
            saveProject();
            clearDragSelectRectangle();
            savedRegionsDisplay.clear();
            savedRegionsReference.clear();
            drawSavedRegions(
                    (String) chromosomeBoxList[chromosomeSelector.getSelectionModel().getSelectedIndex()]);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
        }

    });
    saveSelectionTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
            Dialogs.showErrorDialog(null, "Error finding flanking SNPs\n", "Save Region error", "SNP Viewer",
                    saveSelectionTask.getException());

        }

    });
    saveSelectionTask.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            progressMessage.setText("Region write cancelled");
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
            Dialogs.showErrorDialog(null, "User cancelled region save.", "Save Region", "SNP Viewer",
                    saveSelectionTask.getException());
        }

    });
    cancelButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            saveSelectionTask.cancel();

        }
    });
    new Thread(saveSelectionTask).start();
}

From source file:snpviewer.SnpViewer.java

public void displayFlankingSnpIDs(final String chrom, final double start, final double end) {
    final Task<List<String>> displayTask = new Task<List<String>>() {
        @Override/*from  ww w .  j a v  a2  s .com*/
        protected List<String> call() {
            updateProgress(-1, -1);
            updateTitle("Finding flanking SNPs");
            updateMessage("Searching for nearest SNP in all files...");
            //work out coordinates based on chromosome and pane sizes
            /* read SnpFiles to find closest SNPs - use binary search
            * to find nearby SNP and refine to closest
            */
            List<SnpFile.SnpLine> startAndEndSnps = searchCoordinate(chrom, (int) start, (int) end);
            if (startAndEndSnps == null) {
                //DISPLAY ERROR HERE?
                return null;
            }
            String coordResult = "chr" + chrom + ":" + nf.format(startAndEndSnps.get(0).getPosition()) + "-"
                    + nf.format(startAndEndSnps.get(1).getPosition());
            String idResult = startAndEndSnps.get(0).getId() + ";" + startAndEndSnps.get(1).getId();
            List<String> result = new ArrayList();
            result.add(coordResult);
            result.add(idResult);
            return result;
        }
    };

    setProgressMode(true);
    progressBar.progressProperty().bind(displayTask.progressProperty());
    progressMessage.textProperty().unbind();
    progressMessage.textProperty().bind(displayTask.messageProperty());
    progressTitle.textProperty().unbind();
    progressTitle.textProperty().bind(displayTask.titleProperty());
    displayTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
        }

    });
    displayTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
            Dialogs.showErrorDialog(null, "Error displaying flanking SNPs\n", "Display error", "SNP Viewer",
                    displayTask.getException());

        }

    });
    displayTask.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            progressMessage.setText("Display flanking SNPs cancelled");
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressTitle.textProperty().unbind();
            progressMessage.textProperty().unbind();
            progressTitle.setText("");
            progressMessage.setText("");
            Dialogs.showErrorDialog(null, "User cancelled display.", "Display error", "SNP Viewer",
                    displayTask.getException());
        }

    });
    cancelButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            displayTask.cancel();

        }
    });
    new Thread(displayTask).start();
    try {
        List<String> result = displayTask.get();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("RegionReporter.fxml"));
        Stage stage = new Stage();
        Pane page = (Pane) loader.load();
        Scene scene = new Scene(page);
        stage.setScene(scene);
        stage.setTitle("SNP Viewer Region Summary");
        stage.getIcons().add(new Image(this.getClass().getResourceAsStream("icon.png")));
        RegionReporterController regionReporter = loader.<RegionReporterController>getController();
        if (result == null) {
            regionReporter.setCoordinates("Error!");
            regionReporter.setIds("Error!");
        } else {
            regionReporter.setCoordinates(result.get(0));
            regionReporter.setIds(result.get(1));
        }
        scene.getStylesheets().add(SnpViewer.class.getResource("SnpViewerStyleSheet.css").toExternalForm());
        stage.setResizable(false);
        stage.initModality(Modality.NONE);

        stage.show();
    } catch (InterruptedException | ExecutionException | IOException ex) {
        Dialogs.showErrorDialog(null,
                "Can't display flanking SNP IDs" + " - exception caught!\n\nPlease report this error.",
                "Error!", "SNP Viewer", ex);
    }

}

From source file:utilitybasedfx.MainGUIController.java

private void enableWorking(Task task) {
    btnImportSource.setDisable(true);// w  ww .j  a v a 2  s  .c o  m
    btnImportCompiled.setDisable(true);
    btnCompileFromSource.setDisable(true);
    btnGenerate.setDisable(true);

    lblWorking.setVisible(true);
    barWorking.setVisible(true);
    lblWorkingTask.setVisible(true);

    lblWorkingTask.textProperty().bind(task.messageProperty());
    barWorking.progressProperty().bind(task.progressProperty());

    task.stateProperty().addListener(new ChangeListener<Worker.State>() {
        @Override
        public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState,
                Worker.State newState) {
            if (newState == Worker.State.SUCCEEDED) {
                disableWorking();
            } else if (newState == Worker.State.FAILED) {
                disableWorking();

                //Utils.stackErrorDialog((Exception)task.getException());
                task.getException().printStackTrace();

                Alert alert = new Alert(AlertType.ERROR);
                alert.setTitle("There was an error");
                alert.setHeaderText("There was some kind of error while trying to do the current task");
                alert.setContentText(task.getException().toString());

                alert.showAndWait();
            }
        }
    });

    new Thread(task).start();
}