1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-07-01 07:53:02 +02:00

Moved preferences to Preferences class

This commit is contained in:
Carlos Galindo 2016-12-09 14:10:21 +01:00
parent b9d23ca80a
commit d26978946b
Signed by: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 14 additions and 29 deletions

View file

@ -18,7 +18,7 @@ public class Main extends Application {
primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("img/icon.png"))); primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("img/icon.png")));
primaryStage.setScene(new Scene(root)); primaryStage.setScene(new Scene(root));
primaryStage.show(); primaryStage.show();
primaryStage.setOnCloseRequest(event -> AppConfig.save()); primaryStage.setOnHiding(event -> AppConfig.save());
} }

View file

@ -1,46 +1,31 @@
package es.kauron.jstudy.model; package es.kauron.jstudy.model;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.util.prefs.BackingStoreException;
import java.io.PrintWriter; import java.util.prefs.Preferences;
import java.util.Scanner;
public class AppConfig { public class AppConfig {
private static final String FILE = "./config.ini";
public static boolean repeatWrong, showFeedback; public static boolean repeatWrong, showFeedback;
public static File lastDir; public static File lastDir;
public static void save() { public static void save() {
Preferences prefs = Preferences.userRoot();
prefs.putBoolean("repeatWrong", repeatWrong);
prefs.putBoolean("showFeedback", showFeedback);
prefs.put("lastDir", lastDir == null ? "" : lastDir.getAbsolutePath());
try { try {
PrintWriter pw = new PrintWriter(new File(FILE)); prefs.flush();
pw.println("repeatWrong=" + repeatWrong); } catch (BackingStoreException e) {
pw.println("showFeedback=" + showFeedback);
pw.flush();
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void load() { public static void load() {
File file = new File(FILE); Preferences prefs = Preferences.userRoot();
if (!file.exists()) { repeatWrong = prefs.getBoolean("repeatWrong", true);
repeatWrong = true; showFeedback = prefs.getBoolean("showFeedback", true);
showFeedback = true; String dirPath = prefs.get("lastDir", "");
} else if (!dirPath.isEmpty()) lastDir = new File(dirPath);
try {
Scanner in = new Scanner(new File(FILE));
while (in.hasNextLine()) {
String line = in.nextLine();
String value = line.substring(line.indexOf('=') + 1);
if (line.matches("repeatWrong=.*"))
repeatWrong = Boolean.parseBoolean(value);
else if (line.matches("showFeedback=.*"))
showFeedback = Boolean.parseBoolean(value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} }
public static String printValues() { public static String printValues() {