javafx.concurrent.Service Java Examples

The following examples show how to use javafx.concurrent.Service. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML public Service<Void> handleLoginButtonAction(ActionEvent event){
    // Button for google
    Button googleAuth = new Button(Configuration.getBundle().getString("ui.dialog.auth.google.title"), IconFactory.createGoogleIcon());
    LoginDialog dialog = new LoginDialog(googleAuth);
    googleAuth.setOnAction(t -> {
        GoogleLoginDialog googleDialog = new GoogleLoginDialog(dialog);
        googleDialog.show();
    });
    Optional<Pair<String, String>> result = dialog.showAndWait();

    hBottomBox.add(labelField, 0, 0);
    LoginService loginTask = new LoginService();
    result.ifPresent(usernamePassword -> {
        loginTask.setUsername(usernamePassword.getKey());
        loginTask.setPassword(usernamePassword.getValue());
    });
    labelField.textProperty().bind(loginTask.messageProperty());
    return loginTask;
}
 
Example #2
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleDownloadButtonAction(ActionEvent event){
    if(! MainApp.getZdsutils().isAuthenticated()){
        Service<Void> loginTask = handleLoginButtonAction(event);
        loginTask.setOnSucceeded(t -> downloadContents(null));
        loginTask.setOnCancelled(t -> {
            hBottomBox.getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.auth.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.auth.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.auth.failed.text"));

            alert.showAndWait();
        });

        loginTask.start();
    }else{
        downloadContents(null);
    }
}
 
Example #3
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleDownloadArticleButtonAction(ActionEvent event){
    if(! MainApp.getZdsutils().isAuthenticated()){
        Service<Void> loginTask = handleLoginButtonAction(event);
        loginTask.setOnSucceeded(t -> downloadContents("ARTICLE"));
        loginTask.setOnCancelled(t -> {
            hBottomBox.getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.auth.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.auth.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.auth.failed.text"));

            alert.showAndWait();
        });

        loginTask.start();
    }else{
        downloadContents("ARTICLE");
    }
}
 
Example #4
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleDownloadTutorialButtonAction(ActionEvent event){
    if(! MainApp.getZdsutils().isAuthenticated()){
        Service<Void> loginTask = handleLoginButtonAction(event);
        loginTask.setOnSucceeded(t -> downloadContents("TUTORIAL"));
        loginTask.setOnCancelled(t -> {
            hBottomBox.getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.auth.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.auth.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.auth.failed.text"));

            alert.showAndWait();
        });

        loginTask.start();
    }else{
        downloadContents("TUTORIAL");
    }
}
 
Example #5
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleDownloadOpinionButtonAction(ActionEvent event){
    if(! MainApp.getZdsutils().isAuthenticated()){
        Service<Void> loginTask = handleLoginButtonAction(event);
        loginTask.setOnSucceeded(t -> downloadContents("OPINION"));
        loginTask.setOnCancelled(t -> {
            hBottomBox.getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.auth.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.auth.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.auth.failed.text"));

            alert.showAndWait();
        });

        loginTask.start();
    }else{
        downloadContents("OPINION");
    }
}
 
Example #6
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleUploadButtonAction(ActionEvent event){
    if(! MainApp.getZdsutils().isAuthenticated()){
        Service<Void> loginTask = handleLoginButtonAction(event);
        loginTask.setOnCancelled(t -> {
            hBottomBox.getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.upload.content.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.upload.content.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.upload.content.failed.text"));

            alert.showAndWait();
        });
        loginTask.setOnSucceeded(t -> uploadContents());
        loginTask.start();
    }else{
        uploadContents();
    }
}
 
Example #7
Source File: ImageInputDialog.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
@FXML private void handleSelectFileAction(){
    if(! zdsUtils.isAuthenticated()){
        Service<Void> loginTask = menuManager.handleLoginButtonAction(null);
        loginTask.setOnSucceeded(t -> selectAndUploadImage());
        loginTask.setOnCancelled(t -> {
            menuManager.getHBottomBox().getChildren().clear();
            Alert alert = new CustomAlert(AlertType.ERROR);
            alert.setTitle(Configuration.getBundle().getString("ui.dialog.auth.failed.title"));
            alert.setHeaderText(Configuration.getBundle().getString("ui.dialog.auth.failed.header"));
            alert.setContentText(Configuration.getBundle().getString("ui.dialog.auth.failed.text"));

            alert.showAndWait();
        });
        loginTask.start();
    }else{
        selectAndUploadImage();
    }
}
 
Example #8
Source File: SearchController.java    From Noexes with GNU General Public License v3.0 5 votes vote down vote up
private void initService(Service<?> service) {
    DoubleProperty p = mc.getProgressBar().progressProperty();
    p.unbind();
    p.bind(service.progressProperty());
    service.messageProperty().addListener((observable, oldValue, newValue) -> mc.setStatus(newValue));
    this.runningService = service;
    runningService.restart();
}
 
Example #9
Source File: Minimal.java    From JavaFX with MIT License 5 votes vote down vote up
private void nextPane(Service<Rectangle> recBuilder) {
	loading[counter].textProperty().bind(recBuilder.messageProperty());
	indicators[counter].visibleProperty().bind(
			recBuilder.progressProperty().isNotEqualTo(
					new SimpleDoubleProperty(
							ProgressBar.INDETERMINATE_PROGRESS)));
	recBuilder.restart();
}
 
Example #10
Source File: MdConvertController.java    From zest-writer with GNU General Public License v3.0 4 votes vote down vote up
private void initRenderTask() {
    renderTask = new Service<String>() {
        @Override
        protected Task<String> createTask() {
            return new Task<String>() {
                @Override
                protected String call() throws Exception {
                    String html = getMdBox().markdownToHtml(sourceText.getText());
                    if (html != null) {
                        return MainApp.getMdUtils().addHeaderAndFooter(html);
                    } else {
                        throw new IOException();
                    }
                }
            };
        }
    };

    renderTask.setOnFailed(t -> {
        renderTask.restart();
    });

    renderTask.setOnSucceeded(t -> {
        Platform.runLater(() -> {
            yRenderPosition = getVScrollValue(renderView);
            xRenderPosition = getHScrollValue(renderView);
            renderView.getEngine().loadContent(renderTask.getValue());
            performStats();
            renderTask.reset();
            if(needRefresh.getValue()) {
                needRefresh.set(false);
                renderTask.start();
            }
        });
    });
    renderView.getEngine().getLoadWorker().stateProperty()
        .addListener((ObservableValue<? extends State> ov, State oldState, State newState) -> {
            if (newState == State.SUCCEEDED) {
                Platform.runLater(() -> scrollTo(renderView, xRenderPosition, yRenderPosition));
            }
        });
}
 
Example #11
Source File: ExportPdfService.java    From zest-writer with GNU General Public License v3.0 4 votes vote down vote up
public Service<Void> getThis() {
    return this;
}
 
Example #12
Source File: Minimal.java    From JavaFX with MIT License 4 votes vote down vote up
private void loadPanels(Group root) {
	// change to loadPanel:
	root.getChildren().set(0, createLoadPane());

	// Service:
	final Service<Rectangle> RecBuilder = new Service<Rectangle>() {
		@Override
		protected Task<Rectangle> createTask() {
			return new Task<Rectangle>() {
				@Override
				protected Rectangle call() throws InterruptedException {
					updateMessage("loading rectangle . . .");
					updateProgress(0, 10);
					for (int i = 0; i < 10; i++) {
						Thread.sleep(100);
					}
					updateMessage("Finish!");
					return new Rectangle((380) / 3, (380) / 3,
							colors[counter]);
				}
			};
		}
	};

	// StateListener
	RecBuilder.stateProperty().addListener(
			new ChangeListener<Worker.State>() {
				@Override
				public void changed(
						ObservableValue<? extends Worker.State> observableValue,
						Worker.State oldState, Worker.State newState) {
					switch (newState) {
					case SCHEDULED:
						break;
					case READY:
					case RUNNING:
						break;
					case SUCCEEDED:
						Rectangle rec = RecBuilder.valueProperty()
								.getValue();
						indicators[counter].progressProperty().unbind();
						loading[counter].textProperty().unbind();
						loadPane.getChildren().set(counter, rec);
						if (counter < 8) {
							counter++;
							nextPane(RecBuilder);
						}
						break;
					case CANCELLED:
					case FAILED:
						loading[counter].textProperty().unbind();
						loading[counter].setText("Failed!");
						if (counter < 8) {
							counter++;
							nextPane(RecBuilder);
						}
						break;
					}
				}
			});

	// begin PanelBuilding:
	nextPane(RecBuilder);
}
 
Example #13
Source File: ConfigManager.java    From TerasologyLauncher with Apache License 2.0 2 votes vote down vote up
/**
 * Provides a reader service that can be used to read
 * configurations from the local config file. It should
 * be used only from the JavaFX Application thread.
 *
 * @return the reader service
 */
public Service<Config> getReader() {
    return reader;
}
 
Example #14
Source File: ConfigManager.java    From TerasologyLauncher with Apache License 2.0 2 votes vote down vote up
/**
 * Provides a writer service that can be used to write
 * configurations to the local config file. It should
 * be used only from the JavaFX Application thread.
 *
 * @return the writer service
 */
public Service<Void> getWriter() {
    return writer;
}