mirror of
https://gitlab.com/kauron/jstudy
synced 2025-09-30 21:01:10 +02:00
Test: added Pause checkbox
This commit is contained in:
parent
7fe80b07ec
commit
e9e2c0704d
3 changed files with 93 additions and 61 deletions
|
@ -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<TestItem> list;
|
||||
|
@ -38,19 +37,17 @@ public class TestController implements Initializable {
|
|||
private final ObjectProperty<TestItem> 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);
|
||||
}
|
||||
}
|
||||
|
|
46
src/main/java/es/kauron/jstudy/util/Clock.java
Normal file
46
src/main/java/es/kauron/jstudy/util/Clock.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue