KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > ColumnPropertyHighlighter


1 /*
2  * $Id: ColumnPropertyHighlighter.java,v 1.2 2004/10/14 01:28:21 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.jdnc;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Font JavaDoc;
13
14 import java.util.HashMap JavaDoc;
15
16 import org.jdesktop.swing.decorator.AlternateRowHighlighter;
17 import org.jdesktop.swing.decorator.ComponentAdapter;
18
19 /*
20  * package-private class for implementing a highlighter which can
21  * customize the foreground, background, and font for cells on a column
22  * basis.
23  */

24 class ColumnPropertyHighlighter extends AlternateRowHighlighter {
25
26     private HashMap JavaDoc columnProperties = new HashMap JavaDoc();
27
28     public ColumnPropertyHighlighter() {
29         super(null, null, null);
30     }
31
32     public void setColumnBackground(String JavaDoc columnName, Color JavaDoc background) {
33         if (background != null) {
34             columnProperties.put(columnName + ".background", background);
35         }
36         else {
37             columnProperties.remove(columnName + ".background");
38         }
39     }
40
41     public Color JavaDoc getColumnBackground(String JavaDoc columnName) {
42         return (Color JavaDoc) columnProperties.get(columnName + ".background");
43     }
44
45     public void setColumnForeground(String JavaDoc columnName, Color JavaDoc foreground) {
46         if (foreground != null) {
47             columnProperties.put(columnName + ".foreground", foreground);
48         }
49         else {
50             columnProperties.remove(columnName + ".foreground");
51         }
52     }
53
54     public Color JavaDoc getColumnForeground(String JavaDoc columnName) {
55         return (Color JavaDoc) columnProperties.get(columnName + ".foreground");
56     }
57
58     public void setColumnFont(String JavaDoc columnName, Font JavaDoc font) {
59         if (font != null) {
60             columnProperties.put(columnName + ".font", font);
61         }
62         else {
63             columnProperties.remove(columnName + ".font");
64         }
65     }
66
67     public Font JavaDoc getColumnFont(String JavaDoc columnName) {
68         return (Font JavaDoc) columnProperties.get(columnName + ".font");
69     }
70
71     protected void applyFont(Component JavaDoc renderer, ComponentAdapter adapter) {
72         Font JavaDoc font = getColumnFont(adapter.getColumnName(adapter.column));
73         if (font != null) {
74             renderer.setFont(font);
75         }
76     }
77
78     protected Color JavaDoc computeBackground(Component JavaDoc renderer,
79                                       ComponentAdapter adapter) {
80         Color JavaDoc seed = getColumnBackground(adapter.getColumnName(adapter.column));
81         if (seed == null) {
82             seed = (adapter.row % 2) == 0 ?
83                 getOddRowBackground() : getEvenRowBackground();
84         }
85         if (seed == null) {
86             seed = getBackground() == null ? adapter.getComponent().getBackground() :
87                 getBackground();
88         }
89         return adapter.isSelected() ? computeSelectedBackground(seed) : seed;
90     }
91
92     protected Color JavaDoc computeForeground(Component JavaDoc renderer,
93                                       ComponentAdapter adapter) {
94         Color JavaDoc seed = getColumnForeground(adapter.getColumnName(adapter.column));
95         if (seed == null) {
96             seed = getForeground() == null ? adapter.getComponent().getForeground() :
97                 getForeground();
98         }
99         return adapter.isSelected() ? computeSelectedForeground(seed) : seed;
100
101     }
102 }
103
Popular Tags