View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  import java.awt.Color;
22  import java.awt.Component;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.JLabel;
26  import javax.swing.JTable;
27  import javax.swing.border.Border;
28  import javax.swing.table.TableCellRenderer;
29  
30  /***
31  * A TableCellRenderer that renders colors.
32  *
33  * @author michael@mpowers.net
34  * @author $Author: cgruber $
35  * @version $Revision: 904 $
36  */
37  public class ColorCellRenderer extends JLabel implements TableCellRenderer {
38      Border unselectedBorder = null;
39      Border selectedBorder = null;
40      boolean isBordered = true;
41  
42      public ColorCellRenderer(boolean isBordered)
43      {
44          super();
45          this.isBordered = isBordered;
46          setOpaque(true); // must do this for background to show up.
47      }
48  
49      public Component getTableCellRendererComponent(
50                              JTable table, Object color,
51                              boolean isSelected, boolean hasFocus,
52                              int row, int column)
53      {
54          setBackground((Color)color);
55          if (isBordered)
56          {
57              if (isSelected)
58              {
59                  if (selectedBorder == null)
60                  {
61                      selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
62                                                table.getSelectionBackground());
63                  }
64                  setBorder(selectedBorder);
65              }
66              else
67              {
68                  if (unselectedBorder == null)
69                  {
70                      unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
71                                                table.getBackground());
72                  }
73                  setBorder(unselectedBorder);
74              }
75          }
76          return this;
77      }
78  }
79  
80  
81