/*
 * CustomListRender.java
 *
 * Created on November 24, 2005, 2:32 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package cz.pcland.sat;
import cz.pcland.ga.BinaryIndividual;
import cz.pcland.ga.GenerationInfo;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.border.*;

/**
 *
 * @author petr
 */
public class SATListRender /*extends DefaultListCellRenderer*/ extends JPanel implements ListCellRenderer {

    private JLabel lbl;
    private static final Border EMPTY_BORDER = new EmptyBorder(1, 1, 1, 1);
    SATInstance instance;
    
    /** Creates a new instance of CustomListRender */
    public SATListRender() {
        lbl = new JLabel();
        setLayout(new FlowLayout(FlowLayout.LEFT));
        this.add(lbl);
                    
    }
    
    public void setInstance(SATInstance instance){
        this.instance = instance;
    }
    
    
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        GenerationInfo GI;
        
        if(value instanceof GenerationInfo) {
            GI = ((GenerationInfo)value);
            
            lbl.setText( GI.toString() + ", <br>" + instance.getEvaluatedInstance((BinaryIndividual)GI.getBestString()) + "</html>");
            
        } else {
            lbl.setText("Nastala neocekavana chyba ... :( ");
        }
        
        if (cellHasFocus)
            setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));
        else
            setBorder(EMPTY_BORDER);
        
        
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        
        return this;
    }
    
}
