KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > MarkerRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.views.markers.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.resources.IMarkerDelta;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceChangeEvent;
22 import org.eclipse.core.resources.IResourceChangeListener;
23 import org.eclipse.core.resources.IResourceDelta;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.util.ListenerList;
26
27 /**
28  * Registry that tracks resource markers and maintains a sorted, filtered list of the markers.
29  * Notifies listeners of changes in these markers.
30  */

31 public class MarkerRegistry implements IResourceChangeListener,
32         ITableViewContentProvider {
33
34     private IFilter filter;
35
36     private IResource input;
37
38     private String JavaDoc[] types = new String JavaDoc[0];
39
40     private ListenerList listeners = new ListenerList();
41
42     public MarkerRegistry() {
43     }
44
45     /**
46      * Disposes the registry, releasing all listeners
47      * and any other allocated resources.
48      */

49     public void dispose() {
50         listeners.clear();
51         setInput(null);
52     }
53
54     /**
55      * @return a filtered, sorted list of markers.
56      */

57     public Object JavaDoc[] getElements() {
58         Object JavaDoc[] elements = getUnfilteredElements();
59         if (filter != null) {
60             Object JavaDoc[] filteredItems = filter.filter(elements);
61             return filteredItems;
62         }
63         return elements;
64     }
65
66     /**
67      * @return an unfiltered list of elements
68      */

69     public Object JavaDoc[] getUnfilteredElements() {
70         if (input == null) {
71             return new Object JavaDoc[0];
72         }
73         List JavaDoc elements = new ArrayList JavaDoc();
74         for (int i = 0; i < types.length; i++) {
75             try {
76                 IMarker[] newMarkers = input.findMarkers(types[i], true,
77                         IResource.DEPTH_INFINITE);
78                 elements.addAll(Arrays.asList(newMarkers));
79             } catch (CoreException e) {
80             }
81         }
82         return elements.toArray();
83     }
84
85     /**
86      * @return the registry's filter or <code>null</code> if no filter has been assigned
87      * to the registry.
88      */

89     public IFilter getFilter() {
90         return filter;
91     }
92
93     /**
94      * @return the registry's input resource
95      */

96     public IResource getInput() {
97         return input;
98     }
99
100     /**
101      * Sets the registry's filter
102      *
103      * @param filter
104      */

105     public void setFilter(IFilter filter) {
106         if (this.filter == null || !this.filter.equals(filter)) {
107             this.filter = filter;
108         }
109     }
110
111     /**
112      * Sets the registry's input resource
113      *
114      * @param resource
115      */

116     public void setInput(IResource resource) {
117         if (input != null) {
118             if (input.equals(resource))
119                 return;
120             input.getWorkspace().removeResourceChangeListener(this);
121         }
122         input = resource;
123         if (input != null)
124             resource.getWorkspace().addResourceChangeListener(this);
125     }
126
127     /**
128      * @return the base marker types that the registry is tracking
129      */

130     public String JavaDoc[] getTypes() {
131         return types;
132     }
133
134     /**
135      * Sets the base marker types to track. By default the registry will search for
136      * all markers of these types and their subtypes.
137      *
138      * @param types
139      */

140     public void setTypes(String JavaDoc[] types) {
141         if (types == null)
142             this.types = new String JavaDoc[0];
143         else
144             this.types = types;
145     }
146
147     /**
148      * Convenience method used if only interested in one base marker type.
149      *
150      * @param type
151      */

152     public void setType(String JavaDoc type) {
153         setTypes(new String JavaDoc[] { type });
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
158      */

159     public void resourceChanged(IResourceChangeEvent event) {
160
161         // gather all marker changes from the delta.
162
// be sure to do this in the calling thread,
163
// as the delta is destroyed when this method returns
164
final List JavaDoc additions = new ArrayList JavaDoc();
165         final List JavaDoc removals = new ArrayList JavaDoc();
166         final List JavaDoc changes = new ArrayList JavaDoc();
167
168         IResourceDelta delta = event.getDelta();
169         if (delta == null)
170             return;
171         getMarkerDeltas(delta, additions, removals, changes);
172         //filter additions and changes but not removals since they have already been deleted
173
filterList(additions);
174         filterList(changes);
175         notifyListeners(additions, removals, changes);
176     }
177
178     /**
179      * Recursively walks over the resource delta and gathers all marker deltas. Marker
180      * deltas are placed into one of the two given lists depending on the type of delta
181      * (add or remove).
182      */

183     private void getMarkerDeltas(IResourceDelta delta, List JavaDoc additions,
184             List JavaDoc removals, List JavaDoc changes) {
185         IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();
186         for (int i = 0; i < markerDeltas.length; i++) {
187             IMarkerDelta markerDelta = markerDeltas[i];
188             IMarker marker = markerDelta.getMarker();
189             switch (markerDelta.getKind()) {
190             case IResourceDelta.ADDED: {
191                 boolean added = false;
192                 for (int j = 0; j < types.length && !added; j++) {
193                     if (markerDelta.isSubtypeOf(types[j])) {
194                         additions.add(marker);
195                         added = true;
196                     }
197                 }
198                 break;
199             }
200             case IResourceDelta.REMOVED: {
201                 boolean added = false;
202                 for (int j = 0; j < types.length && !added; j++) {
203                     if (markerDelta.isSubtypeOf(types[j])) {
204                         removals.add(marker);
205                         added = true;
206                     }
207                 }
208                 break;
209             }
210             case IResourceDelta.CHANGED: {
211                 boolean added = false;
212                 for (int j = 0; j < types.length && !added; j++) {
213                     if (markerDelta.isSubtypeOf(types[j])) {
214                         changes.add(marker);
215                         added = true;
216                     }
217                 }
218                 break;
219             }
220             }
221         }
222
223         //recurse on child deltas
224
IResourceDelta[] children = delta.getAffectedChildren();
225         for (int i = 0; i < children.length; i++) {
226             getMarkerDeltas(children[i], additions, removals, changes);
227         }
228     }
229
230     private void notifyListeners(List JavaDoc additions, List JavaDoc removals, List JavaDoc changes) {
231         Object JavaDoc[] listeners = this.listeners.getListeners();
232         for (int i = 0; i < listeners.length; i++) {
233             IItemsChangedListener listener = (IItemsChangedListener) listeners[i];
234             listener.itemsChanged(additions, removals, changes);
235         }
236     }
237
238     /* (non-Javadoc)
239      * @see org.eclipse.ui.views.internal.tableview.ITableViewContentProvider#addItemsChangedListener(org.eclipse.ui.views.internal.tableview.IItemsChangedListener)
240      */

241     public void addItemsChangedListener(IItemsChangedListener listener) {
242         listeners.add(listener);
243     }
244
245     /* (non-Javadoc)
246      * @see org.eclipse.ui.views.internal.tableview.ITableViewContentProvider#removeItemsChangedListener(org.eclipse.ui.views.internal.tableview.IItemsChangedListener)
247      */

248     public void removeItemsChangedListener(IItemsChangedListener listener) {
249         listeners.remove(listener);
250     }
251
252     /**
253      * @return the number of items
254      */

255     public int getItemCount() {
256         //TODO do this more efficiently
257
return getElements().length;
258     }
259
260     public int getRawItemCount() {
261         //TODO do this more efficiently
262
return getUnfilteredElements().length;
263     }
264
265     private void filterList(List JavaDoc list) {
266         if (filter == null || list == null) {
267             return;
268         }
269         int i = 0;
270         while (i < list.size()) {
271             if (filter.select(list.get(i))) {
272                 i++;
273             } else {
274                 list.remove(i);
275             }
276         }
277     }
278
279 }
280
Popular Tags