1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-11-13 07:33:44 +01:00

FileChooser will remember last folders and will suggest filenames

This commit is contained in:
Carlos Galindo 2016-12-08 22:32:55 +01:00
parent 2613dc66e1
commit 112ab9f293
Signed by: kauron
GPG key ID: 83E68706DEE119A3
3 changed files with 15 additions and 2 deletions

View file

@ -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<File> 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);

View file

@ -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<TestItem> 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<TestItem> list, Controller controller, File file) {
void setData(String name, List<TestItem> 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);

View file

@ -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 {