Unify Table Behavior

This commit is contained in:
2026-04-09 10:22:51 -06:00
parent 70c883b01b
commit 0cc160a02c
12 changed files with 62 additions and 15 deletions

View File

@@ -16,6 +16,7 @@ import org.example.petshopdesktop.api.endpoints.AdoptionApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.AdoptionDialogController;
import org.example.petshopdesktop.models.Adoption;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.Comparator;
@@ -80,6 +81,7 @@ public class AdoptionController {
colAdoptionStatus.setCellValueFactory(new PropertyValueFactory<>("adoptionStatus"));
displayAdoptions();
TableViewSupport.installDoubleClickAction(tvAdoptions, selected -> openDialog(selected, "Edit"));
tvAdoptions.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {

View File

@@ -18,6 +18,7 @@ import org.example.petshopdesktop.api.dto.appointment.AppointmentResponse;
import org.example.petshopdesktop.api.endpoints.AppointmentApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.AppointmentDialogController;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.util.List;
import java.util.Comparator;
@@ -61,7 +62,8 @@ public class AppointmentController {
colAppointmentStatus.setCellValueFactory(new PropertyValueFactory<>("appointmentStatus"));
filtered = new FilteredList<>(appointments, a -> true);
tvAppointments.setItems(filtered);
TableViewSupport.bindSortedItems(tvAppointments, filtered);
TableViewSupport.installDoubleClickAction(tvAppointments, selected -> openDialog(selected, "Edit"));
if (txtSearch != null) {
txtSearch.textProperty().addListener((obs, o, n) -> applyFilter(n));

View File

@@ -16,6 +16,7 @@ import org.example.petshopdesktop.api.endpoints.InventoryApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.InventoryDialogController;
import org.example.petshopdesktop.models.Inventory;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.List;
@@ -70,6 +71,7 @@ public class InventoryController {
colQuantity.setCellValueFactory(new PropertyValueFactory<>("quantity"));
displayInventory();
TableViewSupport.installDoubleClickAction(tvInventory, selected -> openDialog(selected, "Edit"));
tvInventory.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {

View File

@@ -22,6 +22,7 @@ import org.example.petshopdesktop.controllers.dialogcontrollers.PetDialogControl
import org.example.petshopdesktop.models.Pet;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.DesktopImageSupport;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.List;
@@ -172,6 +173,7 @@ public class PetController {
cbStatusFilter.getSelectionModel().selectFirst();
displayPets();
TableViewSupport.installDoubleClickAction(tvPets, selected -> openDialog(selected, "Edit"));
tvPets.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {

View File

@@ -22,6 +22,7 @@ import org.example.petshopdesktop.api.endpoints.ProductApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.ProductDialogController;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.DesktopImageSupport;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.ArrayList;
@@ -95,6 +96,7 @@ public class ProductController {
loadCategoryFilter();
displayProduct();
TableViewSupport.installDoubleClickAction(tvProducts, selected -> openDialog(selected, "Edit"));
//EventListener to Enable buttons when a row is selected
tvProducts.getSelectionModel().selectedItemProperty().addListener(

View File

@@ -16,6 +16,7 @@ import org.example.petshopdesktop.api.dto.productsupplier.ProductSupplierRespons
import org.example.petshopdesktop.api.endpoints.ProductSupplierApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.ProductSupplierDialogController;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.List;
@@ -76,6 +77,7 @@ public class ProductSupplierController {
colCost.setCellValueFactory(new PropertyValueFactory<ProductSupplierDTO,Double>("cost"));
displayProductSupplier();
TableViewSupport.installDoubleClickAction(tvProductSuppliers, selected -> openDialog(selected, "Edit"));
//EventListener to Enable buttons when a row is selected
tvProductSuppliers.getSelectionModel().selectedItemProperty().addListener(

View File

@@ -11,6 +11,7 @@ import org.example.petshopdesktop.DTOs.PurchaseOrderDTO;
import org.example.petshopdesktop.api.dto.purchaseorder.PurchaseOrderResponse;
import org.example.petshopdesktop.api.endpoints.PurchaseOrderApi;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.util.List;
import java.util.Comparator;
@@ -49,7 +50,7 @@ public class PurchaseOrderController {
new PropertyValueFactory<>("status"));
filtered = new FilteredList<>(purchaseOrders, p -> true);
tvPurchaseOrders.setItems(filtered);
TableViewSupport.bindSortedItems(tvPurchaseOrders, filtered);
if (txtSearch != null) {
txtSearch.textProperty().addListener((obs, o, n) -> applyFilter(n));
@@ -69,7 +70,6 @@ public class PurchaseOrderController {
Platform.runLater(() -> {
purchaseOrders.setAll(dtos);
tvPurchaseOrders.setItems(filtered);
});
} catch (Exception e) {
Platform.runLater(() -> {

View File

@@ -7,7 +7,6 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.application.Platform;
import javafx.scene.input.MouseButton;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
@@ -37,6 +36,7 @@ import org.example.petshopdesktop.models.SaleCartItem;
import org.example.petshopdesktop.models.SaleDetail;
import org.example.petshopdesktop.models.SaleLineItem;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.math.BigDecimal;
import java.text.NumberFormat;
@@ -168,16 +168,8 @@ public class SaleController {
colSalePaymentType.setCellValueFactory(new PropertyValueFactory<>("paymentMethod"));
filteredSales = new FilteredList<>(saleItems, s -> true);
tvSales.setItems(filteredSales);
tvSales.setOnMouseClicked(event -> {
if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
SaleLineItem selected = tvSales.getSelectionModel().getSelectedItem();
if (selected != null) {
openSaleDetailDialog(selected.getSaleId());
}
}
});
TableViewSupport.bindSortedItems(tvSales, filteredSales);
TableViewSupport.installDoubleClickAction(tvSales, selected -> openSaleDetailDialog(selected.getSaleId()));
txtSearch.textProperty().addListener((obs, oldVal, newVal) -> applySalesFilter(newVal));
}

View File

@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.dto.service.ServiceResponse;
import org.example.petshopdesktop.api.endpoints.ServiceApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import javafx.stage.Modality;
import java.util.List;
@@ -54,6 +55,7 @@ public class ServiceController {
colServicePrice.setCellValueFactory(new PropertyValueFactory<>("servicePrice"));
displayServices();
TableViewSupport.installDoubleClickAction(tvServices, selected -> openDialog(selected, "Edit"));
tvServices.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {

View File

@@ -21,6 +21,7 @@ import org.example.petshopdesktop.api.endpoints.EmployeeApi;
import org.example.petshopdesktop.auth.UserSession;
import org.example.petshopdesktop.models.StaffAccount;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.sql.Timestamp;
import java.time.ZoneId;
@@ -76,7 +77,8 @@ public class StaffAccountsController {
colCreated.setCellValueFactory(new PropertyValueFactory<>("createdAt"));
filtered = new FilteredList<>(staffAccounts, a -> true);
tvStaff.setItems(filtered);
TableViewSupport.bindSortedItems(tvStaff, filtered);
TableViewSupport.installDoubleClickAction(tvStaff, this::openEditDialog);
txtSearch.textProperty().addListener((obs, o, n) -> applyFilter(n));
@@ -131,6 +133,10 @@ public class StaffAccountsController {
void btnEditAccountClicked(ActionEvent event) {
lblError.setText("");
StaffAccount selected = tvStaff.getSelectionModel().getSelectedItem();
openEditDialog(selected);
}
private void openEditDialog(StaffAccount selected) {
if (selected == null) {
lblError.setText("Select a staff account to edit.");
return;

View File

@@ -16,6 +16,7 @@ import org.example.petshopdesktop.api.endpoints.SupplierApi;
import org.example.petshopdesktop.controllers.dialogcontrollers.SupplierDialogController;
import org.example.petshopdesktop.models.Supplier;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TableViewSupport;
import java.io.IOException;
import java.util.List;
@@ -78,6 +79,7 @@ public class SupplierController {
colSupplierPhone.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supPhone"));
displaySupplier();
TableViewSupport.installDoubleClickAction(tvSuppliers, selected -> openDialog(selected, "Edit"));
//EventListener to Enable buttons when a row is selected
tvSuppliers.getSelectionModel().selectedItemProperty().addListener(

View File

@@ -0,0 +1,33 @@
package org.example.petshopdesktop.util;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseButton;
import java.util.function.Consumer;
public final class TableViewSupport {
private TableViewSupport() {
}
public static <T> void bindSortedItems(TableView<T> tableView, FilteredList<T> filteredItems) {
SortedList<T> sortedItems = new SortedList<>(filteredItems);
sortedItems.comparatorProperty().bind(tableView.comparatorProperty());
tableView.setItems(sortedItems);
}
public static <T> void installDoubleClickAction(TableView<T> tableView, Consumer<T> action) {
tableView.setRowFactory(tv -> {
TableRow<T> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2 && !row.isEmpty()) {
action.accept(row.getItem());
}
});
return row;
});
}
}