From f67c9a9d19b451ecd1b939eb0c55c5fe8669e516 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 13 Sep 2019 12:09:33 +0200 Subject: [PATCH] Table: disable buttons when appropriate --- src/main/java/es/kauron/jstudy/Main.java | 5 --- .../jstudy/controller/TableController.java | 38 ++++++++++++------- .../es/kauron/jstudy/view/table.fxml | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/Main.java b/src/main/java/es/kauron/jstudy/Main.java index b7570e0..ced64cf 100644 --- a/src/main/java/es/kauron/jstudy/Main.java +++ b/src/main/java/es/kauron/jstudy/Main.java @@ -20,11 +20,6 @@ public class Main extends Application { primaryStage.setOnHiding(event -> AppPrefs.save()); } - @Override - public void stop() throws Exception { - super.stop(); - System.exit(0); - } public static void main(String[] args) { launch(args); diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index ef7ea9c..20bd8ce 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -27,7 +27,7 @@ public class TableController implements Initializable { @FXML private TextField newQuestionField, newAnswerField, searchField; @FXML - private Button addButton; + private Button addButton, testButton; @FXML private TableColumn answerCol, questionCol; @@ -50,23 +50,30 @@ public class TableController implements Initializable { .then("Add an entry to the table") .otherwise("Save changes in the selected entry")); // Add context menu to Table - MenuItem menuEdit = new MenuItem("_Edit"); - menuEdit.setOnAction(this::onEditAction); - MenuItem menuDelete = new MenuItem("_Delete"); - menuDelete.setOnAction(this::onDeleteAction); - MenuItem menuDup = new MenuItem("D_uplicate"); - menuDup.setOnAction(this::onDuplicateAction); - MenuItem menuSwap = new MenuItem("_Swap"); - menuSwap.setOnAction(this::onSwapAction); - MenuItem menuTest = new MenuItem("_Test selected"); - menuTest.setOnAction(this::onTestSelectionAction); - table.setContextMenu(new ContextMenu(menuEdit, menuDelete, menuDup, menuSwap, new SeparatorMenuItem(), menuTest)); + MenuItem mEdit = new MenuItem("_Edit"); + mEdit.setOnAction(this::onEditAction); + MenuItem mDel = new MenuItem("_Delete"); + mDel.setOnAction(this::onDeleteAction); + MenuItem mDup = new MenuItem("D_uplicate"); + mDup.setOnAction(this::onDuplicateAction); + mDup.disableProperty().bind(mDel.disableProperty()); + MenuItem mSwap = new MenuItem("_Swap"); + mSwap.setOnAction(this::onSwapAction); + mSwap.disableProperty().bind(mDup.disableProperty()); + MenuItem mTest = new MenuItem("_Test selected"); + mTest.setOnAction(this::onTestSelectionAction); + mTest.disableProperty().bind(mSwap.disableProperty()); + mEdit.setDisable(true); + mDel.setDisable(true); + table.setContextMenu(new ContextMenu(mEdit, mDel, mDup, mSwap, new SeparatorMenuItem(), mTest)); table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); answerCol.setCellValueFactory(e -> e.getValue().answerProperty()); questionCol.setCellValueFactory(e -> e.getValue().questionProperty()); - table.getSelectionModel().getSelectedIndices().addListener( - (ListChangeListener) obs -> menuEdit.setDisable(obs.getList().size() != 1)); + table.getSelectionModel().getSelectedIndices().addListener((ListChangeListener) obs -> { + mEdit.setDisable(obs.getList().size() != 1); + mDel.setDisable(obs.getList().size() == 0); + }); searchField.textProperty().addListener((obj, o, n) -> filtered.setPredicate((item) -> item.getQuestion().contains(n) || item.getAnswer().contains(n))); newQuestionField.textProperty().addListener((obj, o, n) -> { @@ -88,6 +95,9 @@ public class TableController implements Initializable { this.parent = controller; table.setRowFactory(new DraggableRowFactory<>(data)::generator); table.setItems(filtered); + filtered.addListener((ListChangeListener) c -> + testButton.setDisable(filtered.size() == 0)); + testButton.setDisable(filtered.size() == 0); this.file = file; saved.set(file != null); } diff --git a/src/main/resources/es/kauron/jstudy/view/table.fxml b/src/main/resources/es/kauron/jstudy/view/table.fxml index d6b40ca..88cb258 100644 --- a/src/main/resources/es/kauron/jstudy/view/table.fxml +++ b/src/main/resources/es/kauron/jstudy/view/table.fxml @@ -28,7 +28,7 @@ -