001: package com.xoetrope.swing.table;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Graphics;
006: import java.text.Format;
007: import javax.swing.JTable;
008: import javax.swing.UIManager;
009: import javax.swing.table.DefaultTableCellRenderer;
010: import net.xoetrope.xui.helper.XuiUtilities;
011:
012: /**
013: * A cell renderer that color codes alternate rows in the table
014: *
015: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
016: * the GNU Public License (GPL), please see license.txt for more details. If
017: * you make commercial use of this software you must purchase a commercial
018: * license from Xoetrope.</p>
019: * <p> $Revision: 1.6 $</p>
020: */
021: public class XAltRowTableCellRenderer extends DefaultTableCellRenderer {
022: private Format format;
023: private Color unselectedForeground;
024: private Color unselectedBackground;
025: private Color altUnselectedForeground;
026: private Color altUnselectedBackground;
027:
028: private boolean wrapsText = false;
029:
030: /**
031: * Set the colors for alternate ( odd ) row colors
032: * @param frgd the foreground color
033: * @param bkgd the background color
034: */
035: public void setAltUnselectedColors(Color frgd, Color bkgd) {
036: altUnselectedForeground = frgd;
037: altUnselectedBackground = bkgd;
038: }
039:
040: /**
041: * Set the wrap text property of this renderer. This property should not be set
042: * to true if the renderer is used for more than one column.
043: * @param state the wrapping state - true to wrap the text
044: */
045: public void setWrapsText(boolean state) {
046: wrapsText = state;
047: }
048:
049: /**
050: *
051: * Returns the default table cell renderer.
052: *
053: * @param table the <code>JTable</code>
054: * @param value the value to assign to the cell at
055: * <code>[row, column]</code>
056: * @param isSelected true if cell is selected
057: * @param hasFocus true if cell has focus
058: * @param row the row of the cell to render
059: * @param column the column of the cell to render
060: * @return the default table cell renderer
061: */
062: public Component getTableCellRendererComponent(JTable table,
063: Object value, boolean isSelected, boolean hasFocus,
064: int row, int column) {
065: if (unselectedForeground == null) {
066: unselectedForeground = table.getForeground();
067: unselectedBackground = table.getBackground();
068: }
069: if (altUnselectedForeground == null) {
070: altUnselectedForeground = table.getForeground();
071: altUnselectedBackground = table.getBackground();
072: }
073:
074: int percentage = 100;
075: if (!table.isEnabled())
076: percentage = 50;
077:
078: Color fgColor, bkColor;
079: if (isSelected) {
080: fgColor = XuiUtilities.unsaturateColor(table
081: .getSelectionForeground(), percentage);
082: bkColor = XuiUtilities.unsaturateColor(table
083: .getSelectionBackground(), percentage);
084: } else {
085: fgColor = XuiUtilities.unsaturateColor(
086: ((row % 2) == 0 ? unselectedForeground
087: : altUnselectedForeground), percentage);
088: bkColor = XuiUtilities.unsaturateColor(
089: ((row % 2) == 0 ? unselectedBackground
090: : altUnselectedBackground), percentage);
091: }
092: super .setForeground(fgColor);
093: super .setBackground(bkColor);
094:
095: setFont(table.getFont());
096:
097: if (hasFocus) {
098: setBorder(UIManager
099: .getBorder("Table.focusCellHighlightBorder"));
100: if (table.isCellEditable(row, column)) {
101: super .setForeground(UIManager
102: .getColor("Table.focusCellForeground"));
103: super .setBackground(UIManager
104: .getColor("Table.focusCellBackground"));
105: }
106: } else
107: setBorder(noFocusBorder);
108:
109: setValue(value);
110:
111: if (wrapsText && (value != null)
112: && (value.toString().length() > 0)) {
113: int h = getPreferredSize().height;
114: if (table.getRowHeight(row) != h)
115: table.setRowHeight(row, h);
116: }
117:
118: return this;
119: }
120: }
|