001: package com.xoetrope.swing.list;
002:
003: import com.xoetrope.swing.XCheckList;
004: import java.awt.Color;
005: import java.awt.Component;
006: import javax.swing.JCheckBox;
007: import javax.swing.JList;
008: import javax.swing.ListCellRenderer;
009: import javax.swing.UIManager;
010: import javax.swing.border.Border;
011: import javax.swing.border.EmptyBorder;
012: import net.xoetrope.xui.helper.XuiUtilities;
013:
014: /**
015: * A renderer for XCheckList components
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.5 $</p>
022: */
023: public class XCheckListCellRenderer implements ListCellRenderer {
024: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
025:
026: private Color unselectedForeground;
027: private Color unselectedBackground;
028:
029: private Color altUnselectedForeground;
030: private Color altUnselectedBackground;
031:
032: private XCheckList parent;
033:
034: public XCheckListCellRenderer(XCheckList p) {
035: parent = p;
036: }
037:
038: public Component getListCellRendererComponent(JList list,
039: Object value, int index, boolean isSelected,
040: boolean cellHasFocus) {
041: if (unselectedForeground == null) {
042: unselectedForeground = list.getForeground();
043: unselectedBackground = list.getBackground();
044: }
045:
046: if (altUnselectedForeground == null) {
047: altUnselectedForeground = list.getForeground();
048: altUnselectedBackground = list.getBackground();
049: }
050:
051: int percentage = 100;
052: if (!list.isEnabled())
053: percentage = 50;
054:
055: Color fgColor, bkColor;
056: if (isSelected) {
057: fgColor = XuiUtilities.unsaturateColor(list
058: .getSelectionForeground(), percentage);
059: bkColor = XuiUtilities.unsaturateColor(list
060: .getSelectionBackground(), percentage);
061: } else {
062: fgColor = XuiUtilities
063: .unsaturateColor(
064: ((index % 2) == 0 ? unselectedForeground
065: : getAltUnselectedForeground()),
066: percentage);
067: bkColor = XuiUtilities
068: .unsaturateColor(
069: ((index % 2) == 0 ? unselectedBackground
070: : getAltUnselectedBackground()),
071: percentage);
072: }
073:
074: JCheckBox checkbox = (JCheckBox) value;
075: checkbox.setBackground(bkColor);
076: checkbox.setForeground(fgColor);
077: //checkbox.setEnabled( parent.isEnabled() );
078: checkbox.setFont(parent.getFont());
079: checkbox.setFocusPainted(false);
080: checkbox.setBorderPainted(true);
081: checkbox.setBorder(isSelected ? UIManager
082: .getBorder("List.focusCellHighlightBorder")
083: : noFocusBorder);
084:
085: return checkbox;
086: }
087:
088: /**
089: * Set the colors for alternate ( odd ) row colors
090: * @param frgd the foreground color
091: * @param bkgd the background color
092: */
093: public void setAltUnselectedColors(Color frgd, Color bkgd) {
094: altUnselectedForeground = frgd;
095: altUnselectedBackground = bkgd;
096: }
097:
098: public Color getAltUnselectedForeground() {
099: return altUnselectedForeground;
100: }
101:
102: public void setAltUnselectedForeground(Color frgd) {
103: altUnselectedForeground = frgd;
104: }
105:
106: public Color getAltUnselectedBackground() {
107: return altUnselectedBackground;
108: }
109:
110: public void setAltUnselectedBackground(Color bkgd) {
111: altUnselectedBackground = bkgd;
112: }
113: }
|