KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.views.markers.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27
28 /**
29  * Represents a list of ConcreteMarkers.
30  */

31 public class MarkerList {
32     
33     private int[] markerCounts = null;
34
35     private ConcreteMarker[] markers;
36
37     /**
38      * Lazily created marker table - maps IMarkers onto ConcreteMarkers. Null if
39      * not created
40      */

41     private Map JavaDoc markerTable;
42
43     /**
44      * Creates an initially empty marker list
45      */

46     public MarkerList() {
47         this(new ConcreteMarker[0]);
48     }
49
50     public MarkerList(Collection JavaDoc markers) {
51         this((ConcreteMarker[]) markers.toArray(new ConcreteMarker[markers
52                 .size()]));
53     }
54
55     /**
56      * Creates a list containing the given set of markers
57      *
58      * @param markers
59      */

60     public MarkerList(ConcreteMarker[] markers) {
61         this.markers = markers;
62     }
63
64     /**
65      * Clears any cached collation keys. Use to free up some memory if the
66      * markers in this list won't be sorted for awhile.
67      */

68     public void clearCache() {
69         for (int i = 0; i < markers.length; i++) {
70             ConcreteMarker marker = markers[i];
71
72             marker.clearCache();
73         }
74
75         markerTable = null;
76     }
77
78     /**
79      * Returns the marker table or lazily creates it if it doesn't exist yet
80      *
81      * @return a map of IMarker onto ConcreteMarker, containing all the
82      * ConcreteMarkers in the list
83      */

84     private Map JavaDoc getMarkerMap() {
85         if (markerTable == null) {
86             markerTable = new HashMap JavaDoc();
87
88             for (int idx = 0; idx < markers.length; idx++) {
89                 ConcreteMarker marker = markers[idx];
90                 markerTable.put(marker.getMarker(), marker);
91             }
92         }
93
94         return markerTable;
95     }
96
97     /**
98      * Returns an existing marker from the list that is associated with the
99      * given IMarker
100      *
101      * @param toFind
102      * the IMarker to lookup in the list
103      * @return the ConcreteMarker that corresponds to the given IMarker
104      */

105     public ConcreteMarker getMarker(IMarker toFind) {
106         return (ConcreteMarker) getMarkerMap().get(toFind);
107     }
108     
109     /**
110      * Return the list of IMarkers contained in the receiver.
111      * @return IMarker[]
112      */

113     public IMarker[] getIMarkers(){
114         IMarker[] iMarkers = new IMarker[markers.length];
115         for (int i = 0; i < markers.length; i++) {
116             iMarkers[i] = markers[i].getMarker();
117         }
118         return iMarkers;
119     }
120
121     public static ConcreteMarker createMarker(IMarker marker)
122             throws CoreException {
123         if (marker.isSubtypeOf(IMarker.TASK)) {
124             return new TaskMarker(marker);
125         } else if (marker.isSubtypeOf(IMarker.BOOKMARK)) {
126             return new BookmarkMarker(marker);
127         } else if (marker.isSubtypeOf(IMarker.PROBLEM)) {
128             return new ProblemMarker(marker);
129         } else {
130             return new ConcreteMarker(marker);
131         }
132     }
133
134     public void refresh() {
135         for (int markerIdx = 0; markerIdx < markers.length; markerIdx++) {
136             ConcreteMarker next = markers[markerIdx];
137             next.refresh();
138         }
139     }
140
141     public List JavaDoc asList() {
142         return Arrays.asList(markers);
143     }
144
145     public MarkerList findMarkers(Collection JavaDoc ofIMarker) {
146         List JavaDoc result = new ArrayList JavaDoc(ofIMarker.size());
147
148         Iterator JavaDoc iter = ofIMarker.iterator();
149         while (iter.hasNext()) {
150             IMarker next = (IMarker) iter.next();
151
152             ConcreteMarker marker = getMarker(next);
153             if (marker != null) {
154                 result.add(marker);
155             }
156         }
157
158         return new MarkerList(result);
159     }
160
161     public static ConcreteMarker[] createMarkers(IMarker[] source)
162             throws CoreException {
163         ConcreteMarker[] result = new ConcreteMarker[source.length];
164
165         for (int idx = 0; idx < source.length; idx++) {
166             result[idx] = createMarker(source[idx]);
167         }
168
169         return result;
170     }
171
172     /**
173      * Computes the set of markers that match the given filter
174      *
175      * @param filters
176      * the filters to apply
177      * @param mon
178      * the monitor to update
179      * @param ignoreExceptions
180      * whether or not exception will be shown
181      * @return MarkerList
182      * @throws CoreException
183      */

184     public static MarkerList compute(MarkerFilter[] filters,
185             IProgressMonitor mon, boolean ignoreExceptions)
186             throws CoreException {
187
188         Collection JavaDoc returnMarkers = new HashSet JavaDoc();// avoid duplicates
189

190         for (int i = 0; i < filters.length; i++) {
191             returnMarkers.addAll(filters[i].findMarkers(mon, ignoreExceptions));
192         }
193         return new MarkerList(returnMarkers);
194     }
195
196     /**
197      * Returns a new MarkerList containing all markers in the workspace of the
198      * specified types
199      *
200      * @param types
201      * @return IMarker[]
202      * @throws CoreException
203      */

204     public static IMarker[] compute(String JavaDoc[] types) throws CoreException {
205
206         ArrayList JavaDoc result = new ArrayList JavaDoc();
207         IResource input = ResourcesPlugin.getWorkspace().getRoot();
208
209         for (int i = 0; i < types.length; i++) {
210             IMarker[] newMarkers = input.findMarkers(types[i], true,
211                     IResource.DEPTH_INFINITE);
212             result.addAll(Arrays.asList(newMarkers));
213         }
214
215         return (IMarker[]) result.toArray(new IMarker[result.size()]);
216     }
217
218     /**
219      * Returns the markers in the list. Read-only.
220      *
221      * @return an array of markers in the list
222      */

223     public ConcreteMarker[] toArray() {
224         return markers;
225     }
226
227     /**
228      * Returns the markers in this list. Read-only.
229      *
230      * @return the markers in the list
231      */

232     // public Collection getMarkers() {
233
// return markers;
234
// }
235
/**
236      * Returns the number of items in the list
237      *
238      * @return the number of items
239      */

240     public int getItemCount() {
241         return markers.length;
242     }
243
244     /**
245      * Returns the number of error markers in the list
246      *
247      * @return the number of errors
248      */

249     public int getErrors() {
250         return getMarkerCounts()[IMarker.SEVERITY_ERROR];
251     }
252
253     /**
254      * Returns the number of info markers in the list
255      *
256      * @return the number of info markers
257      */

258     public int getInfos() {
259         return getMarkerCounts()[IMarker.SEVERITY_INFO];
260     }
261
262     /**
263      * Returns the number of warning markers in the list
264      *
265      * @return the number of warning markers
266      */

267     public int getWarnings() {
268         return getMarkerCounts()[IMarker.SEVERITY_WARNING];
269     }
270
271     /**
272      * Returns an array of marker counts where getMarkerCounts()[severity] is
273      * the number of markers in the list with the given severity.
274      *
275      * @return an array of marker counts
276      */

277     private int[] getMarkerCounts() {
278         if (markerCounts == null) {
279             markerCounts = new int[] { 0, 0, 0 };
280
281             for (int idx = 0; idx < markers.length; idx++) {
282                 ConcreteMarker marker = markers[idx];
283
284                 if (marker instanceof ProblemMarker) {
285                     int severity = ((ProblemMarker) markers[idx]).getSeverity();
286                     if (severity >= 0 && severity <= 2) {
287                         markerCounts[severity]++;
288                     }
289                 }
290
291             }
292         }
293         return markerCounts;
294     }
295
296     /**
297      * Get the array that is the internal representation of the marker list
298      * without making a copy.
299      *
300      * @return Object[]
301      */

302     public Object JavaDoc[] getArray() {
303         return markers;
304     }
305
306     /**
307      * Get the size of the receiver.
308      *
309      * @return int
310      */

311     public int getSize() {
312         return getArray().length;
313     }
314
315     /**
316      * Return the markers at index
317      *
318      * @param index
319      * @return ConcreteMarker
320      */

321     public ConcreteMarker getMarker(int index) {
322         return markers[index];
323     }
324
325     /**
326      * Add the addedMarkers to the receiver.
327      * @param addedMarkers Collection of ConcreteMarker
328      * @param removedMarkers Collection of ConcreteMarker
329      */

330     public void updateMarkers(Collection JavaDoc addedMarkers,Collection JavaDoc removedMarkers) {
331         List JavaDoc list = new ArrayList JavaDoc(asList());
332         list.addAll(addedMarkers);
333         list.removeAll(removedMarkers);
334         markers = new ConcreteMarker[list.size()];
335         list.toArray(markers);
336     }
337     
338     /**
339      * Refresh all of the markers in the receiver.
340      */

341     public void refreshAll() {
342         for (int i = 0; i < markers.length; i++) {
343             markers[i].refresh();
344         }
345     }
346
347     /**
348      * Clear all of the group settings in the receiver.
349      */

350     public void clearGroups() {
351         for (int i = 0; i < markers.length; i++) {
352             markers[i].setGroup(null);
353         }
354         
355     }
356 }
357
Popular Tags