From 7fe80b07ec3b594e64fc56ae34215017c62f7e33 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 13 Sep 2019 00:15:27 +0200 Subject: [PATCH] Table: new editing and filtering * Split buttons between the context menu and above the table. * Moved adding/editing to above the table. * Added filtering. * Added shortcut to edit (double click) and to delete rows. --- .../jstudy/controller/TableController.java | 124 ++++++++++-------- .../es/kauron/jstudy/view/table.fxml | 9 +- 2 files changed, 76 insertions(+), 57 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index fd4fa12..59bb358 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -1,50 +1,47 @@ package es.kauron.jstudy.controller; -import es.kauron.jstudy.Main; 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.beans.binding.Bindings; +import javafx.beans.property.*; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; +import javafx.collections.transformation.FilteredList; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.SelectionMode; -import javafx.scene.control.TableColumn; -import javafx.scene.control.TableView; +import javafx.scene.control.*; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.scene.input.MouseEvent; import javafx.stage.FileChooser; -import javafx.stage.Modality; -import javafx.stage.Stage; import java.io.File; -import java.io.IOException; import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.ResourceBundle; +import java.util.*; public class TableController implements Initializable { @FXML private TableView table; @FXML + private TextField newQuestionField, newAnswerField, searchField; + @FXML + private Button addButton; + @FXML private TableColumn answerCol, questionCol; + private FilteredList filtered; private ObservableList data; private Controller parent; private File file; StringProperty name; final BooleanProperty saved = new SimpleBooleanProperty(); + private final ObjectProperty editing = new SimpleObjectProperty<>(null); @Override public void initialize(URL url, ResourceBundle resourceBundle) { + addButton.textProperty().bind(Bindings.when(editing.isNull()).then("_Add item").otherwise("_Save item")); // Add context menu to Table MenuItem menuEdit = new MenuItem("_Edit"); menuEdit.setOnAction(this::onEditAction); @@ -61,16 +58,18 @@ public class TableController implements Initializable { 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 -> menuEdit.setDisable(obs.getList().size() != 1)); + searchField.textProperty().addListener((obj, o, n) -> + filtered.setPredicate((item) -> item.getQuestion().contains(n) || item.getAnswer().contains(n))); } void setData(String name, List list, Controller controller, File file) { this.name = new SimpleStringProperty(name); this.data = FXCollections.observableArrayList(list); + this.filtered = data.filtered((item) -> true); this.parent = controller; - table.setItems(data); + table.setItems(filtered); this.file = file; saved.set(file != null); } @@ -99,41 +98,31 @@ public class TableController implements Initializable { @FXML protected void onAddAction(ActionEvent event) { - try { - FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/edit.fxml")); - Parent pRoot = loader.load(); - - ((EditController) loader.getController()).setList(data, saved); - - Stage stage = new Stage(); - stage.setTitle("New entry"); - stage.setScene(new Scene(pRoot)); - stage.initModality(Modality.APPLICATION_MODAL); - stage.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); + if (editing.get() == null) { + TestItem item = new TestItem(newQuestionField.getText().trim(), newAnswerField.getText().trim()); + data.add(item); + newQuestionField.setText(""); + newAnswerField.setText(""); + saved.set(false); + } else { + editing.get().answerProperty().set(newAnswerField.getText().trim()); + editing.get().questionProperty().set(newQuestionField.getText().trim()); + editing.set(null); + newQuestionField.setText(""); + newAnswerField.setText(""); } + newQuestionField.requestFocus(); } @FXML protected void onEditAction(ActionEvent event) { - ObservableList list = table.getSelectionModel().getSelectedIndices(); + ObservableList list = table.getSelectionModel().getSelectedItems(); if (list.size() != 1) return; - int index = list.get(0); - try { - FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/edit.fxml")); - Parent root = loader.load(); - - ((EditController) loader.getController()).setList(table.getItems(), index, saved); - - Stage stage = new Stage(); - stage.setTitle("Editing entry..."); - stage.setScene(new Scene(root)); - stage.initModality(Modality.APPLICATION_MODAL); - stage.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); - } + editing.set(list.get(0)); + newQuestionField.setText(list.get(0).getQuestion()); + newAnswerField.setText(list.get(0).getAnswer()); + saved.set(false); + newQuestionField.requestFocus(); } @FXML @@ -151,15 +140,14 @@ public class TableController implements Initializable { protected void onDuplicateAction(ActionEvent event) { if (table.getSelectionModel().getSelectedIndices().size() > 0) saved.set(false); for (int i : table.getSelectionModel().getSelectedIndices()) - table.getItems().add(new TestItem(table.getItems().get(i))); + data.add(new TestItem(filtered.get(i))); table.requestFocus(); } @FXML protected void onDeleteAction(ActionEvent event) { if (table.getSelectionModel().getSelectedIndices().size() > 0) saved.set(false); - for (int i : table.getSelectionModel().getSelectedIndices()) - table.getItems().remove(i); + data.removeAll(table.getSelectionModel().getSelectedItems()); table.requestFocus(); } @@ -170,6 +158,32 @@ public class TableController implements Initializable { @FXML protected void onTestAction(ActionEvent event) { - parent.newTest(data); + parent.newTest(filtered); + } + + @FXML + protected void onTableKeyEvent(KeyEvent event) { + if (event.getCode().equals(KeyCode.DELETE)) { + onDeleteAction(null); + } + } + + private final Timer timer = new Timer(); + private TimerTask timerTask = null; + @FXML + protected void onTableMouseClicked(MouseEvent event) { + if (timerTask == null) { + timerTask = new TimerTask() { + @Override + public void run() { + timerTask = null; + } + }; + timer.schedule(timerTask, 200); + } else { + if (timerTask.cancel()) + onEditAction(null); + timerTask = null; + } } } diff --git a/src/main/resources/es/kauron/jstudy/view/table.fxml b/src/main/resources/es/kauron/jstudy/view/table.fxml index bfe94b1..5b3626b 100644 --- a/src/main/resources/es/kauron/jstudy/view/table.fxml +++ b/src/main/resources/es/kauron/jstudy/view/table.fxml @@ -7,11 +7,14 @@ - +