2
* $Id: CompoundHighlighter.java 3691 2010-05-03 18:03:44Z kschaefe $
4
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx.decorator;
24
import java.awt.Component;
25
import java.util.ArrayList;
26
import java.util.List;
28
import javax.swing.event.ChangeEvent;
29
import javax.swing.event.ChangeListener;
31
import org.jdesktop.swingx.plaf.UIDependent;
32
import org.jdesktop.swingx.util.Contract;
35
* A class which manages the lists of <code>Highlighter</code>s.
39
* @author Ramesh Gupta
40
* @author Jeanette Winzenburg
43
public class CompoundHighlighter extends AbstractHighlighter
44
implements UIDependent {
45
public static final Highlighter[] EMPTY_HIGHLIGHTERS = new Highlighter[0];
47
protected List<Highlighter> highlighters;
49
/** the listener for changes in contained Highlighters. */
50
private ChangeListener highlighterChangeListener;
54
* Instantiates a CompoundHighlighter containing the given
55
* <code>Highlighter</code>s.
57
* @param inList zero or more not-null Highlighters to manage by this
58
* CompoundHighlighter.
59
* @throws NullPointerException if array is null or array contains null values.
61
public CompoundHighlighter(Highlighter... inList) {
66
* Instantiates a CompoundHighlighter with the given predicate containing the given
67
* <code>Highlighter</code>s.
69
* @param predicate the highlightPredicate to use
70
* @param inList zero or more not-null Highlighters to manage by this
71
* CompoundHighlighter.
72
* @throws NullPointerException if array is null or array contains null values.
74
public CompoundHighlighter(HighlightPredicate predicate, Highlighter... inList) {
76
highlighters = new ArrayList<Highlighter>();
77
setHighlighters(inList);
82
* <code>Highlighter</code>s.
84
* @param inList zero or more not-null Highlighters to manage by this
85
* CompoundHighlighter.
86
* @throws NullPointerException if array is null or array contains null values.
88
public void setHighlighters(Highlighter... inList) {
89
Contract.asNotNull(inList, "Highlighter must not be null");
90
if (highlighters.isEmpty() && (inList.length == 0)) return;
91
removeAllHighlightersSilently();
92
for (Highlighter highlighter : inList) {
93
addHighlighterSilently(highlighter, false);
99
* Removes all contained highlighters without firing an event.
100
* Deregisters the listener from all.
102
private void removeAllHighlightersSilently() {
103
for (Highlighter highlighter : highlighters) {
104
highlighter.removeChangeListener(getHighlighterChangeListener());
106
highlighters.clear();
110
* Appends a highlighter to the pipeline.
112
* @param highlighter highlighter to add
113
* @throws NullPointerException if highlighter is null.
115
public void addHighlighter(Highlighter highlighter) {
116
addHighlighter(highlighter, false);
120
* Adds a highlighter to the pipeline.
122
* PENDING: Duplicate inserts?
124
* @param highlighter highlighter to add
125
* @param prepend prepend the highlighter if true; false will append
126
* @throws NullPointerException if highlighter is null.
128
public void addHighlighter(Highlighter highlighter, boolean prepend) {
129
addHighlighterSilently(highlighter, prepend);
133
private void addHighlighterSilently(Highlighter highlighter, boolean prepend) {
134
Contract.asNotNull(highlighter, "Highlighter must not be null");
136
highlighters.add(0, highlighter);
138
highlighters.add(highlighters.size(), highlighter);
140
updateUI(highlighter);
141
highlighter.addChangeListener(getHighlighterChangeListener());
145
* Removes a highlighter from the pipeline.
148
* @param hl highlighter to remove
150
public void removeHighlighter(Highlighter hl) {
151
boolean success = highlighters.remove(hl);
153
// PENDING: duplicates?
154
hl.removeChangeListener(getHighlighterChangeListener());
157
// should log if this didn't succeed. Maybe
161
* Returns an array of contained Highlighters.
163
* @return the contained Highlighters, might be empty but never null.
165
public Highlighter[] getHighlighters() {
166
if (highlighters.isEmpty()) return EMPTY_HIGHLIGHTERS;
167
return highlighters.toArray(new Highlighter[highlighters.size()]);
170
//--------------------- implement UIDependent
175
* Implemented to call updateUI on contained Highlighters.
177
public void updateUI() {
178
for (Highlighter highlighter : highlighters) {
179
updateUI(highlighter);
184
* Returns the <code>ChangeListner</code> to contained
185
* <code>Highlighter</code>s. The listener is lazily created.
187
* @return the listener for contained highlighters, guaranteed
190
protected ChangeListener getHighlighterChangeListener() {
191
if (highlighterChangeListener == null) {
192
highlighterChangeListener = createHighlighterChangeListener();
194
return highlighterChangeListener;
198
* Creates and returns the ChangeListener registered to
199
* contained <code>Highlighter</code>s. Here: fires a
200
* stateChanged on each notification.
202
* @return the listener for contained Highlighters.
205
protected ChangeListener createHighlighterChangeListener() {
206
return highlighterChangeListener = new ChangeListener() {
208
public void stateChanged(ChangeEvent e) {
216
* Updates the ui-dependent state of the given Highlighter.
218
* @param hl the highlighter to update.
220
private void updateUI(Highlighter hl) {
221
if (hl instanceof UIDependent) {
222
((UIDependent) hl).updateUI();
227
//------------------- implement Highlighter
233
protected Component doHighlight(Component stamp, ComponentAdapter adapter) {
234
for (Highlighter highlighter : highlighters) {
235
stamp = highlighter.highlight(stamp, adapter);