diff --git a/ui/BankingJavaFxApp/src/main/resources/Messages.properties b/ui/BankingJavaFxApp/src/main/resources/Messages.properties index 507a7763..a5436602 100755 --- a/ui/BankingJavaFxApp/src/main/resources/Messages.properties +++ b/ui/BankingJavaFxApp/src/main/resources/Messages.properties @@ -18,6 +18,8 @@ main.window.menu.file.quit=Quit accounts.view.all.accounts=All accounts accounts.view.context.menu.info=Info +accounts.view.context.menu.delete.account=Delete +accounts.view.ask.really.delete.account=Really delete account \'%s\'?\r\n\r\nThis cannot be undone and data will be lost. account.transactions.control.view.search.label=Find: diff --git a/ui/BankingJavaFxApp/src/main/resources/Messages_de.properties b/ui/BankingJavaFxApp/src/main/resources/Messages_de.properties index 8bf3836b..e53a9a05 100755 --- a/ui/BankingJavaFxApp/src/main/resources/Messages_de.properties +++ b/ui/BankingJavaFxApp/src/main/resources/Messages_de.properties @@ -18,6 +18,8 @@ main.window.menu.file.quit=Beenden accounts.view.all.accounts=Alle Konten accounts.view.context.menu.info=Info +accounts.view.context.menu.delete.account=Entfernen +accounts.view.ask.really.delete.account=Möchten Sie das Konto \'%s\' wirklich löschen?\r\n\r\nDies kann nicht rückgängig gemacht werden und die hierzu gespeicherten Daten gehen unwiederbringlich verloren. account.transactions.control.view.search.label=Suchen: diff --git a/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsTreeView.kt b/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsTreeView.kt index dc7bd428..a2d55527 100644 --- a/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsTreeView.kt +++ b/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsTreeView.kt @@ -1,9 +1,79 @@ package net.dankito.banking.ui.javafx.controls import javafx.collections.ObservableList +import javafx.scene.control.Alert +import javafx.scene.control.ButtonType +import javafx.scene.control.ContextMenu import javafx.scene.control.TreeView +import javafx.scene.input.ContextMenuEvent +import javafx.scene.input.KeyCode +import net.dankito.banking.ui.javafx.dialogs.JavaFxDialogService +import net.dankito.banking.ui.javafx.model.AccountsAccountTreeItem import net.dankito.banking.ui.javafx.model.AccountsRootTreeItem import net.dankito.banking.ui.model.Account +import net.dankito.banking.ui.presenter.BankingPresenter +import tornadofx.* +import tornadofx.FX.Companion.messages -open class AccountsTreeView(accounts: ObservableList) : TreeView(AccountsRootTreeItem(accounts)) \ No newline at end of file +open class AccountsTreeView(accounts: ObservableList, protected val presenter: BankingPresenter) + : TreeView(AccountsRootTreeItem(accounts)) { + + protected var currentMenu: ContextMenu? = null + + + init { + setupUi() + } + + + protected open fun setupUi() { + setOnContextMenuRequested { event -> showContextMenu(event) } + + setOnKeyReleased { event -> + if (event.code == KeyCode.DELETE) { + (selectionModel.selectedItem as? AccountsAccountTreeItem)?.let { + askIfAccountShouldBeDeleted(it) + } + } + } + } + + + protected open fun showContextMenu(event: ContextMenuEvent) { + currentMenu?.hide() + + (selectionModel.selectedItem as? AccountsAccountTreeItem)?.let { + currentMenu = createContextMenuForItems(it) + currentMenu?.show(this, event.screenX, event.screenY) + } + } + + protected open fun createContextMenuForItems(selectedItem: AccountsAccountTreeItem): ContextMenu { + val contextMenu = ContextMenu() + + contextMenu.apply { + item(messages["accounts.view.context.menu.delete.account"]) { + action { + askIfAccountShouldBeDeleted(selectedItem) + } + } + } + + return contextMenu + } + + protected open fun askIfAccountShouldBeDeleted(treeItem: AccountsAccountTreeItem) { + val account = treeItem.account + + val selectedButton = JavaFxDialogService().showDialog( + Alert.AlertType.WARNING, + String.format(messages["accounts.view.ask.really.delete.account"], account.displayName), + null, FX.primaryStage, ButtonType.YES, ButtonType.NO) + + if (selectedButton == ButtonType.YES) { + presenter.deleteAccount(account) + } + } + +} \ No newline at end of file diff --git a/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsView.kt b/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsView.kt index 44b0e799..c4eece91 100644 --- a/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsView.kt +++ b/ui/BankingJavaFxControls/src/main/kotlin/net/dankito/banking/ui/javafx/controls/AccountsView.kt @@ -51,7 +51,7 @@ open class AccountsView(protected val presenter: BankingPresenter) : View() { } } - add(AccountsTreeView(accounts).apply { + add(AccountsTreeView(accounts, presenter).apply { selectionModel.selectedItemProperty().addListener { _, _, newValue -> selectedBankAccountChanged(newValue) } vboxConstraints {