From a599508fc3f12b16d600e66bb84418facdec3405 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 13 Sep 2019 01:30:34 +0200 Subject: [PATCH] Table: removed the possibility for repeated questions --- .../jstudy/controller/TableController.java | 30 +++++++++++++++++-- .../es/kauron/jstudy/view/table.fxml | 29 +++++++----------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index 59bb358..99786d0 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -41,7 +41,14 @@ public class TableController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - addButton.textProperty().bind(Bindings.when(editing.isNull()).then("_Add item").otherwise("_Save item")); + addButton.textProperty().bind(Bindings.when(addButton.disableProperty()) + .then("Question repeated") + .otherwise(Bindings.when(editing.isNull()) + .then("_Add item") + .otherwise("_Save item"))); + addButton.getTooltip().textProperty().bind(Bindings.when(editing.isNull()) + .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); @@ -62,6 +69,16 @@ public class TableController implements Initializable { (ListChangeListener) obs -> menuEdit.setDisable(obs.getList().size() != 1)); searchField.textProperty().addListener((obj, o, n) -> filtered.setPredicate((item) -> item.getQuestion().contains(n) || item.getAnswer().contains(n))); + newQuestionField.textProperty().addListener((obj, o, n) -> { + for (TestItem item : data) { + if (item == editing.get()) continue; + if (item.getQuestion().equals(n.trim())) { + addButton.setDisable(true); + return; + } + } + addButton.setDisable(false); + }); } void setData(String name, List list, Controller controller, File file) { @@ -98,6 +115,10 @@ public class TableController implements Initializable { @FXML protected void onAddAction(ActionEvent event) { + if (newQuestionField.getText().trim().isEmpty() && newAnswerField.getText().trim().isEmpty()) { + newQuestionField.requestFocus(); + return; + } if (editing.get() == null) { TestItem item = new TestItem(newQuestionField.getText().trim(), newAnswerField.getText().trim()); data.add(item); @@ -139,8 +160,11 @@ public class TableController implements Initializable { @FXML protected void onDuplicateAction(ActionEvent event) { if (table.getSelectionModel().getSelectedIndices().size() > 0) saved.set(false); - for (int i : table.getSelectionModel().getSelectedIndices()) - data.add(new TestItem(filtered.get(i))); + for (int i : table.getSelectionModel().getSelectedIndices()) { + TestItem item = new TestItem(filtered.get(i)); + item.questionProperty().setValue(item.getQuestion() + " 2"); + data.add(item); + } table.requestFocus(); } diff --git a/src/main/resources/es/kauron/jstudy/view/table.fxml b/src/main/resources/es/kauron/jstudy/view/table.fxml index 5b3626b..66d9e92 100644 --- a/src/main/resources/es/kauron/jstudy/view/table.fxml +++ b/src/main/resources/es/kauron/jstudy/view/table.fxml @@ -7,26 +7,19 @@ - - - - - - - - - - - - + + @@ -38,9 +31,7 @@ - +