/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Color;
import java.awt.Component;
import java.util.HashSet;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author smuRf
 */
public class ColorRowRenderer{
    private ColorColumnCellRenderer[] columnRenderers;
    
    HashSet<Integer> equiRows = new HashSet();
    HashSet<Integer> baseRows = new HashSet();
 	
   public ColorRowRenderer(int columnCount, Color baseBgColor, Color baseFgColor, Color equiBgColor, Color equiFgColor) 
   {
       columnRenderers = new ColorColumnCellRenderer[columnCount];
       
       for(int i = 0; i < columnCount; i++)
           columnRenderers[i] = new ColorColumnCellRenderer(this, baseBgColor, baseFgColor, equiBgColor, equiFgColor);
   }
   
   public ColorColumnCellRenderer getColumnRenderer(int index)
   {
       return columnRenderers[index];
   }
   
   public void clearEquiRows()
   {
       equiRows.clear();
   }
   
   public void clearBaseRows()
   {
       baseRows.clear();
   }
   
   public void addEquiRows(int[] _rows)
   {
       for(int row:_rows)
           equiRows.add(row);
   }
   
   public boolean containsEquiRow(int row)
   {
       return equiRows.contains(row);
   }
   
   public void addBaseRows(int[] _rows)
   {
       for(int row:_rows)
           baseRows.add(row);
   }
   
   public boolean containsBaseRow(int row)
   {
       return baseRows.contains(row);
   }
}

class ColorColumnCellRenderer extends DefaultTableCellRenderer{
    private Color equiBgColor, equiFgColor;
    private Color baseBgColor, baseFgColor;
    private ColorRowRenderer parent;
    
    public ColorColumnCellRenderer(ColorRowRenderer _parent, Color _baseBgColor, Color _baseFgColor, Color _equiBgColor, Color _equiFgColor)
    {
        super();
        
        parent = _parent;
        baseBgColor = _baseBgColor;
        baseFgColor = _baseFgColor;
        equiBgColor = _equiBgColor;
        equiFgColor = _equiFgColor;
    }
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    {
        if(isSelected)
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        else
        {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if(parent.containsBaseRow(row))
            {
                cell.setBackground(baseBgColor);
                cell.setForeground(baseFgColor);
            }
            else if(parent.containsEquiRow(row))
            {
                cell.setBackground(equiBgColor);
                cell.setForeground(equiFgColor);
            }
            else
            {
                if(row%2 == 0)
                    cell.setBackground(Color.white);
                else
                    cell.setBackground(new Color(242, 242, 242));

                cell.setForeground(Color.black);
            }
            return cell;
        }
    }
}
