KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > decorator > AlternateRowHighlighter


1 /*
2  * $Id: AlternateRowHighlighter.java,v 1.2 2004/10/14 01:28:19 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.decorator;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12
13 /**
14  * AlternateRowHighlighter prepares a cell renderer to use different background
15  * colors for alternating rows in a data view.
16  *
17  * @author Ramesh Gupta
18  */

19 public class AlternateRowHighlighter extends Highlighter {
20     private final static Color JavaDoc defaultOddRowColor = Color.white;
21     private final static Color JavaDoc defaultEvenRowColor = new Color JavaDoc(0xF0, 0xF0, 0xE0);
22
23     public final static Highlighter beige =
24         new AlternateRowHighlighter(Color.white, new Color JavaDoc(245, 245, 220), null);
25
26     public final static Highlighter linePrinter =
27         new AlternateRowHighlighter(Color.white, new Color JavaDoc(0xCC, 0xCC, 0xFF), null);
28
29     public final static Highlighter classicLinePrinter =
30         new AlternateRowHighlighter(Color.white, new Color JavaDoc(0xCC, 0xFF, 0xCC), null);
31
32     public final static Highlighter floralWhite =
33         new AlternateRowHighlighter(Color.white, new Color JavaDoc(255, 250, 240), null);
34
35     public final static Highlighter quickSilver =
36         new AlternateRowHighlighter(Color.white, defaultEvenRowColor, null);
37
38     private Color JavaDoc oddRowBackground = defaultOddRowColor;
39     private Color JavaDoc evenRowBackground = defaultEvenRowColor;
40
41     /**
42      * Constructs a default <code>AlternateRowHighlighter</code> that prepares a
43      * cell renderer to paint a white background for odd rows and a silver
44      * <code>(0xF0, 0xF0, 0xE0)</code> background for even rows.
45      */

46     public AlternateRowHighlighter() {
47     }
48
49     /**
50      * Constructs an <code>AlternateRowHighlighter</code> that prepares a
51      * cell renderer to paint the specified background colors for odd and even
52      * and even rows. A foreground color may also be specified. If null is
53      * specified for the foreground color, the foreground color for the renderer
54      * is unchanged. Otherwise, it is set to the specified foreground color for
55      * both odd and even rows.
56      *
57      * @param oddRowBackground
58      * @param evenRowBackground
59      * @param foreground
60      */

61     public AlternateRowHighlighter(Color JavaDoc oddRowBackground,
62                                    Color JavaDoc evenRowBackground, Color JavaDoc foreground) {
63         super(oddRowBackground, foreground); // same background for odd and even
64
this.oddRowBackground = oddRowBackground;
65         this.evenRowBackground = evenRowBackground;
66     }
67
68     /**
69      * Returns the background color for odd rows, or null if the background color
70      * of the cell renderer should be left unchanged for odd rows.
71      *
72      * @return the background color for odd rows, or null if the background color
73      * of the cell renderer should be left unchanged for odd rows
74      */

75     public Color JavaDoc getOddRowBackground() {
76         return oddRowBackground;
77     }
78
79     /**
80      * Sets the background color for odd rows to the specified color. If null is
81      * specified, the background color for odd rows is left unchanged in the
82      * renderer
83
84      *
85      * @param color the background color for odd rows, or null if the background
86      * color of the cell renderer should be left unchanged for odd rows
87      */

88     public void setOddRowBackground(Color JavaDoc color) {
89         oddRowBackground = color;
90     }
91
92     /**
93      * Returns the background color for even rows, or null if the background color
94      * of the cell renderer should be left unchanged for even rows.
95      *
96      * @return the background color for even rows, or null if the background color
97      * of the cell renderer should be left unchanged for even rows
98      */

99     public Color JavaDoc getEvenRowBackground() {
100         return evenRowBackground;
101     }
102
103     /**
104      * Sets the background color for even rows to the specified color. If null is
105      * specified, the background color for even rows is left unchanged in the
106      * renderer.
107      *
108      * @param color the background color for even rows, or null if the background
109      * color of the cell renderer should be left unchanged for even rows
110      */

111     public void setEvenRowBackground(Color JavaDoc color) {
112         evenRowBackground = color;
113     }
114
115     /**
116      * Computes the background color for the current rendering context for the
117      * specified adapter. It first chooses the raw background color for the
118      * renderer depending on whether the row being rendered is odd or even. If
119      * the chosen background color is not null
120      * calls {@link Highlighter#computeSelectedBackground(java.awt.Color) computeSelectedBackground}
121      * passing in the chosen raw background color as the seed color, but only if
122      * the row being rendered is selected.
123      *
124      * @param renderer the cell renderer component
125      * @param adapter
126      * @return the computed background color
127      */

128     protected Color JavaDoc computeBackground(Component JavaDoc renderer,
129                                       ComponentAdapter adapter) {
130         // row is zero-based; so even is actually odd!
131
Color JavaDoc color = (adapter.row % 2) == 0 ?
132             oddRowBackground : evenRowBackground;
133
134         if ((color != null) && adapter.isSelected()) {
135             color = computeSelectedBackground(color);
136         }
137
138         return color;
139     }
140 }
141
Popular Tags