001: /*
002: * ColourTableCellRenderer.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.Graphics;
027: import javax.swing.ImageIcon;
028: import javax.swing.JLabel;
029: import javax.swing.JTable;
030: import javax.swing.table.TableCellRenderer;
031:
032: /* ----------------------------------------------------------
033: * CVS NOTE: Changes to the CVS repository prior to the
034: * release of version 3.0.0beta1 has meant a
035: * resetting of CVS revision numbers.
036: * ----------------------------------------------------------
037: */
038:
039: /**
040: * Cell Render for colour selections in cells -
041: * typically in system preferences etc.
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public class ColourTableCellRenderer extends JLabel implements
048: TableCellRenderer {
049:
050: private StringBuffer text;
051: private static ColourSwatchIcon icon;
052:
053: static {
054: icon = new ColourSwatchIcon();
055: }
056:
057: /** Creates a new instance of ColourTableCellRenderer */
058: public ColourTableCellRenderer() {
059: setIconTextGap(10);
060: text = new StringBuffer();
061: }
062:
063: public Component getTableCellRendererComponent(JTable table,
064: Object value, boolean isSelected, boolean cellHasFocus,
065: int row, int col) {
066:
067: Color colour = (Color) value;
068:
069: int red = colour.getRed();
070: int green = colour.getGreen();
071: int blue = colour.getBlue();
072:
073: if (isSelected) {
074: setBackground(table.getSelectionBackground());
075: setForeground(table.getSelectionForeground());
076: } else {
077: setBackground(table.getBackground());
078: setForeground(table.getForeground());
079: }
080:
081: icon.setColour(colour);
082: setIcon(icon);
083:
084: text.append(" [").append(red).append(",").append(green).append(
085: ",").append(blue).append("]");
086:
087: setText(text.toString());
088: text.setLength(0);
089:
090: return this ;
091: }
092:
093: }
094:
095: class ColourSwatchIcon extends ImageIcon {
096:
097: private static final int WIDTH = 11;
098: private static final int HEIGHT = 11;
099:
100: private Color colour;
101:
102: public ColourSwatchIcon() {
103: }
104:
105: public int getIconWidth() {
106: return WIDTH;
107: }
108:
109: public int getIconHeight() {
110: return HEIGHT;
111: }
112:
113: public void setColour(Color colour) {
114: this .colour = colour;
115: }
116:
117: public void paintIcon(Component c, Graphics g, int x, int y) {
118: // fill the background
119: g.setColor(colour);
120: g.fillRect(4, 4, HEIGHT, HEIGHT);
121: // draw the line
122: g.setColor(Color.BLACK);
123: g.drawRect(4, 4, WIDTH, HEIGHT);
124: }
125:
126: } // class ColourSwatchIcon
|