diff --git a/src/core/Counter.java b/src/core/Counter.java deleted file mode 100644 index aad65a54eb4dc334d38c5cda1f3668722217d30f..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..b91cb483c4d8e5acf11abd1aaa72e77cc7c72e59 --- /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 99879c8447d0853d198243bb4cf13dae0013ad04..30289f0eb8144f4703bb56e2ea473cbd6778f7d8 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 4a13eafe15d05b8b008d03db80b2915134242ddd..58d510626a358658feed1067251e34640c280e81 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);