From e058e5f2fe773e87e1dd3264a25a145d6f087833 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 11 Feb 2021 16:57:35 +0100 Subject: [PATCH] replace TimerTask by Timeline in FlashSceneController and remove TImerTask class --- .../subcontrollers/FlashSceneController.java | 39 +++++------ .../ecorp/easy/installer/tasks/TimerTask.java | 69 ------------------- 2 files changed, 17 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/ecorp/easy/installer/tasks/TimerTask.java diff --git a/src/main/java/ecorp/easy/installer/controllers/subcontrollers/FlashSceneController.java b/src/main/java/ecorp/easy/installer/controllers/subcontrollers/FlashSceneController.java index b4e28ea5..00db57fd 100644 --- a/src/main/java/ecorp/easy/installer/controllers/subcontrollers/FlashSceneController.java +++ b/src/main/java/ecorp/easy/installer/controllers/subcontrollers/FlashSceneController.java @@ -25,7 +25,6 @@ import ecorp.easy.installer.logger.GUIAppender; import ecorp.easy.installer.logger.LogPathPropertyDefiner; import ecorp.easy.installer.models.DataBundle; import ecorp.easy.installer.threads.FlashThread; -import ecorp.easy.installer.tasks.TimerTask; import ecorp.easy.installer.tasks.UploadToEcloudTask; import ecorp.easy.installer.utils.IFlashHandler; import ecorp.easy.installer.utils.UiUtils; @@ -35,6 +34,9 @@ import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import javafx.animation.FadeTransition; +import javafx.animation.KeyFrame; +import javafx.animation.KeyValue; +import javafx.animation.Timeline; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.geometry.Pos; @@ -80,7 +82,9 @@ public class FlashSceneController extends AbstractSubSteppedController implement @FXML Button showHideLogBtn; @FXML Button sendLogBtn; - private TimerTask timer; //This is the task which make progression in a progress bar + private Timeline timeline; //this animate the progression in ProgressBar + + private boolean stopped = false; //determine if the process has been stopped (due to error or normal ending) private List instructionsKey; //Contains the different instruction that have to be currently displayed ResourceBundle instructionsImagesBundle; //Give access to image to loads with instructionsKey @@ -160,9 +164,9 @@ public class FlashSceneController extends AbstractSubSteppedController implement if(db == null)return; //close the timer of a loadStepType - if(timer != null){ - timer.cancel(); - timer = null; + if(timeline != null){ + timeline.stop(); + timeline = null; } //Update the progressBar of the global process @@ -278,12 +282,13 @@ public class FlashSceneController extends AbstractSubSteppedController implement int averageTime = db.getInt("averageTime"); logger.debug("averageTime = {} ", averageTime); if(averageTime > -1){ //Or > 0 ? - timer = new TimerTask(averageTime, this); + timeline = new Timeline( + new KeyFrame(Duration.millis(0), new KeyValue(loadStepProgressIndicator.progressProperty(), 0.0) ), + new KeyFrame(Duration.millis(averageTime*1000.0), new KeyValue(loadStepProgressIndicator.progressProperty(), 1.0)) + ); + timeline.playFromStart(); loadStepProgressIndicator.setProgress(0.0); //Reset to prevent showing last value UiUtils.showNode(loadStepProgressIndicator); - Thread timerThread = new Thread(timer); - timerThread.setDaemon(true); - timerThread.start(); } this.stepTitleIcon.setImage(null); } @@ -380,9 +385,9 @@ public class FlashSceneController extends AbstractSubSteppedController implement @Override public void onFlashStop(DataBundle db) { logger.info("onFlashStop()"); - if(timer != null){ - timer.cancel(); - timer = null; + if(timeline != null){ + timeline.stop(); + timeline = null; } stopped = true; parentController.resetNextButtonEventHandler(); @@ -464,16 +469,6 @@ public class FlashSceneController extends AbstractSubSteppedController implement } return false; } - - - /** - * Update the 'load' step's progress indicator. - * It is called by the TimeTask - * @param progression - */ - public synchronized void updateProgressIndicator(double progression){ - loadStepProgressIndicator.setProgress(progression); - } /** * The new behaviour of the parentController's nextButton diff --git a/src/main/java/ecorp/easy/installer/tasks/TimerTask.java b/src/main/java/ecorp/easy/installer/tasks/TimerTask.java deleted file mode 100644 index 983552ad..00000000 --- a/src/main/java/ecorp/easy/installer/tasks/TimerTask.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2019-2020 - ECORP SAS - - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package ecorp.easy.installer.tasks; -import ecorp.easy.installer.controllers.subcontrollers.FlashSceneController; -import javafx.application.Platform; -import javafx.concurrent.Task; - -/** - * - * @author vincent Bourgmayer - */ -public class TimerTask extends Task{ - - double averageTime; - FlashSceneController controller; - - /** - * Constructor for TimerTask - * @param averageTime time in seconds - * @param controller FlashSceneController - */ - public TimerTask(int averageTime, FlashSceneController controller){ - this.averageTime = averageTime*1000.0; - this.controller = controller; - } - - /** - * Implementation of this method is required to extends Task. - * - * @return because it extends Task< Void > - * @throws Exception - */ - @Override - protected Void call() throws Exception { - - double startTime = System.currentTimeMillis(); - double elapsedTime = 0; - Platform.runLater(() -> { - controller.updateProgressIndicator(0.0); - }); - while(!isCancelled() && elapsedTime < averageTime){ - Thread.sleep(300); //try with 150 - double newTime = System.currentTimeMillis(); - elapsedTime = newTime - startTime; - - double percent = elapsedTime / averageTime; - if(percent < 100.0) { - Platform.runLater(() -> { - controller.updateProgressIndicator(percent); - }); - } - } - return null; - } -} -- GitLab