/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2014, Geomatys
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotoolkit.gui.javafx.util;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.Border;
import javafx.util.Callback;
import org.geotoolkit.internal.GeotkFX;
/**
*
* @author Johann Sorel (Geomatys)
*/
public class FXDeleteTableColumn extends TableColumn{
public FXDeleteTableColumn(final boolean showWarning) {
super("Suppression");
setSortable(false);
setResizable(false);
setEditable(false);
setPrefWidth(24);
setMinWidth(24);
setMaxWidth(24);
setGraphic(new ImageView(GeotkFX.ICON_DELETE));
setCellValueFactory(new Callback<TableColumn.CellDataFeatures, ObservableValue>() {
@Override
public ObservableValue call(TableColumn.CellDataFeatures param) {
return new SimpleObjectProperty<>(param.getValue());
}
});
setCellFactory(new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(TableColumn param) {
return new DeleteTableCell(showWarning);
}
});
}
private static final class DeleteTableCell extends TableCell<Object, Object>{
public final Button button = new Button(null,new ImageView(GeotkFX.ICON_DELETE));
public DeleteTableCell(final boolean showWarning) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setAlignment(Pos.CENTER);
button.setBackground(Background.EMPTY);
button.setBorder(Border.EMPTY);
button.setPadding(Insets.EMPTY);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(isEmpty()) return;
final Object rowItem = getTableRow().getItem();
if(showWarning){
final ButtonType res = new Alert(Alert.AlertType.CONFIRMATION,"Confirmer la suppression ?",
ButtonType.NO, ButtonType.YES).showAndWait().get();
if(ButtonType.YES != res) return;
}
getTableView().getItems().remove(rowItem);
}
});
}
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : button);
}
}
}