Fixed AccountSelectionBox.
This commit is contained in:
parent
1a40b78a70
commit
4600470cdb
|
@ -82,6 +82,13 @@ public class EditTransactionController implements RouteSelectionListener {
|
||||||
accounts -> (!accounts.hasCredit() || !accounts.hasDebit()) || !accounts.creditAccount().equals(accounts.debitAccount()),
|
accounts -> (!accounts.hasCredit() || !accounts.hasDebit()) || !accounts.creditAccount().equals(accounts.debitAccount()),
|
||||||
"The credit and debit accounts cannot be the same."
|
"The credit and debit accounts cannot be the same."
|
||||||
)
|
)
|
||||||
|
.addPredicate(
|
||||||
|
accounts -> (
|
||||||
|
(!accounts.hasCredit() || accounts.creditAccount().getCurrency().equals(currencyChoiceBox.getValue())) ||
|
||||||
|
(!accounts.hasDebit() || accounts.debitAccount().getCurrency().equals(currencyChoiceBox.getValue()))
|
||||||
|
),
|
||||||
|
"Linked accounts must use the same currency."
|
||||||
|
)
|
||||||
).validatedInitially().attach(linkedAccountsContainer, linkedAccountsProperty);
|
).validatedInitially().attach(linkedAccountsContainer, linkedAccountsProperty);
|
||||||
|
|
||||||
var formValid = timestampValid.and(amountValid).and(descriptionValid).and(linkedAccountsValid);
|
var formValid = timestampValid.and(amountValid).and(descriptionValid).and(linkedAccountsValid);
|
||||||
|
|
|
@ -1,26 +1,32 @@
|
||||||
package com.andrewlalis.perfin.view.component;
|
package com.andrewlalis.perfin.view.component;
|
||||||
|
|
||||||
|
import com.andrewlalis.perfin.data.util.CurrencyUtil;
|
||||||
import com.andrewlalis.perfin.model.Account;
|
import com.andrewlalis.perfin.model.Account;
|
||||||
|
import com.andrewlalis.perfin.model.MoneyValue;
|
||||||
|
import com.andrewlalis.perfin.model.Profile;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.beans.property.BooleanProperty;
|
import javafx.beans.property.BooleanProperty;
|
||||||
import javafx.beans.property.SimpleBooleanProperty;
|
import javafx.beans.property.SimpleBooleanProperty;
|
||||||
import javafx.scene.control.ComboBox;
|
import javafx.scene.control.ComboBox;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.ListCell;
|
import javafx.scene.control.ListCell;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
|
import javafx.scene.layout.VBox;
|
||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A box that allows the user to select one account from a list of options.
|
* A box that allows the user to select one account from a list of options.
|
||||||
*/
|
*/
|
||||||
public class AccountSelectionBox extends ComboBox<Account> {
|
public class AccountSelectionBox extends ComboBox<Account> {
|
||||||
private final CellFactory cellFactory = new CellFactory();
|
|
||||||
private final BooleanProperty allowNoneProperty = new SimpleBooleanProperty(true);
|
private final BooleanProperty allowNoneProperty = new SimpleBooleanProperty(true);
|
||||||
|
private final BooleanProperty showBalanceProperty = new SimpleBooleanProperty(false);
|
||||||
|
|
||||||
public AccountSelectionBox() {
|
public AccountSelectionBox() {
|
||||||
setCellFactory(cellFactory);
|
setCellFactory(new CellFactory(showBalanceProperty));
|
||||||
setButtonCell(cellFactory.call(null));
|
setButtonCell(new AccountListCell(new SimpleBooleanProperty(false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccounts(List<Account> accounts) {
|
public void setAccounts(List<Account> accounts) {
|
||||||
|
@ -37,7 +43,7 @@ public class AccountSelectionBox extends ComboBox<Account> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void select(Account account) {
|
public void select(Account account) {
|
||||||
setButtonCell(cellFactory.call(null));
|
setButtonCell(new AccountListCell(new SimpleBooleanProperty(false)));
|
||||||
getSelectionModel().select(account);
|
getSelectionModel().select(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,28 +59,63 @@ public class AccountSelectionBox extends ComboBox<Account> {
|
||||||
allowNoneProperty.set(value);
|
allowNoneProperty.set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final BooleanProperty showBalanceProperty() {
|
||||||
|
return showBalanceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final boolean getShowBalance() {
|
||||||
|
return showBalanceProperty.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void setShowBalance(boolean value) {
|
||||||
|
showBalanceProperty.set(value);
|
||||||
|
}
|
||||||
|
|
||||||
private static class CellFactory implements Callback<ListView<Account>, ListCell<Account>> {
|
private static class CellFactory implements Callback<ListView<Account>, ListCell<Account>> {
|
||||||
|
private final BooleanProperty showBalanceProp;
|
||||||
|
|
||||||
|
private CellFactory(BooleanProperty showBalanceProp) {
|
||||||
|
this.showBalanceProp = showBalanceProp;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ListCell<Account> call(ListView<Account> param) {
|
public ListCell<Account> call(ListView<Account> param) {
|
||||||
return new AccountListCell();
|
return new AccountListCell(showBalanceProp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class AccountListCell extends ListCell<Account> {
|
private static class AccountListCell extends ListCell<Account> {
|
||||||
private final Label label = new Label();
|
private final BooleanProperty showBalanceProp;
|
||||||
|
private final Label nameLabel = new Label();
|
||||||
|
private final Label balanceLabel = new Label();
|
||||||
|
|
||||||
public AccountListCell() {
|
public AccountListCell(BooleanProperty showBalanceProp) {
|
||||||
setGraphic(label);
|
this.showBalanceProp = showBalanceProp;
|
||||||
label.getStyleClass().add("normal-color-text-fill");
|
nameLabel.getStyleClass().add("normal-color-text-fill");
|
||||||
|
balanceLabel.getStyleClass().addAll("secondary-color-text-fill", "mono-font", "smallest-font", "italic-text");
|
||||||
|
balanceLabel.managedProperty().bind(balanceLabel.visibleProperty());
|
||||||
|
balanceLabel.setVisible(false);
|
||||||
|
setGraphic(new VBox(nameLabel, balanceLabel));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void updateItem(Account item, boolean empty) {
|
protected void updateItem(Account item, boolean empty) {
|
||||||
super.updateItem(item, empty);
|
super.updateItem(item, empty);
|
||||||
if (item == null || empty) {
|
if (item == null || empty) {
|
||||||
label.setText("None");
|
nameLabel.setText("None");
|
||||||
} else {
|
balanceLabel.setVisible(false);
|
||||||
label.setText(item.getName() + " " + item.getAccountNumberSuffix());
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
nameLabel.setText(item.getName() + " (" + item.getAccountNumberSuffix() + ")");
|
||||||
|
if (showBalanceProp.get()) {
|
||||||
|
Thread.ofVirtual().start(() -> Profile.getCurrent().getDataSource().useAccountRepository(repo -> {
|
||||||
|
BigDecimal balance = repo.deriveCurrentBalance(item.id);
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
balanceLabel.setText(CurrencyUtil.formatMoney(new MoneyValue(balance, item.getCurrency())));
|
||||||
|
balanceLabel.setVisible(true);
|
||||||
|
});
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,11 +43,11 @@
|
||||||
<HBox styleClass="std-padding,std-spacing" fx:id="linkedAccountsContainer">
|
<HBox styleClass="std-padding,std-spacing" fx:id="linkedAccountsContainer">
|
||||||
<VBox>
|
<VBox>
|
||||||
<Label text="Debited Account" labelFor="${debitAccountSelector}" styleClass="bold-text"/>
|
<Label text="Debited Account" labelFor="${debitAccountSelector}" styleClass="bold-text"/>
|
||||||
<AccountSelectionBox fx:id="debitAccountSelector" allowNone="true"/>
|
<AccountSelectionBox fx:id="debitAccountSelector" allowNone="true" showBalance="true"/>
|
||||||
</VBox>
|
</VBox>
|
||||||
<VBox>
|
<VBox>
|
||||||
<Label text="Credited Account" labelFor="${creditAccountSelector}" styleClass="bold-text"/>
|
<Label text="Credited Account" labelFor="${creditAccountSelector}" styleClass="bold-text"/>
|
||||||
<AccountSelectionBox fx:id="creditAccountSelector" allowNone="true"/>
|
<AccountSelectionBox fx:id="creditAccountSelector" allowNone="true" showBalance="true"/>
|
||||||
</VBox>
|
</VBox>
|
||||||
</HBox>
|
</HBox>
|
||||||
<!-- Container for attachments -->
|
<!-- Container for attachments -->
|
||||||
|
|
Loading…
Reference in New Issue