1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 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 31 public class MarkerRegistry implements IResourceChangeListener, 32 ITableViewContentProvider { 33 34 private IFilter filter; 35 36 private IResource input; 37 38 private String [] types = new String [0]; 39 40 private ListenerList listeners = new ListenerList(); 41 42 public MarkerRegistry() { 43 } 44 45 49 public void dispose() { 50 listeners.clear(); 51 setInput(null); 52 } 53 54 57 public Object [] getElements() { 58 Object [] elements = getUnfilteredElements(); 59 if (filter != null) { 60 Object [] filteredItems = filter.filter(elements); 61 return filteredItems; 62 } 63 return elements; 64 } 65 66 69 public Object [] getUnfilteredElements() { 70 if (input == null) { 71 return new Object [0]; 72 } 73 List elements = new ArrayList (); 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 89 public IFilter getFilter() { 90 return filter; 91 } 92 93 96 public IResource getInput() { 97 return input; 98 } 99 100 105 public void setFilter(IFilter filter) { 106 if (this.filter == null || !this.filter.equals(filter)) { 107 this.filter = filter; 108 } 109 } 110 111 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 130 public String [] getTypes() { 131 return types; 132 } 133 134 140 public void setTypes(String [] types) { 141 if (types == null) 142 this.types = new String [0]; 143 else 144 this.types = types; 145 } 146 147 152 public void setType(String type) { 153 setTypes(new String [] { type }); 154 } 155 156 159 public void resourceChanged(IResourceChangeEvent event) { 160 161 final List additions = new ArrayList (); 165 final List removals = new ArrayList (); 166 final List changes = new ArrayList (); 167 168 IResourceDelta delta = event.getDelta(); 169 if (delta == null) 170 return; 171 getMarkerDeltas(delta, additions, removals, changes); 172 filterList(additions); 174 filterList(changes); 175 notifyListeners(additions, removals, changes); 176 } 177 178 183 private void getMarkerDeltas(IResourceDelta delta, List additions, 184 List removals, List 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 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 additions, List removals, List changes) { 231 Object [] 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 241 public void addItemsChangedListener(IItemsChangedListener listener) { 242 listeners.add(listener); 243 } 244 245 248 public void removeItemsChangedListener(IItemsChangedListener listener) { 249 listeners.remove(listener); 250 } 251 252 255 public int getItemCount() { 256 return getElements().length; 258 } 259 260 public int getRawItemCount() { 261 return getUnfilteredElements().length; 263 } 264 265 private void filterList(List 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 |