From 56575f6a1ec0f5d23e4cc3f7c36b8e181d502bb2 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Sun, 12 Jun 2016 12:58:35 +0200 Subject: [PATCH] Added settings tab --- .idea/misc.xml | 16 -------- src/main/java/es/kauron/jstudy/Main.java | 5 +++ .../kauron/jstudy/controller/Controller.java | 11 +++++ .../jstudy/controller/SettingsController.java | 26 ++++++++++++ .../es/kauron/jstudy/model/AppConfig.java | 40 +++++++++++++++++++ .../java/es/kauron/jstudy/model/TestItem.java | 1 + .../resources/es/kauron/jstudy/view/main.fxml | 1 + .../es/kauron/jstudy/view/settings.fxml | 11 +++++ 8 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 src/main/java/es/kauron/jstudy/controller/SettingsController.java create mode 100644 src/main/java/es/kauron/jstudy/model/AppConfig.java create mode 100644 src/main/resources/es/kauron/jstudy/view/settings.fxml diff --git a/.idea/misc.xml b/.idea/misc.xml index dfca8c4..233a7f0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -23,20 +23,4 @@ - - - - - 1.8 - - - - - - - \ No newline at end of file diff --git a/src/main/java/es/kauron/jstudy/Main.java b/src/main/java/es/kauron/jstudy/Main.java index d3dc041..26541e2 100644 --- a/src/main/java/es/kauron/jstudy/Main.java +++ b/src/main/java/es/kauron/jstudy/Main.java @@ -1,5 +1,6 @@ package es.kauron.jstudy; +import es.kauron.jstudy.model.AppConfig; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -10,10 +11,14 @@ 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.setScene(new Scene(root)); primaryStage.show(); + primaryStage.setOnCloseRequest(event -> { + AppConfig.save(); + }); } diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 134245e..c41b4c6 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -42,6 +42,17 @@ public class Controller implements Initializable { saveMenu.disableProperty().bind(table.not()); } + @FXML + private void onSettingsAction(ActionEvent event) { + try { + Parent root = FXMLLoader.load(Main.class.getResource("view/settings.fxml")); + tabPane.getTabs().add(new Tab("Settings", root)); + tabPane.getSelectionModel().selectLast(); + } catch (IOException e) { + e.printStackTrace(); + } + } + @FXML private void onNewAction(ActionEvent event) { TextInputDialog dialog = new TextInputDialog("Name"); diff --git a/src/main/java/es/kauron/jstudy/controller/SettingsController.java b/src/main/java/es/kauron/jstudy/controller/SettingsController.java new file mode 100644 index 0000000..7164b05 --- /dev/null +++ b/src/main/java/es/kauron/jstudy/controller/SettingsController.java @@ -0,0 +1,26 @@ +package es.kauron.jstudy.controller; + +import com.jfoenix.controls.JFXCheckBox; +import es.kauron.jstudy.model.AppConfig; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; + +import java.net.URL; +import java.util.ResourceBundle; + +public class SettingsController implements Initializable { + @FXML + private JFXCheckBox repeatMistakes; + + @Override + public void initialize(URL location, ResourceBundle resources) { + System.err.println(AppConfig.printValues()); + repeatMistakes.setSelected(AppConfig.repeatWrong); + repeatMistakes.selectedProperty().addListener((change, o, n) -> { + AppConfig.repeatWrong = n; + System.err.println(AppConfig.printValues()); + }); + } + + +} diff --git a/src/main/java/es/kauron/jstudy/model/AppConfig.java b/src/main/java/es/kauron/jstudy/model/AppConfig.java new file mode 100644 index 0000000..a6a759e --- /dev/null +++ b/src/main/java/es/kauron/jstudy/model/AppConfig.java @@ -0,0 +1,40 @@ +package es.kauron.jstudy.model; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.Scanner; + +public class AppConfig { + private static final String FILE = "./config.ini"; + public static boolean repeatWrong; + + public static void save() { + try { + PrintWriter pw = new PrintWriter(new File(FILE)); + pw.print("repeatWrong=" + repeatWrong); + pw.flush(); + pw.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public static void load() { + try { + Scanner in = new Scanner(new File(FILE)); + while (in.hasNextLine()) { + String line = in.nextLine(); + if (line.matches("repeatWrong=.*")) { + repeatWrong = Boolean.parseBoolean(line.substring(line.indexOf('=') + 1)); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public static String printValues() { + return "repeatWrong=" + repeatWrong; + } +} diff --git a/src/main/java/es/kauron/jstudy/model/TestItem.java b/src/main/java/es/kauron/jstudy/model/TestItem.java index d791797..2383be4 100644 --- a/src/main/java/es/kauron/jstudy/model/TestItem.java +++ b/src/main/java/es/kauron/jstudy/model/TestItem.java @@ -76,6 +76,7 @@ public class TestItem { list.add(new TestItem(question, answer)); System.err.println(question + " :: " + answer); } + in.close(); } catch (FileNotFoundException | InputMismatchException e) { e.printStackTrace(); } diff --git a/src/main/resources/es/kauron/jstudy/view/main.fxml b/src/main/resources/es/kauron/jstudy/view/main.fxml index f278f19..48b0caa 100644 --- a/src/main/resources/es/kauron/jstudy/view/main.fxml +++ b/src/main/resources/es/kauron/jstudy/view/main.fxml @@ -22,6 +22,7 @@