/*
 * SortIconCellRenderer.java
 *
 * Created on May 24, 2006, 12:19 PM
 *
 */
package usda.weru.wmrm;

import com.klg.jclass.cell.JCCellInfo;
import com.klg.jclass.cell.renderers.JCStringCellRenderer;
import com.klg.jclass.table.Sort;
import com.klg.jclass.table.TableCellInfoModel;
import com.klg.jclass.util.swing.JCIconCreator;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;

/**
 *
 * @author joelevin
 */
public class SortIconCellRenderer extends JCStringCellRenderer {

    private static final long serialVersionUID = 1L;

    private static JCIconCreator c_creator = null;
    private static Image c_asc_image = null;
    private static Image c_desc_image = null;
    private int[] c_columns;
    private int[] c_directions;

    private final static String[] asc_pixels = {
        ".............",
        ".............",
        ".............",
        ".............",
        ".............",
        "..ddddddddd..",
        "...d.....l...",
        "....d...l....",
        ".....d.l.....",
        "......l......",
        ".............",
        ".............",
        "............."
    };

    private final static String[] desc_pixels = {
        ".............",
        ".............",
        ".............",
        ".............",
        ".............",
        "......d......",
        ".....d.l.....",
        "....d...l....",
        "...d.....l...",
        "..dllllllll..",
        ".............",
        ".............",
        "............."
    };

    /** Creates a new instance of SortIconCellRenderer */
    public SortIconCellRenderer() {
        super();
    }

    @Override
    public void draw(Graphics g, JCCellInfo cellInfo, Object o, boolean selected) {
        super.draw(g, cellInfo, o, selected);
        TableCellInfoModel info;
        if (cellInfo instanceof TableCellInfoModel) {
            info = (TableCellInfoModel) cellInfo;
        } else {
            return;
        }

        if (isSorted(info.getColumn())) {
            //Make the icons if needed
            if (c_creator == null) {
                c_creator = new JCIconCreator();
                c_creator.setColor('l', Color.white);
                c_creator.setColor('b', info.getBackground());
                c_creator.setColor('d', info.getBackground().darker());

                c_creator.setSize(asc_pixels.length, asc_pixels.length);
                c_creator.setPixels(asc_pixels);
                c_asc_image = c_creator.getIcon().getImage();

                c_creator.setSize(desc_pixels.length, desc_pixels.length);
                c_creator.setPixels(desc_pixels);
                c_desc_image = c_creator.getIcon().getImage();
            }
            FontMetrics fm = g.getFontMetrics();
            int x = 5;
//            int y = fm.getAscent();
            int direction = getDirection(info.getColumn());
            if (direction == Sort.ASCENDING) {
                g.drawImage(c_asc_image, x, (fm.getAscent() - asc_pixels.length) / 2, null);
            } else if (direction == Sort.DESCENDING) {
                g.drawImage(c_desc_image, x, (fm.getAscent() - asc_pixels.length) / 2, null);

            }
        }
    }

    /**
     *
     * @param directions
     */
    public void setDirection(int... directions) {
        c_directions = directions != null ? directions.clone() : null;
    }

    /**
     *
     * @param columns
     */
    public void setColumns(int... columns) {
        c_columns = columns != null ? columns.clone() : null;
    }

    private boolean isSorted(int columnIndex) {
        if (c_columns == null) {
            return false;
        }
        for (int c : c_columns) {
            if (c == columnIndex) {
                return true;
            }
        }
        return false;
    }

    private int getDirection(int columnIndex) {
        if (c_columns == null || c_directions == null) {
            return -999;
        }
        for (int i = 0; i < c_columns.length; i++) {
            if (c_columns[i] == columnIndex) {
                return c_directions[i];
            }
        }
        return -999;
    }

}
