package com.xoetrope.svgscanner;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import net.xoetrope.xui.style.XStyle;
import com.xoetrope.swing.list.XAltListCellRenderer;
import java.awt.Component;
import javax.swing.JList;
/**
*
* <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
* the GNU Public License (GPL), please see license.txt for more details. If
* you make commercial use of this software you must purchase a commercial
* license from Xoetrope.</p>
* <p>$Revision: 1.3 $</p>
*/
public class ColorListCellRenderer extends XAltListCellRenderer
{
/** Creates a new instance of ColorListCellRenderer */
public ColorListCellRenderer()
{
}
/**
* Render the cell by drawing the text and then two colour swatches and the font size
* @param g
*/
public void paintComponent( Graphics g )
{
Dimension b = getSize();
g.setColor( getBackground() );
g.fillRect( 0, 0, b.width, b.height );
int d = b.height;
String s = getText();
Color clr = (Color)XStyle.parseColor( s );
g.setColor( clr );
g.fillRect( 2, 2, d -4, d - 5 );
g.setColor( getForeground() );
g.drawRect( 2, 2, d -4, d - 5 );
FontMetrics fm = g.getFontMetrics();
int h = fm.getLeading() + fm.getAscent();
g.drawString( s, d + 6, ( d + h )/ 2 );
}
/**
* Overrides <code>DefaultTreeCellRenderer.getPreferredSize</code> to
* return slightly wider preferred size value.
*/
public Dimension getPreferredSize()
{
Dimension retDimension = super.getPreferredSize();
if ( retDimension != null )
retDimension = new Dimension( Math.max( retDimension.width + 2 * retDimension.height + 32, 100 ),
retDimension.height );
return retDimension;
}
public Component getListCellRendererComponent( JList list, Object value,
int index, boolean isSelected,
boolean cellHasFocus )
{
return super.getListCellRendererComponent( list, value,
index, isSelected, cellHasFocus );
}
}
|