KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > util > ExtendedTableCellRenderer


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.util;
9
10 import javax.swing.*;
11 import javax.swing.border.Border JavaDoc;
12 import javax.swing.border.EmptyBorder JavaDoc;
13 import javax.swing.table.TableCellRenderer JavaDoc;
14 import java.awt.*;
15
16
17 /**
18     A <tt>TableCellRenderer</tt> which is based on an <tt>ExtendedJLabel</tt> rather than
19     a <tt>JLabel</tt> like the <tt>javax.swing.table.DefaultTableCellRenderer</tt>.
20
21     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
22     @version $Revision: 1.4 $ $Date: 2003/08/18 07:48:28 $
23 */

24 public class ExtendedTableCellRenderer extends ExtendedJLabel
25                                        implements TableCellRenderer JavaDoc {
26
27     private static final Border JavaDoc NO_FOCUS_BORDER = new EmptyBorder JavaDoc(1, 1, 1, 1);
28
29     private Color unselectedForeground;
30     private Color unselectedBackground;
31
32     /**
33      * Constructor.
34      */

35     public ExtendedTableCellRenderer() {
36         super();
37         setOpaque(true);
38         setBorder(NO_FOCUS_BORDER);
39     }
40
41     public void setForeground(Color c) {
42         super.setForeground(c);
43         unselectedForeground = c;
44     }
45     
46     public void setBackground(Color c) {
47         super.setBackground(c);
48         unselectedBackground = c;
49     }
50
51     public void updateUI() {
52         super.updateUI();
53         setForeground(null);
54         setBackground(null);
55     }
56     
57     public Component getTableCellRendererComponent(JTable table,
58                                                    Object JavaDoc value,
59                                                    boolean isSelected,
60                                                    boolean hasFocus,
61                                                    int row,
62                                                    int column)
63     {
64         if (isSelected) {
65            super.setForeground(table.getSelectionForeground());
66            super.setBackground(table.getSelectionBackground());
67
68         } else {
69             super.setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground());
70             super.setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground());
71         }
72
73         setFont(table.getFont());
74
75         if (hasFocus) {
76             setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
77             if (table.isCellEditable(row, column)) {
78                 super.setForeground(UIManager.getColor("Table.focusCellForeground"));
79                 super.setBackground(UIManager.getColor("Table.focusCellBackground"));
80             }
81         } else {
82             setBorder(NO_FOCUS_BORDER);
83         }
84
85         setValue(value);
86
87         Color background = getBackground();
88         boolean colorMatch = (background != null) && (background.equals(table.getBackground())) && table.isOpaque();
89         setOpaque(!colorMatch);
90
91         return this;
92     }
93     
94
95     public void validate() {}
96
97     public void revalidate() {}
98
99     public void repaint(long tm, int x, int y, int width, int height) {}
100
101     public void repaint(Rectangle r) { }
102
103     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
104         if (propertyName == "text") {
105             super.firePropertyChange(propertyName, oldValue, newValue);
106         }
107     }
108
109     public void firePropertyChange(String JavaDoc propertyName, boolean oldValue, boolean newValue) { }
110
111
112     private void setValue(Object JavaDoc value) {
113         setText((value == null) ? "" : value.toString());
114     }
115     
116
117 }
118
119
120
Popular Tags