KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > highlight > HighlightManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.highlight;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.WeakHashMap JavaDoc;
30 import org.netbeans.modules.xml.xam.Component;
31 import org.openide.util.WeakListeners;
32
33 /**
34  * Manages the active set of HighlightGroup instances, as well as the
35  * Highlighted implementations that interpret the active highlights.
36  *
37  * @author Nathan Fiedler
38  */

39 public abstract class HighlightManager {
40     /** Default global highlight manager instance. */
41     private static final HighlightManager defaultInstance;
42     /** Registered (weak) highlight listeners. */
43     private List JavaDoc<Highlighted> listeners;
44     /** Registered highlight groups. */
45     private List JavaDoc<HighlightGroup> groups;
46     /** Map of components to the listeners interested in them. */
47     private Map JavaDoc<Component, List JavaDoc<Highlighted>> componentListenerMap;
48
49     static {
50         defaultInstance = new DefaultHighlightManager();
51     }
52
53     /**
54      * Creates a new instance of HighlightManager.
55      */

56     public HighlightManager() {
57         listeners = new ArrayList JavaDoc<Highlighted>();
58         groups = new ArrayList JavaDoc<HighlightGroup>();
59         componentListenerMap = new WeakHashMap JavaDoc<Component, List JavaDoc<Highlighted>>();
60     }
61
62     /**
63      * Adds the given HighlightGroup to this manager.
64      *
65      * @param group HighlightGroup to be added.
66      */

67     public void addHighlightGroup(HighlightGroup group) {
68         synchronized (groups) {
69             groups.add(group);
70         }
71         showHighlights(group);
72     }
73
74     /**
75      * Adds the given Highlighted to this manager. The listener will
76      * be weakly referenced to allow it to be garbage collected.
77      *
78      * @param l Highlighted to be added.
79      */

80     public void addHighlighted(Highlighted l) {
81         Highlighted wl = (Highlighted)
82                 WeakListeners.create(Highlighted.class, l, this);
83         synchronized (listeners) {
84             listeners.add(wl);
85         }
86         synchronized (componentListenerMap) {
87             Set JavaDoc<Component> comps = wl.getComponents();
88             if (comps != null) {
89                 for (Component comp : comps) {
90                     List JavaDoc<Highlighted> list = componentListenerMap.get(comp);
91                     if (list == null) {
92                         list = new LinkedList JavaDoc<Highlighted>();
93                         componentListenerMap.put(comp, list);
94                     }
95                     list.add(wl);
96                 }
97             }
98         }
99         highlight(l);
100     }
101
102     /**
103      * Locate the highlight listeners that are affected by the given group
104      * of highlights.
105      *
106      * @param group highlight group.
107      * @return highlight listeners and the highlights they care about.
108      */

109     protected Map JavaDoc<Highlighted, List JavaDoc<Highlight>> findListeners(
110             HighlightGroup group) {
111         Map JavaDoc<Highlighted, List JavaDoc<Highlight>> map =
112                 new HashMap JavaDoc<Highlighted, List JavaDoc<Highlight>>();
113         synchronized (componentListenerMap) {
114             Iterator JavaDoc<Highlight> iter = group.highlights().iterator();
115             while (iter.hasNext()) {
116                 Highlight h = iter.next();
117                 Component comp = h.getComponent();
118                 List JavaDoc<Highlighted> list = componentListenerMap.get(comp);
119                 if (list != null) {
120                     for (Highlighted l : list) {
121                         List JavaDoc<Highlight> lights = map.get(l);
122                         if (lights == null) {
123                             lights = new ArrayList JavaDoc<Highlight>();
124                             map.put(l, lights);
125                         }
126                         lights.add(h);
127                     }
128                 }
129             }
130         }
131         return map;
132     }
133
134     /**
135      * Returns the global instance of a HighlightManager.
136      *
137      * @return global highlight manager instance.
138      */

139     public static HighlightManager getDefault(){
140         return defaultInstance;
141     }
142
143     /**
144      * Retrieves the highlight groups of the given type, if any.
145      *
146      * @return highlight groups matching type (empty if none).
147      */

148     public List JavaDoc<HighlightGroup> getHighlightGroups(String JavaDoc type) {
149         List JavaDoc<HighlightGroup> list = new ArrayList JavaDoc<HighlightGroup>();
150         synchronized (groups) {
151             if (groups.size() > 0) {
152                 for (HighlightGroup group : groups) {
153                     if (group.getType().equals(type)) {
154                         list.add(group);
155                     }
156                 }
157             }
158         }
159         return list;
160     }
161
162     /**
163      * Deactivate all of the Highlight instances in the group.
164      *
165      * @param group HighlightGroup to be hidden.
166      */

167     protected abstract void hideHighlights(HighlightGroup group);
168
169     /**
170      * Determines if any of the existing highlight groups affect the given
171      * listener or not, and if so, show the highlight for that listener.
172      *
173      * @param listener listener which needs highlighting.
174      */

175     protected void highlight(Highlighted listener) {
176         Set JavaDoc<Component> comps = listener.getComponents();
177         if (comps != null && !comps.isEmpty()) {
178             synchronized (groups) {
179                 for (HighlightGroup group : groups) {
180                     if (group.isShowing()) {
181                         Iterator JavaDoc<Highlight> iter = group.highlights().iterator();
182                         while (iter.hasNext()) {
183                             Highlight h = iter.next();
184                             Component comp = h.getComponent();
185                             if (comps.contains(comp)) {
186                                 listener.highlightAdded(h);
187                             }
188                         }
189                     }
190                 }
191             }
192         }
193     }
194
195     /**
196      * Removes the given HighlightGroup from this manager.
197      *
198      * @param group HighlightGroup to be removed.
199      */

200     public void removeHighlightGroup(HighlightGroup group) {
201         hideHighlights(group);
202         synchronized (groups) {
203             groups.remove(group);
204         }
205     }
206
207     /**
208      * Removes the given Highlighted from this manager. This should only
209      * be called by the NetBeans WeakListeners class, which is used to
210      * wrap the original listener in <code>addHighlighted()</code>.
211      * That is, the listener will be automatically removed when it is no
212      * longer needed.
213      *
214      * @param l Highlighted to be removed.
215      */

216     public void removeHighlighted(Highlighted l) {
217         // Counting on weak listener to delegate equals() to actual listener.
218
synchronized (listeners) {
219             listeners.remove(l);
220         }
221         synchronized (componentListenerMap) {
222             Set JavaDoc<Component> comps = l.getComponents();
223             if (comps != null) {
224                 for (Component comp : comps) {
225                     List JavaDoc<Highlighted> list = componentListenerMap.get(comp);
226                     if (list != null) {
227                         list.remove(l);
228                     }
229                 }
230             }
231         }
232     }
233
234     /**
235      * Activate all of the Highlight instances in the group.
236      *
237      * @param group HighlightGroup to be shown.
238      */

239     protected abstract void showHighlights(HighlightGroup group);
240 }
241
Popular Tags