Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
AlternatingRowCellRenderer |
|
| 2.0;2 |
1 | /* |
|
2 | Wotonomy: OpenStep design patterns for pure Java applications. |
|
3 | Copyright (C) 2001 Intersect Software Corporation |
|
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.JComponent; |
|
25 | import javax.swing.JTable; |
|
26 | import javax.swing.UIManager; |
|
27 | import javax.swing.table.DefaultTableCellRenderer; |
|
28 | import javax.swing.table.TableCellRenderer; |
|
29 | ||
30 | /** |
|
31 | * A TableCellRenderer that wraps another TableCellRenderer |
|
32 | * and sets the background to the specified color for odd-numbered rows. |
|
33 | * This makes every other row appear to be a different color, |
|
34 | * which helps users distinguish rows of data in densely-packed |
|
35 | * tables. |
|
36 | * |
|
37 | * @author michael@mpowers.net |
|
38 | * @author $Author: cgruber $ |
|
39 | * @version $Revision: 904 $ |
|
40 | */ |
|
41 | public class AlternatingRowCellRenderer implements TableCellRenderer { |
|
42 | ||
43 | protected TableCellRenderer wrappedRenderer; |
|
44 | protected Color alternateColor; |
|
45 | ||
46 | /** |
|
47 | * Default constructor uses a lighter shade of the system control color |
|
48 | * and wraps a DefaultTableCellRenderer. |
|
49 | */ |
|
50 | public AlternatingRowCellRenderer() |
|
51 | { |
|
52 | 0 | this( new DefaultTableCellRenderer() ); |
53 | 0 | } |
54 | ||
55 | /** |
|
56 | * Uses the specified color for the background of the alternating rows, |
|
57 | * and wraps a DefaultTableCellRenderer. |
|
58 | */ |
|
59 | public AlternatingRowCellRenderer( |
|
60 | Color aColor ) |
|
61 | { |
|
62 | 0 | this( aColor, new DefaultTableCellRenderer() ); |
63 | 0 | } |
64 | ||
65 | /** |
|
66 | * Uses the uses a lighter shade of the system control color |
|
67 | * for the background of the alternating rows, |
|
68 | * and wraps the specified TableCellRenderer. |
|
69 | */ |
|
70 | 0 | public AlternatingRowCellRenderer( |
71 | TableCellRenderer aRenderer ) |
|
72 | 0 | { |
73 | 0 | Color c = UIManager.getColor( "control" ); |
74 | 0 | c = new Color( // lighten this color just slightly |
75 | 0 | (int) ( c.getRed() + ( ( 255 - c.getRed() ) / 1.5 ) ), |
76 | 0 | (int) ( c.getGreen() + ( ( 255 - c.getGreen() ) / 1.5 ) ), |
77 | 0 | (int) ( c.getBlue() + ( ( 255 - c.getBlue() ) / 1.5 ) ) ); |
78 | ||
79 | 0 | alternateColor = c; |
80 | 0 | wrappedRenderer = aRenderer; |
81 | 0 | } |
82 | ||
83 | /** |
|
84 | * Uses the specified color for the background of the alternating rows, |
|
85 | * and wraps the specified TableCellRenderer. |
|
86 | */ |
|
87 | 0 | public AlternatingRowCellRenderer( |
88 | Color aColor, TableCellRenderer aRenderer ) |
|
89 | 0 | { |
90 | 0 | alternateColor = aColor; |
91 | 0 | wrappedRenderer = aRenderer; |
92 | 0 | } |
93 | ||
94 | public Component getTableCellRendererComponent( |
|
95 | JTable table, Object value, |
|
96 | boolean isSelected, boolean hasFocus, |
|
97 | int row, int column) |
|
98 | { |
|
99 | 0 | Component result = wrappedRenderer.getTableCellRendererComponent( |
100 | 0 | table, value, isSelected, hasFocus, row, column ); |
101 | 0 | if ( ! isSelected ) |
102 | { |
|
103 | 0 | if ( row % 2 == 0 ) |
104 | { |
|
105 | 0 | if ( ! result.getBackground().equals( table.getBackground() ) ) |
106 | { |
|
107 | 0 | result.setBackground( table.getBackground() ); |
108 | 0 | } |
109 | } |
|
110 | else |
|
111 | { |
|
112 | 0 | if ( ! result.getBackground().equals( alternateColor ) ) |
113 | { |
|
114 | // jdk1.3's default renderer is opaque |
|
115 | 0 | if ( result instanceof JComponent ) |
116 | { |
|
117 | 0 | ((JComponent)result).setOpaque( true ); |
118 | } |
|
119 | ||
120 | 0 | result.setBackground( alternateColor ); |
121 | } |
|
122 | } |
|
123 | } |
|
124 | 0 | return result; |
125 | } |
|
126 | } |
|
127 | ||
128 | ||
129 |