Example usage for javafx.concurrent Service cancel

List of usage examples for javafx.concurrent Service cancel

Introduction

In this page you can find the example usage for javafx.concurrent Service cancel.

Prototype

@Override
public boolean cancel() 

Source Link

Document

Cancels any currently running Task, if any.

Usage

From source file:fr.amap.lidar.amapvox.gui.MainFrameController.java

private void exportDartPlots(final File voxelFile) {

    if (Util.checkIfVoxelFile(voxelFile)) {

        fileChooserSaveDartFile.setInitialFileName("plots.xml");
        fileChooserSaveDartFile.setInitialDirectory(voxelFile.getParentFile());

        final File plotFile = fileChooserSaveDartFile.showSaveDialog(stage);

        if (plotFile != null) {

            Alert alert = new Alert(AlertType.CONFIRMATION);

            alert.setTitle("Coordinate system");
            alert.setContentText("Choose your coordinate system");

            ButtonType buttonTypeGlobal = new ButtonType("Global");
            ButtonType buttonTypeLocal = new ButtonType("Local");

            alert.getButtonTypes().setAll(buttonTypeGlobal, buttonTypeLocal);

            Optional<ButtonType> result = alert.showAndWait();

            final boolean global;

            if (result.get() == buttonTypeGlobal) {
                global = true;/*from  w  w w. j av  a2  s .c o m*/
            } else if (result.get() == buttonTypeLocal) {
                global = false;
            } else {
                return;
            }

            final DartPlotsXMLWriter dartPlotsXML = new DartPlotsXMLWriter();

            final Service service = new Service() {

                @Override
                protected Task createTask() {
                    return new Task<Object>() {

                        @Override
                        protected Object call() throws Exception {

                            dartPlotsXML.writeFromVoxelFile(voxelFile, plotFile, global);
                            return null;
                        }
                    };
                }
            };

            ProgressDialog progressDialog = new ProgressDialog(service);
            progressDialog.show();
            service.start();

            Button buttonCancel = new Button("cancel");
            progressDialog.setGraphic(buttonCancel);

            buttonCancel.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    service.cancel();
                    dartPlotsXML.setCancelled(true);
                }
            });

        }
    } else {
        logger.error("File is not a voxel file: " + voxelFile.getAbsolutePath());
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.setHeaderText("Incorrect file");
        alert.setContentText("File is corrupted or cannot be read!\n" + "Do you want to keep it?");

        Optional<ButtonType> result = alert.showAndWait();

        if (result.get() == ButtonType.CANCEL) {
            listViewProductsFiles.getItems().remove(voxelFile);
        }
    }

}