Implemented removing accounts

This commit is contained in:
dankito 2020-05-24 20:51:39 +02:00
parent 14ef4f8797
commit 69ed44491c
4 changed files with 76 additions and 2 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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<Account>) : TreeView<String>(AccountsRootTreeItem(accounts))
open class AccountsTreeView(accounts: ObservableList<Account>, protected val presenter: BankingPresenter)
: TreeView<String>(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)
}
}
}

View File

@ -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 {