KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: HighlighterPipeline.java,v 1.4 2004/10/14 01:28:20 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.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.EventListener JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import java.awt.Component JavaDoc;
17
18 import javax.swing.event.EventListenerList JavaDoc;
19 import javax.swing.event.TableModelEvent JavaDoc;
20 import javax.swing.event.TableModelListener JavaDoc;
21
22 /**
23  * A class which manages the lists of highlighters.
24  *
25  * @see Highlighter
26  *
27  * @author Ramesh Gupta
28  */

29 public class HighlighterPipeline {
30     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
31
32     private List JavaDoc highlighters;
33     private final static Highlighter nullHighlighter = new Highlighter(null, null);
34
35     public HighlighterPipeline(Highlighter[] inList) {
36         // always returns a new copy of inList
37
// XXX seems like there is too much happening here
38
List JavaDoc copy = Arrays.asList((Highlighter[])inList.clone());
39         highlighters = new ArrayList JavaDoc(copy.size());
40         highlighters.addAll(copy);
41
42         reindexHighlighters();
43     }
44
45     private void reindexHighlighters() {
46         Iterator JavaDoc iter = highlighters.iterator();
47         int i = 0;
48         while (iter.hasNext()) {
49             Highlighter hl = (Highlighter)iter.next();
50             hl.order = i++;
51         }
52     }
53
54     /**
55      * Appends a highlighter to the pipeline.
56      *
57      * @param hl highlighter to add
58      */

59     public void addHighlighter(Highlighter hl) {
60         addHighlighter(hl, false);
61     }
62
63     /**
64      * Adds a highlighter to the pipeline.
65      *
66      * @param hl highlighter to add
67      * @param prepend prepend the highlighter if true; false will append
68      */

69     public void addHighlighter(Highlighter hl, boolean prepend) {
70         if (highlighters == null) {
71             highlighters = new ArrayList JavaDoc();
72         }
73         if (prepend) {
74             highlighters.add(0, hl);
75         } else {
76             highlighters.add(highlighters.size(), hl);
77         }
78         reindexHighlighters();
79     }
80
81     /**
82      * Removes a highlighter from the pipeline.
83      *
84      * @param hl highlighter to remove
85      */

86     public void removeHighlighter(Highlighter hl) {
87         boolean success = highlighters.remove(hl);
88         // should log if this didn't succeed. Maybe
89
}
90
91     public Highlighter[] getHighlighters() {
92         return (Highlighter[])highlighters.toArray(new Highlighter[highlighters.size()]);
93     }
94
95     public EventListener JavaDoc[] getListeners(Class JavaDoc listenerType) {
96         return listenerList.getListeners(listenerType);
97     }
98
99     /**
100      * Adds a listener to the list that's notified each time there is a change
101      * to the pipeline.
102      *
103      * @param l the <code>PipelineListener</code> to be added
104      */

105     public void addPipelineListener(PipelineListener l) {
106         listenerList.add(PipelineListener.class, l);
107     }
108
109     /**
110      * Removes a listener from the list that's notified each time there is a change
111      * to the pipeline.
112      *
113      * @param l the <code>PipelineListener</code> to be removed
114      */

115     public void removePipelineListener(PipelineListener l) {
116         listenerList.remove(PipelineListener.class, l);
117     }
118
119     /**
120      * Returns an array of all the pipeline listeners
121      * registered on this <code>HighlighterPipeline</code>.
122      *
123      * @return all of this pipeline's <code>PipelineListener</code>s,
124      * or an empty array if no pipeline listeners
125      * are currently registered
126      *
127      * @see #addPipelineListener
128      * @see #removePipelineListener
129      */

130     public PipelineListener[] getPipelineListeners() {
131         return (PipelineListener[]) listenerList.getListeners(
132             PipelineListener.class);
133     }
134
135     protected void fireContentsChanged(Object JavaDoc source) {
136         Object JavaDoc[] listeners = listenerList.getListenerList();
137         PipelineEvent e = null;
138
139         for (int i = listeners.length - 2; i >= 0; i -= 2) {
140             if (listeners[i] == PipelineListener.class) {
141                 if (e == null) {
142                     e = new PipelineEvent(source, PipelineEvent.CONTENTS_CHANGED);
143                 }
144                 ( (PipelineListener) listeners[i + 1]).contentsChanged(e);
145             }
146         }
147     }
148
149     /**
150      * Applies all the highlighters to the components.
151      */

152     public Component JavaDoc apply(Component JavaDoc stamp, ComponentAdapter adapter) {
153         /** @todo optimize the following bug fix */
154         stamp = nullHighlighter.highlight(stamp, adapter); // fixed bug from M1
155

156         Iterator JavaDoc iter = highlighters.iterator();
157         while (iter.hasNext()) {
158             Highlighter hl = (Highlighter)iter.next();
159             stamp = hl.highlight(stamp, adapter);
160         }
161         return stamp;
162     }
163 }
164
Popular Tags