From e9e2c0704d0aca4dad8a21a13db22f27d6ca1c01 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 13 Sep 2019 00:55:07 +0200 Subject: [PATCH] Test: added Pause checkbox --- .../jstudy/controller/TestController.java | 48 +++++++-------- .../java/es/kauron/jstudy/util/Clock.java | 46 ++++++++++++++ .../resources/es/kauron/jstudy/view/test.fxml | 60 +++++++++---------- 3 files changed, 93 insertions(+), 61 deletions(-) create mode 100644 src/main/java/es/kauron/jstudy/util/Clock.java diff --git a/src/main/java/es/kauron/jstudy/controller/TestController.java b/src/main/java/es/kauron/jstudy/controller/TestController.java index 4cb5fab..ce7526f 100644 --- a/src/main/java/es/kauron/jstudy/controller/TestController.java +++ b/src/main/java/es/kauron/jstudy/controller/TestController.java @@ -2,25 +2,22 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; -import javafx.application.Platform; +import es.kauron.jstudy.util.Clock; import javafx.beans.binding.Bindings; import javafx.beans.property.*; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.ProgressBar; -import javafx.scene.control.TextField; -import javafx.scene.layout.HBox; +import javafx.scene.control.*; +import javafx.scene.layout.Pane; import java.net.URL; import java.util.List; import java.util.ResourceBundle; -import java.util.Timer; -import java.util.TimerTask; public class TestController implements Initializable { + private static final TestItem ending = new TestItem("That's it!", ""); + @FXML private Label question, prevQuestion, prevAnswer, correctAnswer, progressLabel, correctLabel; @FXML @@ -28,9 +25,11 @@ public class TestController implements Initializable { @FXML private ProgressBar progress; @FXML - private HBox feedback; + private Pane feedback, buttonBox; @FXML private Button skipButton; + @FXML + private CheckBox pauseCheckBox; private final SimpleBooleanProperty correctingError = new SimpleBooleanProperty(false); private List list; @@ -38,19 +37,17 @@ public class TestController implements Initializable { private final ObjectProperty item = new SimpleObjectProperty<>(); private final IntegerProperty done = new SimpleIntegerProperty(0); // Time accounting - private final IntegerProperty seconds = new SimpleIntegerProperty(0); - private final IntegerProperty minutes = new SimpleIntegerProperty(0); - private final Timer timer = new Timer(); + private final Clock clock = new Clock(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { skipButton.disableProperty().bind(correctingError); feedback.visibleProperty().bind(AppPrefs.showFeedback); - seconds.addListener((obj, o, n) -> { - if (n.intValue() == 60) { - minutes.set(minutes.get() + 1); - seconds.set(0); - } + answer.disableProperty().bind(pauseCheckBox.selectedProperty()); + buttonBox.disableProperty().bind(pauseCheckBox.selectedProperty()); + pauseCheckBox.selectedProperty().addListener((obj, o, n) -> { + if (n) clock.pause(); + else clock.play(); }); } @@ -58,9 +55,9 @@ public class TestController implements Initializable { this.list = list; int total = list.size(); progressLabel.textProperty().bind(Bindings.format( - String.format("%%.2f%%%% %%d / %d %%d mistakes %%02d:%%02d", total), + String.format("%%.2f%%%% %%d / %d %%d mistakes %%s", total), done.multiply(100.0).divide(total), done, - errors, minutes, seconds)); + errors, clock.timeProperty())); progress.progressProperty().bind(done.divide((double) total)); item.addListener((obj, o, n) -> { if (o != null) { @@ -69,12 +66,7 @@ public class TestController implements Initializable { } question.setText(n.getQuestion()); }); - timer.schedule(new TimerTask() { - @Override - public void run() { - Platform.runLater(() -> seconds.set(seconds.get() + 1)); - } - }, 0, 1000); + clock.play(); onSkipAction(null); } @@ -134,8 +126,8 @@ public class TestController implements Initializable { @FXML private void onEndAction(ActionEvent event) { answer.setText(""); - answer.setDisable(true); - question.setText("That's it!"); - timer.cancel(); + item.set(ending); + pauseCheckBox.setSelected(true); + pauseCheckBox.setDisable(true); } } diff --git a/src/main/java/es/kauron/jstudy/util/Clock.java b/src/main/java/es/kauron/jstudy/util/Clock.java new file mode 100644 index 0000000..c3b10ab --- /dev/null +++ b/src/main/java/es/kauron/jstudy/util/Clock.java @@ -0,0 +1,46 @@ +package es.kauron.jstudy.util; + +import javafx.application.Platform; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +import java.util.Timer; +import java.util.TimerTask; + +public class Clock { + private static final short SEC_TO_MIN = 60; + private short seconds = 0; + private Timer timer = new Timer(); + private TimerTask task; + private final StringProperty time = new SimpleStringProperty(); + + public Clock() { + setObservable(); + } + + public StringProperty timeProperty() { + return time; + } + + public void play() { + if (task != null) task.cancel(); + task = new TimerTask() { + @Override + public void run() { + seconds++; + Platform.runLater(() -> setObservable()); + } + }; + timer.schedule(task, 0, 1000); + } + + public void pause() { + if (task != null) + task.cancel(); + task = null; + } + + private void setObservable() { + time.set(String.format("%02d:%02d", seconds / SEC_TO_MIN, seconds % SEC_TO_MIN)); + } +} diff --git a/src/main/resources/es/kauron/jstudy/view/test.fxml b/src/main/resources/es/kauron/jstudy/view/test.fxml index e43cdf5..e6cdd49 100644 --- a/src/main/resources/es/kauron/jstudy/view/test.fxml +++ b/src/main/resources/es/kauron/jstudy/view/test.fxml @@ -4,78 +4,72 @@ - +
- + - + - - + + - - - + + + - - +
- - - + - -