From d26978946b0842b80f4503e3844050302dbf7449 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 9 Dec 2016 14:10:21 +0100 Subject: [PATCH] Moved preferences to Preferences class --- src/main/java/es/kauron/jstudy/Main.java | 2 +- .../es/kauron/jstudy/model/AppConfig.java | 41 ++++++------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/Main.java b/src/main/java/es/kauron/jstudy/Main.java index 116bcd6..27add72 100644 --- a/src/main/java/es/kauron/jstudy/Main.java +++ b/src/main/java/es/kauron/jstudy/Main.java @@ -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()); } diff --git a/src/main/java/es/kauron/jstudy/model/AppConfig.java b/src/main/java/es/kauron/jstudy/model/AppConfig.java index 84cbe88..26a277a 100644 --- a/src/main/java/es/kauron/jstudy/model/AppConfig.java +++ b/src/main/java/es/kauron/jstudy/model/AppConfig.java @@ -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() {