View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table.renderers;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.Graphics;
25  
26  import javax.swing.JTable;
27  import javax.swing.border.BevelBorder;
28  import javax.swing.border.Border;
29  import javax.swing.border.CompoundBorder;
30  import javax.swing.border.EmptyBorder;
31  import javax.swing.table.DefaultTableCellRenderer;
32  
33  
34  /**
35   * Renders cells containing <code>java.awt.Color</code> objects. Cell will be
36   * rendered as opaque colored cells of specified color with beveled border.
37   *
38   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
39   * @version $id$
40   */
41  public class ColorRenderer extends DefaultTableCellRenderer
42  {
43  	private static final long serialVersionUID = 1L;
44  	private static final Border border = new CompoundBorder(new EmptyBorder(2, 2, 2, 2),
45  		    new BevelBorder(BevelBorder.RAISED));
46  
47  	/**
48  	 * Constructor for ColorRenderer.
49  	 */
50  	public ColorRenderer()
51  	{
52  		super();
53  		setOpaque(true);
54  	}
55  
56  	/*
57  	 * (non-Javadoc)
58  	 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
59  	 */
60  	public Component getTableCellRendererComponent(JTable table, Object value,
61  	    boolean isSelected, boolean hasFocus, int row, int column)
62  	{
63  		Component cell = super.getTableCellRendererComponent(table, value,
64  			    isSelected, hasFocus, row, column);
65  
66  		setBorder(border);
67  
68  		if (value instanceof Color) {
69  			setBackground((Color)value);
70  		}
71  
72  		setText("");
73  
74  		return cell;
75  	}
76  
77  	/*
78  	 * (non-Javadoc)
79  	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
80  	 */
81  	protected void paintComponent(Graphics g)
82  	{
83  		super.paintComponent(g);
84  		g.setColor(Color.WHITE);
85  		g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
86  		g.drawRect(0, 0, getWidth() - 2, getHeight() - 2);
87  		g.drawRect(1, 1, getWidth() - 3, getHeight() - 3);
88  	}
89  }
90  
91  /* __oOo__ */