1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-06-26 13:33: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.setScene(new Scene(root));
primaryStage.show();
primaryStage.setOnCloseRequest(event -> AppConfig.save());
primaryStage.setOnHiding(event -> AppConfig.save());
}

View file

@ -1,46 +1,31 @@
package es.kauron.jstudy.model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
public class AppConfig {
private static final String FILE = "./config.ini";
public static boolean repeatWrong, showFeedback;
public static File lastDir;
public static void save() {
Preferences prefs = Preferences.userRoot();
prefs.putBoolean("repeatWrong", repeatWrong);
prefs.putBoolean("showFeedback", showFeedback);
prefs.put("lastDir", lastDir == null ? "" : lastDir.getAbsolutePath());
try {
PrintWriter pw = new PrintWriter(new File(FILE));
pw.println("repeatWrong=" + repeatWrong);
pw.println("showFeedback=" + showFeedback);
pw.flush();
pw.close();
} catch (FileNotFoundException e) {
prefs.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
public static void load() {
File file = new File(FILE);
if (!file.exists()) {
repeatWrong = true;
showFeedback = true;
} else
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();
}
Preferences prefs = Preferences.userRoot();
repeatWrong = prefs.getBoolean("repeatWrong", true);
showFeedback = prefs.getBoolean("showFeedback", true);
String dirPath = prefs.get("lastDir", "");
if (!dirPath.isEmpty()) lastDir = new File(dirPath);
}
public static String printValues() {