From 112ab9f2932067eeebbfe61041190602eca2a5e6 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 8 Dec 2016 22:32:55 +0100 Subject: [PATCH] FileChooser will remember last folders and will suggest filenames --- .../java/es/kauron/jstudy/controller/Controller.java | 7 ++++++- .../es/kauron/jstudy/controller/TableController.java | 9 ++++++++- src/main/java/es/kauron/jstudy/model/AppConfig.java | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 6cb02dd..f3b8674 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -1,6 +1,7 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.Main; +import es.kauron.jstudy.model.AppConfig; import es.kauron.jstudy.model.TestItem; import javafx.application.Platform; import javafx.beans.property.BooleanProperty; @@ -83,8 +84,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) chooser.setInitialDirectory(AppConfig.lastDir); List list = chooser.showOpenMultipleDialog(root.getScene().getWindow()); if (list == null || list.isEmpty()) return; + AppConfig.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); @@ -127,8 +130,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); File file = chooser.showOpenDialog(root.getScene().getWindow()); if (file == null) return; + AppConfig.lastDir = file.getParentFile(); String separator; if (file.getName().matches(".*txt")) separator = TestItem.TAB; @@ -150,7 +155,7 @@ public class Controller implements Initializable { FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/table.fxml")); Parent tableRoot = loader.load(); - ((TableController) loader.getController()).setData(list, this, file); + ((TableController) loader.getController()).setData(name, list, this, file); tabMap.put(tab, loader.getController()); tab.setContent(tableRoot); diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index 5865126..2d77a1c 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -1,6 +1,7 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.Main; +import es.kauron.jstudy.model.AppConfig; import es.kauron.jstudy.model.TestItem; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -38,6 +39,7 @@ public class TableController implements Initializable { private ObservableList data; private Controller parent; private File file; + private String name; protected BooleanProperty saved = new SimpleBooleanProperty(); @Override @@ -55,7 +57,8 @@ public class TableController implements Initializable { saveButton.disableProperty().bind(saved); } - void setData(List list, Controller controller, File file) { + void setData(String name, List list, Controller controller, File file) { + this.name = name; this.data = FXCollections.observableArrayList(list); this.parent = controller; table.setItems(data); @@ -68,7 +71,11 @@ 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); + chooser.setInitialFileName(name); file = chooser.showSaveDialog(table.getScene().getWindow()); + if (file != null) + AppConfig.lastDir = file.getParentFile(); } if (file == null) return; TestItem.saveTo(file, data); diff --git a/src/main/java/es/kauron/jstudy/model/AppConfig.java b/src/main/java/es/kauron/jstudy/model/AppConfig.java index c086e48..84cbe88 100644 --- a/src/main/java/es/kauron/jstudy/model/AppConfig.java +++ b/src/main/java/es/kauron/jstudy/model/AppConfig.java @@ -8,6 +8,7 @@ import java.util.Scanner; public class AppConfig { private static final String FILE = "./config.ini"; public static boolean repeatWrong, showFeedback; + public static File lastDir; public static void save() { try {