01: package com.xoetrope.swing.list;
02:
03: import java.awt.Color;
04: import java.awt.Component;
05: import javax.swing.DefaultListCellRenderer;
06: import javax.swing.JList;
07: import javax.swing.UIManager;
08: import net.xoetrope.xui.helper.XuiUtilities;
09:
10: /**
11: * A cell renderer that color codes alternate rows in the list
12: *
13: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
14: * the GNU Public License (GPL), please see license.txt for more details. If
15: * you make commercial use of this software you must purchase a commercial
16: * license from Xoetrope.</p>
17: * <p> $Revision: 1.6 $</p>
18: */
19: public class XAltListCellRenderer extends DefaultListCellRenderer {
20: protected Color unselectedForeground;
21: protected Color unselectedBackground;
22: protected Color altUnselectedForeground;
23: protected Color altUnselectedBackground;
24:
25: /** Creates a new instance of XAltListCellRenderer */
26: public XAltListCellRenderer() {
27: }
28:
29: /**
30: * Set the colors for alternate ( odd ) row colors
31: * @param frgd the foreground color
32: * @param bkgd the background color
33: */
34: public void setAltUnselectedColors(Color frgd, Color bkgd) {
35: altUnselectedForeground = frgd;
36: altUnselectedBackground = bkgd;
37: }
38:
39: /*
40: * This method finds the image and text corresponding
41: * to the selected value and returns the label, set-up
42: * to display the text and image.
43: */
44: public Component getListCellRendererComponent(JList list,
45: Object value, int index, boolean isSelected,
46: boolean cellHasFocus) {
47: if (unselectedForeground == null) {
48: unselectedForeground = list.getForeground();
49: unselectedBackground = list.getBackground();
50: }
51:
52: if (altUnselectedForeground == null) {
53: altUnselectedForeground = list.getForeground();
54: altUnselectedBackground = list.getBackground();
55: }
56:
57: int percentage = 100;
58: if (!list.isEnabled())
59: percentage = 50;
60:
61: Color fgColor, bkColor;
62: if (isSelected) {
63: fgColor = XuiUtilities.unsaturateColor(list
64: .getSelectionForeground(), percentage);
65: bkColor = XuiUtilities.unsaturateColor(list
66: .getSelectionBackground(), percentage);
67: } else {
68: fgColor = XuiUtilities.unsaturateColor(
69: ((index % 2) == 0 ? unselectedForeground
70: : altUnselectedForeground), percentage);
71: bkColor = XuiUtilities.unsaturateColor(
72: ((index % 2) == 0 ? unselectedBackground
73: : altUnselectedBackground), percentage);
74: }
75: super .setForeground(fgColor);
76: super .setBackground(bkColor);
77:
78: setFont(list.getFont());
79: if (cellHasFocus) {
80: setBorder(UIManager
81: .getBorder("List.focusCellHighlightBorder"));
82: // if ( list.isCellEdilist( row, column )) {
83: // super.setForeground( UIManager.getColor( "List.focusCellForeground" ));
84: // super.setBackground( UIManager.getColor( "List.focusCellBackground" ));
85: // }
86: } else
87: setBorder(noFocusBorder);
88:
89: setText(value.toString());
90:
91: return this;
92: }
93: }
|