package org.javafxdata.control.cell;

import javafx.beans.value.ObservableValue;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TableCell;

/**
 * <p>A class containing a {@link TableCell} implementation that draws a {@link ProgressBar}
 * node inside the cell.
 * 
 * <img src="http://www.javafxdata.org/screenshots/ProgressBarCell-TableView.PNG" 
 * style="float: right; padding: 0px 0px 0px 3px;"/>
 * 
 * @param <S> The type of the elements contained within the TableView.
 * @author <a href="http://www.jonathangiles.net">Jonathan Giles</a>
 */
public class ProgressBarTableCell<S,Double> extends TableCell<S, Double> {
    
    private final ProgressBar progressBar;
    private ObservableValue observable;
    
    /**
     * Creates a default {@link ProgressBarTableCell} instance
     */
    public ProgressBarTableCell() {
        this.getStyleClass().add("progress-bar-table-cell");
        
        this.progressBar = new ProgressBar();
        setGraphic(progressBar);
    }
    
    /** {@inheritDoc} */
    @Override public void updateItem(Double item, boolean empty) {
        super.updateItem(item, empty);
        
        if (empty) {
            setGraphic(null);
        } else {
            progressBar.progressProperty().unbind();
            
            observable = getTableColumn().getCellObservableValue(getIndex());
            if (observable != null) {
                progressBar.progressProperty().bind(observable);
            } else {
                // FIXME Compiler complains here
//                progressBar.setProgress(item);
            }
            
            setGraphic(progressBar);
        }
    }
}