KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ConditionalHighlighter.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  * ConditionalHighlighter
15  *
16  * @author Ramesh Gupta
17  */

18 public abstract class ConditionalHighlighter extends Highlighter {
19     protected int testColumn = 0; // always in model coordinates
20
protected int highlightColumn = -1; // always in model coordinates
21
protected int mask = 255;
22
23     public ConditionalHighlighter() {
24         // default constructor
25
}
26
27     /**
28      * <p>Constructs a <code>ConditionalHighlighter</code> instance with the
29      * specified background and foreground colors that will be used to highlight
30      * the renderer component for a cell in the specified highlightColumn of any
31      * row if and only if {@link #needsHighlight needsHighlight} returns true
32      * for the adapter that identifies that cell.</p>
33      *
34      * @param cellBackground background color for highlighted cells, or null, if
35      * background should not be changed
36      * @param cellForeground foreground color for highlighted cells, or null, if
37      * foreground should not be changed
38      * @param testColumn column whose value is to be tested to determine if a
39      * cell <em>should</em> be highlighted
40      * @param highlightColumn column whose index is used to determine if a cell
41      * <em>could</em> be highlighted; may be a valid column index in model
42      * coordinates, or -1 to indicate all columns
43      */

44     public ConditionalHighlighter(Color JavaDoc cellBackground, Color JavaDoc cellForeground,
45                                   int testColumn, int highlightColumn) {
46         super(cellBackground, cellForeground);
47         this.testColumn = testColumn;
48         this.highlightColumn = highlightColumn;
49     }
50
51     public void setMask(int alpha) {
52         mask = alpha;
53     }
54
55     public int getMask() {
56         return mask;
57     }
58
59     /**
60      * Performs a conditional highlight. Calls {@link #doHighlight doHighlight} if
61      * and only if {@link #needsHighlight needsHighlight} returns true.
62      *
63      * @param renderer
64      * @param adapter
65      * @return the highlighted component
66      */

67     public Component JavaDoc highlight(Component JavaDoc renderer, ComponentAdapter adapter) {
68         if (needsHighlight(adapter)) {
69             return doHighlight(renderer, adapter);
70         }
71         else if (getMask() < 256) {
72             return doMask(renderer, adapter);
73         }
74         return renderer;
75     }
76
77     protected Component JavaDoc doMask(Component JavaDoc renderer, ComponentAdapter adapter) {
78
79         maskBackground(renderer, adapter);
80         maskForeground(renderer, adapter);
81         // and so on...
82

83         return renderer;
84     }
85
86     protected void maskBackground(Component JavaDoc renderer, ComponentAdapter adapter) {
87         Color JavaDoc seed = renderer.getBackground();
88         Color JavaDoc color = adapter.isSelected() ? computeSelectedBackground(seed) : seed;
89         renderer.setBackground(
90                                new Color JavaDoc((getMask() << 24) | (color.getRGB() & 0x00FFFFFF), true));
91     }
92
93     protected void maskForeground(Component JavaDoc renderer, ComponentAdapter adapter) {
94         Color JavaDoc seed = renderer.getForeground();
95         Color JavaDoc color = adapter.isSelected() ? computeSelectedForeground(seed) : seed;
96         renderer.setForeground(
97                                new Color JavaDoc((getMask() << 24) | (color.getRGB() & 0x00FFFFFF), true));
98     }
99
100     /**
101      *
102      * @param renderer
103      * @param adapter
104      * @return null if the background is null; otherwise delegate to superclass
105      */

106     protected Color JavaDoc computeBackground(Component JavaDoc renderer, ComponentAdapter adapter) {
107         return getBackground() == null ? null :
108             super.computeBackground(renderer, adapter);
109     }
110
111     /**
112      *
113      * @param renderer
114      * @param adapter
115      * @return null if the foreground is null; otherwise delegate to superclass
116      */

117     protected Color JavaDoc computeForeground(Component JavaDoc renderer, ComponentAdapter adapter) {
118         return getForeground() == null ? null :
119             super.computeForeground(renderer, adapter);
120     }
121
122     protected Color JavaDoc computeSelectedForeground(Color JavaDoc seed) {
123         return getSelectedForeground() == null ? seed.brighter() : getSelectedForeground();
124     }
125
126     public int getTestColumnIndex() {
127         return testColumn;
128     }
129
130     public void setTestColumnIndex(int columnIndex) {
131         this.testColumn = columnIndex;
132     }
133
134     public int getHighlightColumnIndex() {
135         return highlightColumn;
136     }
137
138     public void setHighlightColumnIndex(int columnIndex) {
139         this.highlightColumn = columnIndex;
140     }
141
142     /**
143      * Checks if the cell identified by the specified adapter is a potential
144      * candidate for highlighting, and returns true if so; otherwise, it returns false.
145      *
146      * @param adapter
147      * @return true if the cell identified by the specified adapter needs
148      * highlight; false otherwise
149      */

150     protected boolean needsHighlight(ComponentAdapter adapter) {
151         // Before running test(), quickly check if the cell in the current
152
// adapter is a potential candidate for highlighting. If so, run the test.
153
// highlightColumn is always in "model" coordinates, but adapter.column
154
// is in "view" coordinates. So, convert before comparing.
155
if ((highlightColumn < 0) ||
156             (adapter.modelToView(highlightColumn) == adapter.column)) {
157             return test(adapter);
158         }
159         return false; // cell is not a candidate for decoration;
160
}
161
162     /**
163      * Tests if the cell identified by the specified adapter should actually be
164      * highlighted, and returns true if so; otherwise, it returns false.
165      *
166      * @param adapter
167      * @return true if the test succeeds; false otherwise
168      */

169     protected abstract boolean test(ComponentAdapter adapter);
170
171 }
172
Popular Tags