From ce4b58aa4fee8c2f880588d6d3cbd31bda9ce2d9 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Wed, 11 Sep 2019 19:52:59 +0200 Subject: [PATCH] Table: added unsaved indicator --- .../java/es/kauron/jstudy/controller/Controller.java | 7 ++++++- .../es/kauron/jstudy/controller/TableController.java | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 2362d23..caa6979 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -4,6 +4,7 @@ import es.kauron.jstudy.Main; import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; import javafx.application.Platform; +import javafx.beans.binding.Bindings; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.event.ActionEvent; @@ -163,7 +164,11 @@ public class Controller implements Initializable { FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/table.fxml")); Parent tableRoot = loader.load(); - ((TableController) loader.getController()).setData(name, list, this, file); + TableController tController = loader.getController(); + tController.setData(name, list, this, file); + tab.textProperty().bind(Bindings.format("%s%s", + Bindings.when(tController.saved).then("").otherwise("*"), + tController.name)); tabMap.put(tab, loader.getController()); MenuItem duplicate = new MenuItem("Duplicate table"); diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index 922084a..c5c1622 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -5,6 +5,8 @@ import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; @@ -40,7 +42,7 @@ public class TableController implements Initializable { private ObservableList data; private Controller parent; private File file; - private String name; + StringProperty name; BooleanProperty saved = new SimpleBooleanProperty(); @Override @@ -59,7 +61,7 @@ public class TableController implements Initializable { } void setData(String name, List list, Controller controller, File file) { - this.name = name; + this.name = new SimpleStringProperty(name); this.data = FXCollections.observableArrayList(list); this.parent = controller; table.setItems(data); @@ -68,7 +70,7 @@ public class TableController implements Initializable { } List getData() {return new ArrayList<>(data);} - String getName() {return file == null ? name : file.getName().substring(0, file.getName().lastIndexOf('.'));} + String getName() {return name.get();} @FXML protected void onSaveAction(ActionEvent event) { @@ -76,12 +78,13 @@ public class TableController implements Initializable { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JStudy file", "*.jsdb")); if (AppPrefs.lastDir != null) chooser.setInitialDirectory(AppPrefs.lastDir); - chooser.setInitialFileName(name); + chooser.setInitialFileName(name.get()); file = chooser.showSaveDialog(table.getScene().getWindow()); if (!file.getName().endsWith(".jsdb")) file = new File(file.getPath() + ".jsdb"); if (file != null) AppPrefs.lastDir = file.getParentFile(); + name.set(file.getName().substring(0, file.getName().lastIndexOf('.'))); } if (file == null) return; TestItem.saveTo(file, data);