/*
 * ColoredJListRenderer.java
 *
 * Created on 26. listopad 2005, 18:54
 *
 * 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.knapsack;
import cz.pcland.ga.GenerationInfo;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.border.*;
import cz.pcland.gavisualization.*;
/**
 *
 * @author petr
 */
public class ColoredJListRenderer /*extends DefaultListCellRenderer*/ extends JPanel implements ListCellRenderer {
    //JPanel colorPanel;
    JLabel lbl;
    private static final Border EMPTY_BORDER = new EmptyBorder(1, 1, 1, 1);
    
    /** Creates a new instance of CustomListRender */
    public ColoredJListRenderer() {
        lbl = new JLabel();
        //colorPanel = new JPanel();
        setLayout(new FlowLayout(FlowLayout.LEFT));
        
        //this.add(colorPanel);
        this.add(lbl);
    }
    
    
    
    
    
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        
        //super.getListCellRendererComponent(list, value, index,  isSelected, cellHasFocus);
        GenerationInfo GI;
        
        if(value instanceof KnapSackItem) {
            KnapSackItem item = ((KnapSackItem)value);
            //colorPanel.setBackground(item.getColor());
            // colorPanel.setSize(10,10);
            
            if(item.isInSack()){
                lbl.setOpaque(true);
                lbl.setBackground(Color.orange);
                
            }else {
                lbl.setOpaque(false);
            }
            
            if(item.isInApproximate()){
           //     String str = "in approximate, ";
                lbl.setText(item.toString());
            //    lbl.setOpaque(true);
              //  lbl.setBackground(Color.green);
                
            }else {
                lbl.setText(item.toString());
            }
            
            
            
        }else  {
            lbl.setText("Nastala neocekavana chyba ... :( ");
            GI = null;
        }
        
        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;
    }
    
}

