001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.table;
022:
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Rectangle;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.JLabel;
029: import javax.swing.JTable;
030: import javax.swing.UIManager;
031: import javax.swing.border.Border;
032:
033: public class DefaultTableCellRenderer extends JLabel implements
034: TableCellRenderer {
035: public static class UIResource extends DefaultTableCellRenderer
036: implements javax.swing.plaf.UIResource {
037: }
038:
039: protected static Border noFocusBorder = BorderFactory
040: .createEmptyBorder(1, 1, 1, 1);
041: private Border focusBorder;
042: private Color focusCellBackground;
043: private Color focusCellForeground;
044:
045: public DefaultTableCellRenderer() {
046: updateUI();
047: setName(null);
048: }
049:
050: public void setForeground(final Color c) {
051: super .setForeground(c);
052: }
053:
054: public void setBackground(final Color c) {
055: super .setBackground(c);
056: }
057:
058: public void updateUI() {
059: super .updateUI();
060: setBackground(null);
061: setForeground(null);
062: focusBorder = UIManager
063: .getBorder("Table.focusCellHighlightBorder");
064: focusCellBackground = UIManager
065: .getColor("Table.focusCellBackground");
066: focusCellForeground = UIManager
067: .getColor("Table.focusCellForeground");
068: }
069:
070: public Component getTableCellRendererComponent(final JTable table,
071: final Object value, final boolean isSelected,
072: final boolean hasFocus, final int row, final int column) {
073:
074: setValue(value);
075: setFont(table.getFont());
076: if (hasFocus) {
077: setBorder(focusBorder);
078: if (isSelected) {
079: setBackground(table.getSelectionBackground());
080: setForeground(table.getSelectionForeground());
081: } else if (table.isCellEditable(row, column)) {
082: setBackground(focusCellBackground);
083: setForeground(focusCellForeground);
084: } else {
085: setBackground(table.getBackground());
086: setForeground(table.getForeground());
087: }
088: } else {
089: setBorder(noFocusBorder);
090: if (isSelected) {
091: setBackground(table.getSelectionBackground());
092: setForeground(table.getSelectionForeground());
093: } else {
094: setBackground(table.getBackground());
095: setForeground(table.getForeground());
096: }
097: }
098:
099: return this ;
100: }
101:
102: public boolean isOpaque() {
103: return true;
104: }
105:
106: public void invalidate() {
107: }
108:
109: public void validate() {
110: }
111:
112: public void revalidate() {
113: }
114:
115: public void repaint(final long tm, final int x, final int y,
116: final int width, final int height) {
117: }
118:
119: public void repaint(final Rectangle r) {
120: }
121:
122: public void repaint() {
123: }
124:
125: public void firePropertyChange(final String propertyName,
126: final boolean oldValue, final boolean newValue) {
127: }
128:
129: protected void firePropertyChange(final String propertyName,
130: final Object oldValue, final Object newValue) {
131: }
132:
133: protected void setValue(final Object value) {
134: setText(value != null ? value.toString() : "");
135: }
136: }
|