01: package com.xoetrope.svgscanner;
02:
03: import java.awt.Color;
04: import java.awt.Dimension;
05: import java.awt.FontMetrics;
06: import java.awt.Graphics;
07: import net.xoetrope.xui.style.XStyle;
08: import com.xoetrope.swing.list.XAltListCellRenderer;
09: import java.awt.Component;
10: import javax.swing.JList;
11:
12: /**
13: *
14: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
15: * the GNU Public License (GPL), please see license.txt for more details. If
16: * you make commercial use of this software you must purchase a commercial
17: * license from Xoetrope.</p>
18: * <p>$Revision: 1.3 $</p>
19: */
20: public class ColorListCellRenderer extends XAltListCellRenderer {
21:
22: /** Creates a new instance of ColorListCellRenderer */
23: public ColorListCellRenderer() {
24: }
25:
26: /**
27: * Render the cell by drawing the text and then two colour swatches and the font size
28: * @param g
29: */
30: public void paintComponent(Graphics g) {
31: Dimension b = getSize();
32: g.setColor(getBackground());
33: g.fillRect(0, 0, b.width, b.height);
34:
35: int d = b.height;
36:
37: String s = getText();
38: Color clr = (Color) XStyle.parseColor(s);
39: g.setColor(clr);
40: g.fillRect(2, 2, d - 4, d - 5);
41:
42: g.setColor(getForeground());
43: g.drawRect(2, 2, d - 4, d - 5);
44:
45: FontMetrics fm = g.getFontMetrics();
46: int h = fm.getLeading() + fm.getAscent();
47: g.drawString(s, d + 6, (d + h) / 2);
48: }
49:
50: /**
51: * Overrides <code>DefaultTreeCellRenderer.getPreferredSize</code> to
52: * return slightly wider preferred size value.
53: */
54: public Dimension getPreferredSize() {
55: Dimension retDimension = super .getPreferredSize();
56:
57: if (retDimension != null)
58: retDimension = new Dimension(Math.max(retDimension.width
59: + 2 * retDimension.height + 32, 100),
60: retDimension.height);
61: return retDimension;
62: }
63:
64: public Component getListCellRendererComponent(JList list,
65: Object value, int index, boolean isSelected,
66: boolean cellHasFocus) {
67: return super.getListCellRendererComponent(list, value, index,
68: isSelected, cellHasFocus);
69: }
70: }
|