From 412d5412e834781fe000511972dc6ff22deab9bb Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Fri, 24 Nov 2017 12:09:48 +0100 Subject: [PATCH] Fixed #18 - timer label use current time instead of increasing seconds counter --- src/core/Counter.java | 48 ------------------- src/core/Timer.java | 39 +++++++++++++++ src/userInterface/Main.java | 2 +- .../UserInterfaceController.java | 39 ++++++--------- 4 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 src/core/Counter.java create mode 100644 src/core/Timer.java diff --git a/src/core/Counter.java b/src/core/Counter.java deleted file mode 100644 index aad65a5..0000000 --- a/src/core/Counter.java +++ /dev/null @@ -1,48 +0,0 @@ -package core; - -import javafx.application.Platform; -import tools.ConvertTo; -import userInterface.UserInterfaceController; - -/** - * Thread für die zeitgleiche Ausführung der Zeitmessung ohne Beeinträchtigung der Hauptanwendung - * @author Robert - * - */ -public class Counter extends Thread -{ - public static boolean running; - public static long ausgabe; - public static UserInterfaceController uic; - - @Override - public void run() - { - //löscht zu Beginn den Text des Labels - uic.labelTime.setText(""); - //initialisiert die Zählvariable - ausgabe = 0; - - while (running) - { - try - { - //konvertiert die bereits verstrichenen Millisekunden in Stunden, Minuten und Sekunden - //und gibt diese auf dem Label aus - Platform.runLater(()->{ - uic.labelTime.setText(ConvertTo.ConvertMillisToTime(ausgabe)); - }); - - //schläft 1000 Millisekunden - Thread.sleep(1000); - //erhöht die Zählvariable um 1000 Millisekunden - ausgabe = ausgabe + 1000; - } - //reagiert auf eine InterruptedException, die ausgelöst wird, wenn der Stopp-Button gedrückt wird - catch (InterruptedException e) - { - running = false; - } - } - } -} \ No newline at end of file diff --git a/src/core/Timer.java b/src/core/Timer.java new file mode 100644 index 0000000..b91cb48 --- /dev/null +++ b/src/core/Timer.java @@ -0,0 +1,39 @@ +package core; + +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.animation.Animation.Status; +import javafx.scene.control.Label; +import javafx.util.Duration; +import tools.ConvertTo; + +public class Timer +{ + Timeline timeline; + long startTime; + + public Timer(Label label) + { + timeline = new Timeline(); + timeline.setCycleCount(Timeline.INDEFINITE); + timeline.getKeyFrames().add(new KeyFrame(Duration.millis(500), (event) -> { + label.setText(ConvertTo.ConvertMillisToTime(System.currentTimeMillis()-startTime)); + })); + } + + public void start() + { + startTime = System.currentTimeMillis(); + timeline.playFromStart(); + } + + public void stop() + { + timeline.stop(); + } + + public boolean isRunning() + { + return timeline.getStatus().equals(Status.RUNNING); + } +} diff --git a/src/userInterface/Main.java b/src/userInterface/Main.java index 99879c8..30289f0 100644 --- a/src/userInterface/Main.java +++ b/src/userInterface/Main.java @@ -50,7 +50,7 @@ public class Main extends Application { public void handle(WindowEvent we) { - if(controller.stoppUhrLäuftFlag == true) + if(controller.isTimerRunning()) { AlertGenerator.showAlert(AlertType.WARNING, "Warnung", "", "Die Stoppuhr läuft noch!", new Image("/userInterface/icon.png"), stage, null, false); diff --git a/src/userInterface/UserInterfaceController.java b/src/userInterface/UserInterfaceController.java index 4a13eaf..58d5106 100644 --- a/src/userInterface/UserInterfaceController.java +++ b/src/userInterface/UserInterfaceController.java @@ -18,6 +18,7 @@ import core.Importer; import core.LogObject; import core.SQL; import core.Settings; +import core.Timer; import core.Utils; import javafx.application.Platform; import javafx.beans.property.SimpleStringProperty; @@ -80,8 +81,7 @@ public class UserInterfaceController @FXML private Label labelSeparator; private Stage stage; - private core.Counter stoppUhr; - public boolean stoppUhrLäuftFlag; + private Timer timer; public boolean projektExistiertFlag; private ArrayList<TreeItem<HBox>> aktuelleTasks; private TreeItem<HBox> item; @@ -109,7 +109,6 @@ public class UserInterfaceController accordion.setExpandedPane(gesamtesLog); projektExistiertFlag = false; - stoppUhrLäuftFlag = false; labelTime.setText("0 h 0 min 0 sek"); @@ -121,30 +120,17 @@ public class UserInterfaceController if(projektExistiertFlag == true) if(startButton.isSelected()) - { - stoppUhrLäuftFlag = true; - - labelTime.setText(""); - core.Counter.ausgabe = 0; - + { startButton.setText("Stopp"); - - stoppUhr = new core.Counter(); - core.Counter.running = true; - core.Counter.uic = this; startClock(); - - stoppUhr.start(); + timer = new Timer(labelTime); + timer.start(); } else - { - stoppUhrLäuftFlag = false; + { startButton.setText("Start"); - - stoppUhr.interrupt(); - + timer.stop(); endClock(); - loadAll(); } else @@ -164,7 +150,7 @@ public class UserInterfaceController public void closeRequest() { // Prüft, ob die Stoppuhr noch läuft - if(stoppUhrLäuftFlag == true) + if(isTimerRunning()) { AlertGenerator.showAlert(AlertType.WARNING, "Warnung", "", "Stoppuhr läuft noch!", icon, stage, null, false); } @@ -173,6 +159,11 @@ public class UserInterfaceController stage.close(); } } + + public boolean isTimerRunning() + { + return timer != null && timer.isRunning(); + } public void setLabels(String project, String task) { @@ -182,7 +173,7 @@ public class UserInterfaceController public void openProjectGUI(ActionEvent e) { - if( ! stoppUhrLäuftFlag) + if(!isTimerRunning()) { try { @@ -478,14 +469,12 @@ public class UserInterfaceController private void startClock() { - stoppUhrLäuftFlag = true; log.createStartTime(); startTimestamp = System.currentTimeMillis(); } private void endClock() { - stoppUhrLäuftFlag = false; log.createEndTime(); endTimestamp = System.currentTimeMillis(); log.setDuration(endTimestamp - startTimestamp); -- GitLab