package com.ht.ui.controls.cell;

import java.net.URL;
import java.text.Format;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import javafx.scene.control.TableCell;
import javafx.scene.text.TextAlignment;

import org.javafxdata.control.cell.CSSTableCell;
import org.javafxdata.control.cell.LabelTableCell;

/**
 * <p>A class containing a {@link TableCell} implementation for a money cell,
 * which can format numbers such that they appear as to represent a currency. 
 * 
 * <img src="http://www.javafxdata.org/screenshots/MoneyCell-TableView.PNG" 
 * style="float: right; padding: 0px 0px 0px 3px;"/>
 *  
 * <p>Note that this class extends {@link CSSTableCell}, which allows for
 * easy CSS styling. This class adds the <code>money-cell</code> style class. 
 * In addition, it adds two pseudo class states:
 * <code>negative</code> and <code>positive</code>. Using these, it is possible
 * to provide CSS styling to customise the look of the cell. For example, the
 * default CSS for the Money*Cell implementations is the following: 
 * 
 * <pre>
 * {@code
 * .money-cell {
 *   -fx-text-fill: -fx-text-inner-color;
 * }
 *
 * .money-cell:selected {
 *   -fx-text-fill: white;
 * }
 * 
 * .money-cell:positive {
 *   -fx-text-fill: green;
 * }
 * 
 * .money-cell:negative {
 *   -fx-text-fill: red;
 * }}
 * </pre>
 * 
 * <p>You may customise this CSS in your own applications by providing your own
 * stylesheet that overrides these values.
 * 
 * @author <a href="http://www.jonathangiles.net">Jonathan Giles</a>
 */
public class NumberTableCell<S,T extends Number> extends LabelTableCell<S,T> {
    
	public NumberTableCell(TextAlignment align, Format format) {
		super(align, format);
        this.getStyleClass().add("number-cell");
	}

	/** {@inheritDoc} */
    @Override protected String getUserAgentStylesheet() {
        URL resource = getClass().getResource("cells.css");
        if (resource != null) {
            return resource.toString();
        }
        return null;
    }
    
    /** {@inheritDoc} */
    @Override public void getCssState(List<String> s) {
        NumberCell.getCssState(this, s);
    }
    
    /** {@inheritDoc} */
    @Override public String toString(T item) {
        String str = item == null ? "" : item.toString();
        
        stateChanged(NumberCellFactory.NEGATIVE_STATE, NumberCellFactory.POSITIVE_STATE);
        
        return str;
    };
}