/*
 * Created on 30-03-2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package net.sf.hibern8ide;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import net.sf.hibern8ide.ErrorListModel.ErrorItem;

/**
 * @author max
 *
 */
public class ErrorCellRenderer
    extends DefaultListCellRenderer
    implements ListCellRenderer {

    /* (non-Javadoc)
     * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
     */
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
        JLabel label =
            (JLabel) super.getListCellRendererComponent(
                list,
                value,
                index,
                isSelected,
                cellHasFocus);

        if (value instanceof ErrorListModel.ErrorItem) {
            ErrorListModel.ErrorItem ei = (ErrorItem) value;
            String t = ei.message;
            t  = "<html>" + t;
            t.replaceAll("\n","<br>");
            label.setText(ei.message);
            
            if (ei.level == ErrorListModel.ERROR) {
                label.setBackground(Color.red);
            } else {
                label.setBackground(list.getBackground());
            }
        }

        //          ---- begin optimization to avoid painting background ---- (stolen from defaultcellrender)
        // If this is not present will the background sometimes be painted and sometimes it will flicker.
        Color back = getBackground();
        boolean colorMatch =
            (back != null)
                && (back.equals(list.getBackground()))
                && list.isOpaque();
                setOpaque(!colorMatch);
        // ---- end optimization to aviod painting background ----

        return label;
    }

}
