Skip to content

Commit

Permalink
Disable delete button if no item is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinKlar committed Jul 6, 2024
1 parent 806f75b commit 7dd8d8e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/link/biosmarcel/baka/view/PaymentDetails.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package link.biosmarcel.baka.view;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.scene.control.Button;
Expand All @@ -22,6 +24,7 @@
public class PaymentDetails extends VBox {
public final ObjectProperty<@Nullable PaymentFX> activePayment = new SimpleObjectProperty<>();
public final BooleanProperty disableComponents = new SimpleBooleanProperty(true);
private final BooleanBinding disableDelete; // Do not inline, it will get garbage collected!
private final ApplicationState state;

public PaymentDetails(final ApplicationState state) {
Expand All @@ -31,7 +34,8 @@ public PaymentDetails(final ApplicationState state) {
classificationsTable.setEditable(true);

final TableColumn<Classification, BigDecimal> amountColumn = new TableColumn<>("Amount");
final Callback<TableColumn<Classification, @Nullable BigDecimal>, TableCell<Classification, @Nullable BigDecimal>> amountColumnCellFactory = __ ->
final Callback<TableColumn<Classification, @Nullable BigDecimal>, TableCell<Classification, @Nullable BigDecimal>>
amountColumnCellFactory = _ ->
new TextFieldTableCell<>(new StringConverter<BigDecimal>() {
@Override
public @Nullable String toString(final @Nullable BigDecimal value) {
Expand All @@ -58,7 +62,8 @@ public PaymentDetails(final ApplicationState state) {
});

final TableColumn<Classification, String> tagColumn = new TableColumn<>("Tag");
final Callback<TableColumn<Classification, @Nullable String>, TableCell<Classification, @Nullable String>> simpleStringColumnFactory = __ ->
final Callback<TableColumn<Classification, @Nullable String>, TableCell<Classification, @Nullable String>>
simpleStringColumnFactory = _ ->
new TextFieldTableCell<>(new StringConverter<String>() {
@Override
public @Nullable String toString(final @Nullable String string) {
Expand Down Expand Up @@ -113,10 +118,14 @@ public PaymentDetails(final ApplicationState state) {
});

final var deleteButton = new Button("Delete");
final ReadOnlyObjectProperty<@Nullable Classification> selectedClassification = classificationsTable.getSelectionModel().selectedItemProperty();
selectedClassification.addListener((_, _, newValue) -> {
deleteButton.setDisable(newValue == null);
});
final ReadOnlyObjectProperty<@Nullable Classification> selectedClassifiction =
classificationsTable.getSelectionModel().selectedItemProperty();
disableDelete = Bindings.createBooleanBinding(
() -> disableComponents.get() || selectedClassifiction.get() == null,
disableComponents, selectedClassifiction);
deleteButton.disableProperty().bind(disableDelete);
final ReadOnlyObjectProperty<@Nullable Classification> selectedClassification =
classificationsTable.getSelectionModel().selectedItemProperty();
deleteButton.setOnAction(_ -> {
final var selected = Objects.requireNonNull(selectedClassification.get());
final var payment = Objects.requireNonNull(activePayment.get());
Expand Down

0 comments on commit 7dd8d8e

Please sign in to comment.