From 8b41dff1c5edb9b7dcd06131f50c72c16b29ddbc Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 10 Sep 2019 18:51:25 +0200 Subject: [PATCH] Settings: shortened code to load/save preferences --- src/main/java/es/kauron/jstudy/Main.java | 5 +- .../kauron/jstudy/controller/Controller.java | 10 ++-- .../jstudy/controller/SettingsController.java | 8 +-- .../jstudy/controller/TableController.java | 6 +- .../jstudy/controller/TestController.java | 8 +-- .../es/kauron/jstudy/model/AppConfig.java | 42 -------------- .../java/es/kauron/jstudy/model/AppPrefs.java | 55 +++++++++++++++++++ 7 files changed, 73 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/es/kauron/jstudy/model/AppConfig.java create mode 100644 src/main/java/es/kauron/jstudy/model/AppPrefs.java diff --git a/src/main/java/es/kauron/jstudy/Main.java b/src/main/java/es/kauron/jstudy/Main.java index 27add72..ced64cf 100644 --- a/src/main/java/es/kauron/jstudy/Main.java +++ b/src/main/java/es/kauron/jstudy/Main.java @@ -1,6 +1,6 @@ package es.kauron.jstudy; -import es.kauron.jstudy.model.AppConfig; +import es.kauron.jstudy.model.AppPrefs; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -12,13 +12,12 @@ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ - AppConfig.load(); Parent root = FXMLLoader.load(Main.class.getResource("view/main.fxml")); primaryStage.setTitle("JStudy"); primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("img/icon.png"))); primaryStage.setScene(new Scene(root)); primaryStage.show(); - primaryStage.setOnHiding(event -> AppConfig.save()); + primaryStage.setOnHiding(event -> AppPrefs.save()); } diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 989a183..1f645be 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -1,7 +1,7 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.Main; -import es.kauron.jstudy.model.AppConfig; +import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; import javafx.application.Platform; import javafx.beans.property.BooleanProperty; @@ -93,10 +93,10 @@ public class Controller implements Initializable { private void onLoadAction(ActionEvent event) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JStudy file", "*.jsdb")); - if (AppConfig.lastDir != null && AppConfig.lastDir.exists()) chooser.setInitialDirectory(AppConfig.lastDir); + if (AppPrefs.lastDir != null && AppPrefs.lastDir.exists()) chooser.setInitialDirectory(AppPrefs.lastDir); List list = chooser.showOpenMultipleDialog(root.getScene().getWindow()); if (list == null || list.isEmpty()) return; - AppConfig.lastDir = list.get(0).getParentFile(); + AppPrefs.lastDir = list.get(0).getParentFile(); if (list.size() > 1) { ButtonType mergeBT = new ButtonType("Merge", ButtonBar.ButtonData.APPLY); ButtonType openBT = new ButtonType("Open", ButtonBar.ButtonData.OK_DONE); @@ -138,10 +138,10 @@ public class Controller implements Initializable { private void onImportAction(ActionEvent event) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv, .txt)", "*.csv", "*.txt")); - if (AppConfig.lastDir != null) chooser.setInitialDirectory(AppConfig.lastDir); + if (AppPrefs.lastDir != null) chooser.setInitialDirectory(AppPrefs.lastDir); File file = chooser.showOpenDialog(root.getScene().getWindow()); if (file == null) return; - AppConfig.lastDir = file.getParentFile(); + AppPrefs.lastDir = file.getParentFile(); String separator; if (file.getName().matches(".*txt")) separator = TestItem.TAB; diff --git a/src/main/java/es/kauron/jstudy/controller/SettingsController.java b/src/main/java/es/kauron/jstudy/controller/SettingsController.java index 587e200..a15150a 100644 --- a/src/main/java/es/kauron/jstudy/controller/SettingsController.java +++ b/src/main/java/es/kauron/jstudy/controller/SettingsController.java @@ -1,7 +1,7 @@ package es.kauron.jstudy.controller; import com.jfoenix.controls.JFXCheckBox; -import es.kauron.jstudy.model.AppConfig; +import es.kauron.jstudy.model.AppPrefs; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -15,9 +15,9 @@ public class SettingsController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { repeatImmediately.disableProperty().bind(repeatMistakes.selectedProperty().not()); - repeatImmediately.selectedProperty().bindBidirectional(AppConfig.repeatImmediately); - repeatMistakes.selectedProperty().bindBidirectional(AppConfig.repeatWrong); - showFeedback.selectedProperty().bindBidirectional(AppConfig.showFeedback); + repeatImmediately.selectedProperty().bindBidirectional(AppPrefs.repeatImmediately); + repeatMistakes.selectedProperty().bindBidirectional(AppPrefs.repeatWrong); + showFeedback.selectedProperty().bindBidirectional(AppPrefs.showFeedback); repeatMistakes.selectedProperty().addListener((obj, o, n) -> { if (!n) repeatImmediately.setSelected(false); }); diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index b9cf2f4..922084a 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -1,7 +1,7 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.Main; -import es.kauron.jstudy.model.AppConfig; +import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -75,13 +75,13 @@ public class TableController implements Initializable { if (file == null) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JStudy file", "*.jsdb")); - if (AppConfig.lastDir != null) chooser.setInitialDirectory(AppConfig.lastDir); + if (AppPrefs.lastDir != null) chooser.setInitialDirectory(AppPrefs.lastDir); chooser.setInitialFileName(name); file = chooser.showSaveDialog(table.getScene().getWindow()); if (!file.getName().endsWith(".jsdb")) file = new File(file.getPath() + ".jsdb"); if (file != null) - AppConfig.lastDir = file.getParentFile(); + AppPrefs.lastDir = file.getParentFile(); } if (file == null) return; TestItem.saveTo(file, data); diff --git a/src/main/java/es/kauron/jstudy/controller/TestController.java b/src/main/java/es/kauron/jstudy/controller/TestController.java index 0ce5e77..41451d4 100644 --- a/src/main/java/es/kauron/jstudy/controller/TestController.java +++ b/src/main/java/es/kauron/jstudy/controller/TestController.java @@ -1,6 +1,6 @@ package es.kauron.jstudy.controller; -import es.kauron.jstudy.model.AppConfig; +import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; import javafx.beans.property.SimpleBooleanProperty; import javafx.event.ActionEvent; @@ -65,7 +65,7 @@ public class TestController implements Initializable { } // Remove the question from the pool if the question was correctly answered or there is no repetition - if ((right && !correctingError.get()) || !AppConfig.repeatWrong.get()) { + if ((right && !correctingError.get()) || !AppPrefs.repeatWrong.get()) { list.remove(current); progress.setProgress(++done / (double) total); progressLabel.setText(done + "/" + total); @@ -74,10 +74,10 @@ public class TestController implements Initializable { return; } chooseQuestion(); - } else if (!AppConfig.repeatImmediately.get()) { + } else if (!AppPrefs.repeatImmediately.get()) { chooseQuestion(); } - correctingError.set(!right && AppConfig.repeatImmediately.get()); + correctingError.set(!right && AppPrefs.repeatImmediately.get()); setQuestion(); } diff --git a/src/main/java/es/kauron/jstudy/model/AppConfig.java b/src/main/java/es/kauron/jstudy/model/AppConfig.java deleted file mode 100644 index a0454a5..0000000 --- a/src/main/java/es/kauron/jstudy/model/AppConfig.java +++ /dev/null @@ -1,42 +0,0 @@ -package es.kauron.jstudy.model; - -import javafx.beans.property.BooleanProperty; -import javafx.beans.property.SimpleBooleanProperty; - -import java.io.File; -import java.util.prefs.BackingStoreException; -import java.util.prefs.Preferences; - -public class AppConfig { - public static BooleanProperty repeatWrong, showFeedback, repeatImmediately; - public static File lastDir; - - public static void save() { - Preferences prefs = Preferences.userRoot(); - prefs.putBoolean("repeatWrong", repeatWrong.get()); - prefs.putBoolean("showFeedback", showFeedback.get()); - prefs.putBoolean("repeatImmediately", repeatImmediately.get()); - prefs.put("lastDir", lastDir == null ? "" : lastDir.getAbsolutePath()); - try { - prefs.flush(); - } catch (BackingStoreException e) { - e.printStackTrace(); - } - } - - public static void load() { - Preferences prefs = Preferences.userRoot(); - repeatWrong = new SimpleBooleanProperty(prefs.getBoolean("repeatWrong", true)); - showFeedback = new SimpleBooleanProperty(prefs.getBoolean("showFeedback", true)); - repeatImmediately = new SimpleBooleanProperty(prefs.getBoolean("repeatImmediately", false)); - repeatWrong.addListener((obj, o, n) -> save()); - showFeedback.addListener((obj, o, n) -> save()); - repeatImmediately.addListener((obj, o, n) -> save()); - String dirPath = prefs.get("lastDir", ""); - if (!dirPath.isEmpty()) lastDir = new File(dirPath); - } - - public static String printValues() { - return String.format("repeatWrong=%b\nshowFeedback=%b", repeatWrong, showFeedback); - } -} diff --git a/src/main/java/es/kauron/jstudy/model/AppPrefs.java b/src/main/java/es/kauron/jstudy/model/AppPrefs.java new file mode 100644 index 0000000..2d07b5e --- /dev/null +++ b/src/main/java/es/kauron/jstudy/model/AppPrefs.java @@ -0,0 +1,55 @@ +package es.kauron.jstudy.model; + +import javafx.beans.property.SimpleBooleanProperty; + +import java.io.File; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; + +public class AppPrefs { + private static final Preferences prefs = Preferences.userRoot(); + public static final BooleanPref repeatWrong = new BooleanPref(false, "repeatWrong"); + public static final BooleanPref showFeedback = new BooleanPref(true, "showFeedback"); + public static final BooleanPref repeatImmediately = new BooleanPref(false, "repeatImmediately"); + public static File lastDir; + + static { + // Load data (booleanprefs are loaded automatically) + String dirPath = prefs.get("lastDir", ""); + if (!dirPath.isEmpty()) lastDir = new File(dirPath); + } + + private static void flushPrefs() { + try { + prefs.flush(); + } catch (BackingStoreException e) { + e.printStackTrace(); + } + } + + public static void save() { + Preferences prefs = Preferences.userRoot(); + prefs.put("lastDir", lastDir == null ? "" : lastDir.getAbsolutePath()); + flushPrefs(); + } + + public static String printValues() { + return String.format("repeatWrong=%b\nshowFeedback=%b", repeatWrong, showFeedback); + } + + public static class BooleanPref extends SimpleBooleanProperty { + private final String name; + + BooleanPref(boolean defValue, String name) { + super(defValue); + this.name = name; + set(prefs.getBoolean(name, defValue)); + addListener((obj, o, n) -> this.save()); + } + + void save() { + prefs.putBoolean(name, get()); + flushPrefs(); + } + } +}