/**
 * Lets you render an ISO date in a table.
 */
package com.mindprod.vercheck;

import com.mindprod.common11.BigDate;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

/**
 * render a date in ISO format yyyy-mm-dd
 */
final class ISODateRenderer extends DefaultTableCellRenderer implements TableCellRenderer
    {
    private final Color foreground;

    private final Font font;

    private final int horizontalAlignment;

    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * constructor
     *
     * @param font                for to render the column
     * @param foreground          foreground colour
     * @param horizontalAlignment e.g. JLabel.CENTER
     */
    @SuppressWarnings( { "SameParameterValue" } )
    public ISODateRenderer( Font font,
                            Color foreground,
                            int horizontalAlignment )
        {
        this.foreground = foreground;
        this.font = font;
        this.horizontalAlignment = horizontalAlignment;
        }

    /**
     * Prepare a JLabel with the relevant icon in it.
     *
     * @param table      the JTable
     * @param value      the Date to display, as a BigDate
     * @param isSelected true if this cell is selected.
     * @param hasFocus   true if this cell has focus
     * @param row        0-based row
     * @param column     0-based column
     * @return JLabel with an Icon in it.
     */
    public Component getTableCellRendererComponent( JTable table,
                                                    Object value,
                                                    boolean isSelected,
                                                    boolean hasFocus,
                                                    int row,
                                                    int column )
        {
        JLabel template = ( JLabel ) super.getTableCellRendererComponent( table, value,
                isSelected, hasFocus, row, column );
        assert value == null || value instanceof BigDate : "not a BigDate: " + value.toString();
        template.setFont( font );
        template.setForeground( foreground );
        // we don't handle setting selected background here.
        // We don't get called when selection changes.
        // leave it up to JTable to set the background to selected or normal.
        template.setHorizontalAlignment( horizontalAlignment );
        if ( value == null )
            {
            template.setText( "" );
            }
        else
            {
            BigDate bdValue = ( BigDate ) value;
            if ( bdValue.getOrdinal() == BigDate.NULL_ORDINAL )
                {
                template.setText( "" );
                }
            else
                {
                template.setText( bdValue.toString() );
                }
            }
        // we don't handle choosing the selected background.
        // JTable uses the selected or normal background.  If we set we override, and don't
        // notice selections fast enough.
        return template;
        }
    }