001: /*
002: This library is free software; you can redistribute it and/or
003: modify it under the terms of the GNU General Public
004: License as published by the Free Software Foundation; either
005: version 2 of the license, or (at your option) any later version.
006: */
007:
008: package org.gjt.jclasslib.util;
009:
010: import javax.swing.*;
011: import javax.swing.border.Border;
012: import javax.swing.border.EmptyBorder;
013: import javax.swing.table.TableCellRenderer;
014: import java.awt.*;
015:
016: /**
017: A <tt>TableCellRenderer</tt> which is based on an <tt>ExtendedJLabel</tt> rather than
018: a <tt>JLabel</tt> like the <tt>javax.swing.table.DefaultTableCellRenderer</tt>.
019:
020: @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
021: @version $Revision: 1.4 $ $Date: 2003/08/18 07:48:28 $
022: */
023: public class ExtendedTableCellRenderer extends ExtendedJLabel implements
024: TableCellRenderer {
025:
026: private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1,
027: 1, 1);
028:
029: private Color unselectedForeground;
030: private Color unselectedBackground;
031:
032: /**
033: * Constructor.
034: */
035: public ExtendedTableCellRenderer() {
036: super ();
037: setOpaque(true);
038: setBorder(NO_FOCUS_BORDER);
039: }
040:
041: public void setForeground(Color c) {
042: super .setForeground(c);
043: unselectedForeground = c;
044: }
045:
046: public void setBackground(Color c) {
047: super .setBackground(c);
048: unselectedBackground = c;
049: }
050:
051: public void updateUI() {
052: super .updateUI();
053: setForeground(null);
054: setBackground(null);
055: }
056:
057: public Component getTableCellRendererComponent(JTable table,
058: Object value, boolean isSelected, boolean hasFocus,
059: int row, int column) {
060: if (isSelected) {
061: super .setForeground(table.getSelectionForeground());
062: super .setBackground(table.getSelectionBackground());
063:
064: } else {
065: super
066: .setForeground((unselectedForeground != null) ? unselectedForeground
067: : table.getForeground());
068: super
069: .setBackground((unselectedBackground != null) ? unselectedBackground
070: : table.getBackground());
071: }
072:
073: setFont(table.getFont());
074:
075: if (hasFocus) {
076: setBorder(UIManager
077: .getBorder("Table.focusCellHighlightBorder"));
078: if (table.isCellEditable(row, column)) {
079: super .setForeground(UIManager
080: .getColor("Table.focusCellForeground"));
081: super .setBackground(UIManager
082: .getColor("Table.focusCellBackground"));
083: }
084: } else {
085: setBorder(NO_FOCUS_BORDER);
086: }
087:
088: setValue(value);
089:
090: Color background = getBackground();
091: boolean colorMatch = (background != null)
092: && (background.equals(table.getBackground()))
093: && table.isOpaque();
094: setOpaque(!colorMatch);
095:
096: return this ;
097: }
098:
099: public void validate() {
100: }
101:
102: public void revalidate() {
103: }
104:
105: public void repaint(long tm, int x, int y, int width, int height) {
106: }
107:
108: public void repaint(Rectangle r) {
109: }
110:
111: protected void firePropertyChange(String propertyName,
112: Object oldValue, Object newValue) {
113: if (propertyName == "text") {
114: super .firePropertyChange(propertyName, oldValue, newValue);
115: }
116: }
117:
118: public void firePropertyChange(String propertyName,
119: boolean oldValue, boolean newValue) {
120: }
121:
122: private void setValue(Object value) {
123: setText((value == null) ? "" : value.toString());
124: }
125:
126: }
|