~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/decorator/CompoundHighlighter.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: CompoundHighlighter.java 3691 2010-05-03 18:03:44Z kschaefe $
3
 
 *
4
 
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5
 
 * Santa Clara, California 95054, U.S.A. All rights reserved.
6
 
 *
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.
11
 
 * 
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.
16
 
 * 
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
20
 
 */
21
 
 
22
 
package org.jdesktop.swingx.decorator;
23
 
 
24
 
import java.awt.Component;
25
 
import java.util.ArrayList;
26
 
import java.util.List;
27
 
 
28
 
import javax.swing.event.ChangeEvent;
29
 
import javax.swing.event.ChangeListener;
30
 
 
31
 
import org.jdesktop.swingx.plaf.UIDependent;
32
 
import org.jdesktop.swingx.util.Contract;
33
 
 
34
 
/**
35
 
 * A class which manages the lists of <code>Highlighter</code>s.
36
 
 *
37
 
 * @see Highlighter
38
 
 *
39
 
 * @author Ramesh Gupta
40
 
 * @author Jeanette Winzenburg
41
 
 * 
42
 
 */
43
 
public class CompoundHighlighter extends AbstractHighlighter 
44
 
    implements UIDependent {
45
 
    public static final Highlighter[] EMPTY_HIGHLIGHTERS = new Highlighter[0];
46
 
 
47
 
    protected List<Highlighter> highlighters;
48
 
    
49
 
    /** the listener for changes in contained Highlighters. */
50
 
    private ChangeListener highlighterChangeListener;
51
 
 
52
 
    
53
 
    /**
54
 
     * Instantiates a CompoundHighlighter containing the given 
55
 
     * <code>Highlighter</code>s. 
56
 
     * 
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.
60
 
     */
61
 
    public CompoundHighlighter(Highlighter... inList) {
62
 
        this(null, inList);
63
 
    }
64
 
 
65
 
    /**
66
 
     * Instantiates a CompoundHighlighter with the given predicate containing the given 
67
 
     * <code>Highlighter</code>s. 
68
 
     * 
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.
73
 
     */
74
 
    public CompoundHighlighter(HighlightPredicate predicate, Highlighter... inList) {
75
 
        super(predicate);
76
 
        highlighters = new ArrayList<Highlighter>();
77
 
        setHighlighters(inList);
78
 
    }
79
 
 
80
 
    /**
81
 
     * Sets the given 
82
 
     * <code>Highlighter</code>s. 
83
 
     * 
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.
87
 
     */
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);
94
 
        }
95
 
        fireStateChanged();
96
 
    }
97
 
 
98
 
    /**
99
 
     * Removes all contained highlighters without firing an event.
100
 
     * Deregisters the listener from all.
101
 
     */
102
 
    private void removeAllHighlightersSilently() {
103
 
        for (Highlighter highlighter : highlighters) {
104
 
            highlighter.removeChangeListener(getHighlighterChangeListener());
105
 
        }
106
 
        highlighters.clear();
107
 
    }
108
 
 
109
 
    /**
110
 
     * Appends a highlighter to the pipeline.
111
 
     *
112
 
     * @param highlighter highlighter to add
113
 
      * @throws NullPointerException if highlighter is null.
114
 
    */
115
 
    public void addHighlighter(Highlighter highlighter) {
116
 
        addHighlighter(highlighter, false);
117
 
    }
118
 
 
119
 
    /**
120
 
     * Adds a highlighter to the pipeline.
121
 
     *
122
 
     * PENDING: Duplicate inserts?
123
 
     * 
124
 
     * @param highlighter highlighter to add
125
 
     * @param prepend prepend the highlighter if true; false will append
126
 
     * @throws NullPointerException if highlighter is null.
127
 
     */
128
 
    public void addHighlighter(Highlighter highlighter, boolean prepend) {
129
 
        addHighlighterSilently(highlighter, prepend);
130
 
        fireStateChanged();
131
 
    }
132
 
 
133
 
    private void addHighlighterSilently(Highlighter highlighter, boolean prepend) {
134
 
        Contract.asNotNull(highlighter, "Highlighter must not be null");
135
 
        if (prepend) {
136
 
            highlighters.add(0, highlighter);
137
 
        } else {
138
 
            highlighters.add(highlighters.size(), highlighter);
139
 
        }
140
 
        updateUI(highlighter);
141
 
        highlighter.addChangeListener(getHighlighterChangeListener());
142
 
    }
143
 
 
144
 
    /**
145
 
     * Removes a highlighter from the pipeline.
146
 
     *
147
 
     *  
148
 
     * @param hl highlighter to remove
149
 
     */
150
 
    public void removeHighlighter(Highlighter hl) {
151
 
        boolean success = highlighters.remove(hl);
152
 
        if (success) {
153
 
            // PENDING: duplicates?
154
 
            hl.removeChangeListener(getHighlighterChangeListener());
155
 
            fireStateChanged();
156
 
        }
157
 
        // should log if this didn't succeed. Maybe
158
 
    }
159
 
 
160
 
    /**
161
 
     * Returns an array of contained Highlighters.
162
 
     * 
163
 
     * @return the contained Highlighters, might be empty but never null.
164
 
     */
165
 
    public Highlighter[] getHighlighters() {
166
 
        if (highlighters.isEmpty()) return EMPTY_HIGHLIGHTERS;
167
 
        return highlighters.toArray(new Highlighter[highlighters.size()]);
168
 
    }
169
 
 
170
 
//--------------------- implement UIDependent
171
 
 
172
 
    /**
173
 
     * {@inheritDoc} <p>
174
 
     * 
175
 
     * Implemented to call updateUI on contained Highlighters.
176
 
     */
177
 
    public void updateUI() {
178
 
        for (Highlighter highlighter : highlighters) {
179
 
            updateUI(highlighter);
180
 
        }
181
 
    }   
182
 
 
183
 
    /**
184
 
     * Returns the <code>ChangeListner</code> to contained
185
 
     * <code>Highlighter</code>s. The listener is lazily created.
186
 
     *  
187
 
     * @return the listener for contained highlighters, guaranteed
188
 
     *   to be not null.
189
 
     */
190
 
    protected ChangeListener getHighlighterChangeListener() {
191
 
        if (highlighterChangeListener == null) {
192
 
            highlighterChangeListener = createHighlighterChangeListener();
193
 
        }
194
 
        return highlighterChangeListener;
195
 
    }
196
 
 
197
 
    /**
198
 
     * Creates and returns the ChangeListener registered to
199
 
     * contained <code>Highlighter</code>s. Here: fires a 
200
 
     * stateChanged on each notification. 
201
 
     * 
202
 
     * @return the listener for contained Highlighters.
203
 
     * 
204
 
     */
205
 
    protected ChangeListener createHighlighterChangeListener() {
206
 
        return highlighterChangeListener = new ChangeListener() {
207
 
 
208
 
            public void stateChanged(ChangeEvent e) {
209
 
                fireStateChanged();
210
 
            }
211
 
            
212
 
        };
213
 
    }
214
 
 
215
 
   /**
216
 
    *  Updates the ui-dependent state of the given Highlighter.
217
 
    *  
218
 
     * @param hl the highlighter to update.
219
 
     */
220
 
    private void updateUI(Highlighter hl) {
221
 
        if (hl instanceof UIDependent) {
222
 
            ((UIDependent) hl).updateUI();
223
 
        } 
224
 
    }
225
 
 
226
 
 
227
 
//------------------- implement Highlighter    
228
 
 
229
 
    /**
230
 
     * {@inheritDoc}
231
 
     */
232
 
    @Override
233
 
    protected Component doHighlight(Component stamp, ComponentAdapter adapter) {
234
 
        for (Highlighter highlighter : highlighters) {
235
 
            stamp = highlighter.highlight(stamp, adapter);
236
 
        }
237
 
        return stamp;
238
 
    }
239
 
    
240
 
    
241
 
 
242
 
}