From c5f62ed84aeba354e479073f5207b044b9adb3e0 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Fri, 15 Mar 2019 04:09:04 +0300 Subject: [PATCH] Context menu added to the TableView for items. Now it's possible to remove items from the table. --- .../Controllers/NSLMainController.java | 6 +++ .../Controllers/NSTableViewController.java | 52 ++++++++++++++++--- src/main/resources/locale.properties | 2 + src/main/resources/locale_rus.properties | 2 + src/main/resources/locale_ukr.properties | 4 +- 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/nsusbloader/Controllers/NSLMainController.java b/src/main/java/nsusbloader/Controllers/NSLMainController.java index e4d9c74..569a969 100644 --- a/src/main/java/nsusbloader/Controllers/NSLMainController.java +++ b/src/main/java/nsusbloader/Controllers/NSLMainController.java @@ -191,6 +191,12 @@ public class NSLMainController implements Initializable { uploadStopBtn.getStyleClass().add("buttonUp"); } } + /** + * Crunch. Now you see that I'm not a programmer.. This function called from NSTableViewController + * */ + public void disableUploadStopBtn(){ + uploadStopBtn.setDisable(true); + } /** * Drag-n-drop support (dragOver consumer) * */ diff --git a/src/main/java/nsusbloader/Controllers/NSTableViewController.java b/src/main/java/nsusbloader/Controllers/NSTableViewController.java index 6b5a9da..c11191d 100644 --- a/src/main/java/nsusbloader/Controllers/NSTableViewController.java +++ b/src/main/java/nsusbloader/Controllers/NSTableViewController.java @@ -1,19 +1,21 @@ package nsusbloader.Controllers; +import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Label; -import javafx.scene.control.TableCell; -import javafx.scene.control.TableColumn; -import javafx.scene.control.TableView; +import javafx.scene.control.*; import javafx.scene.control.cell.CheckBoxTableCell; import javafx.scene.control.cell.PropertyValueFactory; import javafx.util.Callback; +import nsusbloader.MediatorControl; import nsusbloader.NSLDataTypes.EFileStatus; import java.io.File; @@ -32,6 +34,7 @@ public class NSTableViewController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { rowsObsLst = FXCollections.observableArrayList(); + table.setPlaceholder(new Label()); table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); @@ -87,7 +90,45 @@ public class NSTableViewController implements Initializable { return cell; } }); + table.setRowFactory( // this shit is made to implement context menu. It's such a pain.. + new Callback, TableRow>() { + @Override + public TableRow call(TableView nslRowModelTableView) { + final TableRow row = new TableRow<>(); + ContextMenu contextMenu = new ContextMenu(); + MenuItem deleteMenuItem = new MenuItem(resourceBundle.getString("contextMenuBtnDelete")); + deleteMenuItem.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent actionEvent) { + rowsObsLst.remove(row.getItem()); + if (rowsObsLst.isEmpty()) + MediatorControl.getInstance().getContoller().disableUploadStopBtn(); + table.refresh(); + } + }); + MenuItem deleteAllMenuItem = new MenuItem(resourceBundle.getString("contextMenuBtnDeleteAll")); + deleteAllMenuItem.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent actionEvent) { + rowsObsLst.clear(); + MediatorControl.getInstance().getContoller().disableUploadStopBtn(); + table.refresh(); + } + }); + contextMenu.getItems().addAll(deleteMenuItem, deleteAllMenuItem); + row.setContextMenu(contextMenu); + row.contextMenuProperty().bind( + Bindings.when( + Bindings.isNotNull( + row.itemProperty())) + .then(MediatorControl.getInstance().getTransferActive()?(ContextMenu)null:contextMenu) + .otherwise((ContextMenu) null) + ); + return row; + } + } + ); table.setItems(rowsObsLst); table.getColumns().addAll(statusColumn, fileNameColumn, fileSizeColumn, uploadColumn); } @@ -108,9 +149,8 @@ public class NSTableViewController implements Initializable { * */ public void setFiles(List files){ rowsObsLst.clear(); // TODO: consider table refresh - if (files == null) { + if (files == null) return; - } if (protocol.equals("TinFoil")){ for (File nspFile: files){ rowsObsLst.add(new NSLRowModel(nspFile, true)); diff --git a/src/main/resources/locale.properties b/src/main/resources/locale.properties index b81bc63..3a61b8f 100644 --- a/src/main/resources/locale.properties +++ b/src/main/resources/locale.properties @@ -19,3 +19,5 @@ tableStatusLbl=Status tableFileNameLbl=File name tableSizeLbl=Size tableUploadLbl=Upload? +contextMenuBtnDelete=Remove +contextMenuBtnDeleteAll=Remove all diff --git a/src/main/resources/locale_rus.properties b/src/main/resources/locale_rus.properties index e215f8e..6869862 100644 --- a/src/main/resources/locale_rus.properties +++ b/src/main/resources/locale_rus.properties @@ -20,4 +20,6 @@ tableUploadLbl=\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044C? tableSizeLbl=\u0420\u0430\u0437\u043C\u0435\u0440 tableFileNameLbl=\u0418\u043C\u044F \u0444\u0430\u0439\u043B\u0430 tableStatusLbl=\u0421\u043E\u0441\u0442\u043E\u044F\u043D\u0438\u0435 +contextMenuBtnDelete=\u0423\u0434\u0430\u043B\u0438\u0442\u044C +contextMenuBtnDeleteAll=\u0423\u0434\u0430\u043B\u0438\u0442\u044C \u0432\u0441\u0451 diff --git a/src/main/resources/locale_ukr.properties b/src/main/resources/locale_ukr.properties index ba9e5d5..4112031 100644 --- a/src/main/resources/locale_ukr.properties +++ b/src/main/resources/locale_ukr.properties @@ -18,4 +18,6 @@ windowBodyConfirmWrongPFS0=\u0412\u0438\u0431\u0440\u0430\u043D\u0438\u0439 \u04 tableStatusLbl=\u0421\u0442\u0430\u043D tableFileNameLbl=\u0406\u043C'\u044F \u0444\u0430\u0439\u043B\u0443 tableSizeLbl=\u0420\u043E\u0437\u043C\u0456\u0440 -tableUploadLbl=\u0417\u0430\u0432\u0430\u043D\u0442\u0430\u0436.? \ No newline at end of file +tableUploadLbl=\u0417\u0430\u0432\u0430\u043D\u0442\u0430\u0436.? +contextMenuBtnDelete=\u0412\u0438\u0434\u0430\u043B\u0438\u0442\u0438 +contextMenuBtnDeleteAll=\u0412\u0438\u0434\u0430\u043B\u0438\u0442\u0438 \u0432\u0441\u0435 \ No newline at end of file