001: /*
002: * $Id: AlternateRowHighlighter.java,v 1.5 2005/11/22 12:21:39 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.decorator;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.util.HashMap;
027:
028: import javax.swing.UIManager;
029:
030: /**
031: * AlternateRowHighlighter prepares a cell renderer to use different background
032: * colors for alternating rows in a data view. The size (in terms of row count) of
033: * an alternating group is configurable (defaults to 1);
034: *
035: * @author Ramesh Gupta
036: */
037: public class AlternateRowHighlighter extends Highlighter {
038: private final static Color defaultOddRowColor = Color.white;
039: private final static Color defaultEvenRowColor = new Color(0xF0,
040: 0xF0, 0xE0);
041:
042: public final static Highlighter beige = new AlternateRowHighlighter(
043: Color.white, new Color(245, 245, 220), null, true);
044:
045: public final static Highlighter linePrinter = new AlternateRowHighlighter(
046: Color.white, new Color(0xCC, 0xCC, 0xFF), null, true);
047:
048: public final static Highlighter classicLinePrinter = new AlternateRowHighlighter(
049: Color.white, new Color(0xCC, 0xFF, 0xCC), null, true);
050:
051: public final static Highlighter floralWhite = new AlternateRowHighlighter(
052: Color.white, new Color(255, 250, 240), null, true);
053:
054: public final static Highlighter quickSilver = new AlternateRowHighlighter(
055: Color.white, defaultEvenRowColor, null, true);
056:
057: public final static AlternateRowHighlighter genericGrey = new AlternateRowHighlighter(
058: Color.white, new Color(229, 229, 229), null, true);
059:
060: private Color oddRowBackground = defaultOddRowColor;
061: private Color evenRowBackground = defaultEvenRowColor;
062: private int linesPerGroup = 1;
063:
064: /**
065: * Constructs a default <code>AlternateRowHighlighter</code> that prepares a
066: * cell renderer to paint a white background for odd rows and a silver
067: * <code>(0xF0, 0xF0, 0xE0)</code> background for even rows.
068: */
069: public AlternateRowHighlighter() {
070: }
071:
072: /**
073: * Constructs an <code>AlternateRowHighlighter</code> that prepares a
074: * cell renderer to paint the specified background colors for odd and even
075: * and even rows. A foreground color may also be specified. If null is
076: * specified for the foreground color, the foreground color for the renderer
077: * is unchanged. Otherwise, it is set to the specified foreground color for
078: * both odd and even rows.
079: *
080: * @param oddRowBackground
081: * @param evenRowBackground
082: * @param foreground
083: */
084: public AlternateRowHighlighter(Color oddRowBackground,
085: Color evenRowBackground, Color foreground) {
086: this (oddRowBackground, evenRowBackground, foreground, false);
087: }
088:
089: public AlternateRowHighlighter(Color oddRowBackground,
090: Color evenRowBackground, Color foreground, boolean immutable) {
091: super (oddRowBackground, foreground, immutable); // same background for odd and even
092: this .oddRowBackground = oddRowBackground;
093: this .evenRowBackground = evenRowBackground;
094: }
095:
096: /**
097: * Returns the background color for odd rows, or null if the background color
098: * of the cell renderer should be left unchanged for odd rows.
099: *
100: * @return the background color for odd rows, or null if the background color
101: * of the cell renderer should be left unchanged for odd rows
102: */
103: public Color getOddRowBackground() {
104: return oddRowBackground;
105: }
106:
107: /**
108: * Sets the background color for odd rows to the specified color. If null is
109: * specified, the background color for odd rows is left unchanged in the
110: * renderer
111:
112: *
113: * @param color the background color for odd rows, or null if the background
114: * color of the cell renderer should be left unchanged for odd rows
115: */
116: public void setOddRowBackground(Color color) {
117: if (isImmutable())
118: return;
119: oddRowBackground = color;
120: fireStateChanged();
121: }
122:
123: /**
124: * Returns the background color for even rows, or null if the background color
125: * of the cell renderer should be left unchanged for even rows.
126: *
127: * @return the background color for even rows, or null if the background color
128: * of the cell renderer should be left unchanged for even rows
129: */
130: public Color getEvenRowBackground() {
131: return evenRowBackground;
132: }
133:
134: /**
135: * Sets the background color for even rows to the specified color. If null is
136: * specified, the background color for even rows is left unchanged in the
137: * renderer.
138: *
139: * @param color the background color for even rows, or null if the background
140: * color of the cell renderer should be left unchanged for even rows
141: */
142: public void setEvenRowBackground(Color color) {
143: if (isImmutable())
144: return;
145: evenRowBackground = color;
146: fireStateChanged();
147: }
148:
149: @Override
150: protected Color computeUnselectedBackground(Component renderer,
151: ComponentAdapter adapter) {
152: return isOddRow(adapter) ? oddRowBackground : evenRowBackground;
153: }
154:
155: /**
156: * @param adapter
157: * @return true if the adapter's row should be colored with the oddRowBackground,
158: * false is not
159: */
160: protected boolean isOddRow(ComponentAdapter adapter) {
161: return ((adapter.row / getLinesPerGroup()) % 2) == 0;
162: }
163:
164: public int getLinesPerGroup() {
165: return linesPerGroup;
166: }
167:
168: public void setLinesPerGroup(int linesPerGroup) {
169: if (isImmutable())
170: return;
171: this .linesPerGroup = Math.max(1, linesPerGroup);
172: fireStateChanged();
173: }
174:
175: public static class UIAlternateRowHighlighter extends
176: AlternateRowHighlighter implements UIHighlighter {
177:
178: private HashMap<Color, Color> colorMap;
179:
180: public UIAlternateRowHighlighter() {
181: super (Color.WHITE, null, null);
182: initColorMap();
183: updateUI();
184: }
185:
186: public void updateUI() {
187:
188: Color selection = UIManager
189: .getColor("Table.selectionBackground");
190: Color highlight = getMappedColor(selection);
191:
192: setEvenRowBackground(highlight);
193: }
194:
195: private Color getMappedColor(Color selection) {
196: Color color = colorMap.get(selection);
197: if (color == null) {
198: color = AlternateRowHighlighter.genericGrey
199: .getEvenRowBackground();
200: }
201: return color;
202: }
203:
204: /**
205: * this is a hack until we can think about something better!
206: * we map all known selection colors to highlighter colors.
207: *
208: */
209: private void initColorMap() {
210: colorMap = new HashMap<Color, Color>();
211: // Ocean
212: colorMap.put(new Color(184, 207, 229), new Color(230, 238,
213: 246));
214: // xp blue
215: colorMap.put(new Color(49, 106, 197), new Color(224, 233,
216: 246));
217: // xp silver
218: colorMap.put(new Color(178, 180, 191), new Color(235, 235,
219: 236));
220: // xp olive
221: colorMap.put(new Color(147, 160, 112), new Color(228, 231,
222: 219));
223: // win classic
224: colorMap.put(new Color(10, 36, 106), new Color(218, 222,
225: 233));
226: // win 2k?
227: colorMap
228: .put(new Color(0, 0, 128), new Color(218, 222, 233));
229: // default metal
230: colorMap.put(new Color(205, 205, 255), new Color(235, 235,
231: 255));
232: // mac OS X
233: colorMap.put(new Color(56, 117, 215), new Color(237, 243,
234: 254));
235:
236: }
237:
238: }
239:
240: }
|