mirror of
https://gitlab.com/kauron/jstudy
synced 2024-12-22 08:23:33 +01:00
Settings: shortened code to load/save preferences
This commit is contained in:
parent
54bcedbef1
commit
8b41dff1c5
7 changed files with 73 additions and 61 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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<File> 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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
55
src/main/java/es/kauron/jstudy/model/AppPrefs.java
Normal file
55
src/main/java/es/kauron/jstudy/model/AppPrefs.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue