com.daarons.control.DeleteTableCell.java Source code

Java tutorial

Introduction

Here is the source code for com.daarons.control.DeleteTableCell.java

Source

/*
 * Copyright 2016 David Aarons.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daarons.control;

import com.daarons.controller.CsvController;
import com.daarons.controller.TransferSettingsController;
import com.daarons.springconfig.SpringConfig;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Cell;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableRow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;

/**
 *
 * @author David
 */
public class DeleteTableCell extends TableCell<String, String> {

    private static final Logger log = LogManager.getLogger(DeleteTableCell.class);
    private Button deleteButton;
    private final Image TRASHCAN_IMAGE = new Image(getClass().getResourceAsStream("/images/trashcan.png"));

    public DeleteTableCell() {
        deleteButton = new Button();
        deleteButton.setOnAction(event -> {
            Alert deleteAlert = new Alert(Alert.AlertType.CONFIRMATION,
                    "Are you " + "sure that you want to delete this?");
            deleteAlert.showAndWait().ifPresent(response -> {
                if (response == ButtonType.OK) {
                    ApplicationContext applicationContext = SpringConfig.getApplicationContext();
                    manageSelectionBehavior();
                    String tableViewId = getTableView().getId();
                    if (tableViewId.equals("csvTableView")) {
                        applicationContext.getBean(CsvController.class).removeSelectedTransferRecords();
                    } else if (tableViewId.equals("uploadTableView")) {
                        applicationContext.getBean(TransferSettingsController.class)
                                .removeSelectedUploadTransferObjects();
                    } else if (tableViewId.equals("downloadTableView")) {
                        applicationContext.getBean(TransferSettingsController.class)
                                .removeSelectedDownloadTransferObjects();
                    }
                }
            });
        });
    }

    private void manageSelectionBehavior() {
        TableRow row = ((TableRow) ((Cell) deleteButton.getParent()).getParent());
        int numberOfSelectedRows = getTableView().getSelectionModel().getSelectedIndices().size();
        if ((numberOfSelectedRows < 2) || (numberOfSelectedRows >= 2 && !isRowSelected(row))) {
            getTableView().getSelectionModel().clearSelection();
            getTableView().getSelectionModel().select(row.getIndex());
        }
    }

    @Override
    public void updateItem(String delete, boolean empty) {
        super.updateItem(delete, empty);
        if (empty) {
            setGraphic(null);
        } else {
            deleteButton.setGraphic(new ImageView(TRASHCAN_IMAGE));
            setGraphic(deleteButton);
        }
    }

    private boolean isRowSelected(TableRow row) {
        return getTableView().getSelectionModel().getSelectedIndices().contains(row.getIndex());
    }
}