Search code examples
javafxjavafx-8

JavaFX: Custom TableViewSelectionModel


I have a TableView which lists several addresses as a preview. The selected address can be edited in a form next to the TableView. In case that the user has made changes to the selected address and changes the selected address in the TableView, I want to ask him if he wants to proceed without saving changes. If no, I want the Tableview to stay at the currently selected position, if the user wants to discard the changes the TableView may perform the selection change.

My approach would have been to extend the SelectionModel of the TableView and only allow the selection to change if the user answers the described dialog or there are no changes. Basically I only want to add an if-clause before changing the selection in the Tableview. Unfortunately, I only found the abstract class “TableViewSelectionModel” and no concrete implementations of it.

Is there any way to obtain this desired behavior without implementing the whole class?


Solution

  • Solved it with James_D's recommendation:

    public class ControllableTableSelectionModel<T> extends TableViewSelectionModel<T>{
    
        private TableViewSelectionModel<T> originalModel;
        private ControlsTableView controller;
    
        public ControllableTableSelectionModel(TableViewSelectionModel<T> originalModel, ControlsTableView controller) {
            super(originalModel.getTableView());
            this.originalModel=originalModel;
            this.controller=controller;
        }
    
        @Override
        public boolean isSelected(int row, TableColumnBase<T, ?> column) {
            return originalModel.isSelected(row, column);
        }
    
        @Override
        public void selectLeftCell() {
            if(controller.canSelect(this))
            {
                originalModel.selectLeftCell();
            }
        }
    
        @Override
        public void selectRightCell() {
            if(controller.canSelect(this))
            {
                originalModel.selectRightCell();
            }
        }
    
        @Override
        public void selectAboveCell() {
            if(controller.canSelect(this))
            {
                originalModel.selectAboveCell();
            }
    
        }
    
        @Override
        public void selectBelowCell() {
            if(controller.canSelect(this))
            {
                originalModel.selectBelowCell();
            }
    
        }
    
        @Override
        public void selectRange(int minRow, TableColumnBase<T, ?> minColumn, int maxRow,
                TableColumnBase<T, ?> maxColumn) {
            if(controller.canSelect(this))
            {
                originalModel.selectRange(minRow, minColumn, maxRow, maxColumn);
            }
    
        }
    
        @Override
        protected int getItemCount() {
            return originalModel.getTableView().getItems().size();
        }
    
        @Override
        protected T getModelItem(int index) {
            return originalModel.getTableView().getItems().get(index);
        }
    
        @Override
        public void focus(int index) {
            originalModel.getTableView().getFocusModel().focus(index);      
        }
    
        @Override
        public int getFocusedIndex() {
            return originalModel.getTableView().getFocusModel().getFocusedIndex();
        }
    
        @Override
        public ObservableList<TablePosition> getSelectedCells() {
            return originalModel.getSelectedCells();
        }
    
        @Override
        public boolean isSelected(int row, TableColumn<T, ?> column) {
            return originalModel.isSelected(row, column);
        }
    
        @Override
        public void select(int row, TableColumn<T, ?> column) {
            if(controller.canSelect(this))
            {
                originalModel.select(row, column);
            }       
        }
    
        @Override
        public void clearAndSelect(int row, TableColumn<T, ?> column) {
            if(controller.canSelect(this))
            {
                originalModel.clearAndSelect(row, column);
            }       
        }
    
        @Override
        public void clearSelection(int row, TableColumn<T, ?> column) {
            if(controller.canSelect(this))
            {
                originalModel.clearSelection(row, column);
            }   
        }
    
    }