001: /*
002: * DefaultTableHeaderRenderer.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.table;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.GradientPaint;
028: import java.awt.Graphics;
029: import java.awt.Graphics2D;
030: import java.awt.Paint;
031: import javax.swing.JLabel;
032: import javax.swing.JTable;
033: import javax.swing.UIManager;
034: import javax.swing.table.DefaultTableCellRenderer;
035: import javax.swing.table.JTableHeader;
036: import org.underworldlabs.Constants;
037:
038: import org.underworldlabs.swing.plaf.UIUtils;
039:
040: /* ----------------------------------------------------------
041: * CVS NOTE: Changes to the CVS repository prior to the
042: * release of version 3.0.0beta1 has meant a
043: * resetting of CVS revision numbers.
044: * ----------------------------------------------------------
045: */
046:
047: /**
048: * Simple header renderer with tool tip text and
049: * configurable height.
050: *
051: * @author Takis Diakoumis
052: * @version $Revision: 1.7 $
053: * @date $Date: 2006/07/16 15:44:56 $
054: */
055: public class DefaultTableHeaderRenderer extends
056: DefaultTableCellRenderer {
057: //implements TableCellRenderer {
058:
059: /** the height of the header */
060: private int height;
061:
062: /** the light gradient colour 1 */
063: private Color colour1;
064:
065: /** the dark gradient colour 2 */
066: private Color colour2;
067:
068: /** whether to fill a gradient background */
069: private boolean fillGradient;
070:
071: /** the default height - 20 */
072: protected static final int DEFAULT_HEIGHT = 20;
073:
074: /** Creates a new instance of DefaultTableHeaderRenderer */
075: public DefaultTableHeaderRenderer() {
076: this (DEFAULT_HEIGHT);
077: }
078:
079: /** Creates a new instance of DefaultTableHeaderRenderer */
080: public DefaultTableHeaderRenderer(int height) {
081: this .height = height;
082: /*
083: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
084: setFont(UIManager.getFont("TableHeader.font"));
085: setForeground(UIManager.getColor("TableHeader.foreground"));
086: setBackground(UIManager.getColor("TableHeader.background"));
087: */
088: setHorizontalAlignment(JLabel.CENTER);
089:
090: fillGradient = UIUtils.isDefaultLookAndFeel()
091: || UIUtils.usingOcean();
092: if (fillGradient) {
093: colour1 = UIManager.getColor("Label.background");
094: colour2 = UIUtils.getDarker(colour1, 0.85);
095: }
096:
097: }
098:
099: public Component getTableCellRendererComponent(JTable table,
100: Object value, boolean isSelected, boolean hasFocus,
101: int row, int column) {
102: String label = null;
103: if (value != null) {
104: label = value.toString();
105: } else {
106: label = Constants.EMPTY;
107: }
108:
109: setText(label);
110: setToolTipText(label);
111:
112: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
113: setFont(UIManager.getFont("TableHeader.font"));
114: setForeground(UIManager.getColor("TableHeader.foreground"));
115: setBackground(UIManager.getColor("TableHeader.background"));
116:
117: return this ;
118: }
119:
120: public void paintComponent(Graphics g) {
121: if (fillGradient) {
122: int width = getWidth();
123: int height = getHeight();
124:
125: Graphics2D g2 = (Graphics2D) g;
126: Paint originalPaint = g2.getPaint();
127: GradientPaint fade = new GradientPaint(0, height, colour2,
128: 0, (int) (height * 0.5), colour1);
129:
130: g2.setPaint(fade);
131: g2.fillRect(0, 0, width, height);
132:
133: g2.setPaint(originalPaint);
134: }
135: super .paintComponent(g);
136: }
137:
138: public boolean isOpaque() {
139: return !fillGradient;
140: }
141:
142: public Dimension getPreferredSize() {
143: return new Dimension(getWidth(), height);
144: }
145:
146: /**
147: * Returns the height of the renderer.
148: */
149: public int getHeight() {
150: return height;
151: }
152:
153: }
|