1 11 package org.eclipse.ui.texteditor; 12 13 import org.eclipse.core.resources.IMarker; 14 import org.eclipse.core.resources.IMarkerDelta; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IResourceChangeEvent; 17 import org.eclipse.core.resources.IResourceChangeListener; 18 import org.eclipse.core.resources.IResourceDelta; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.resources.IWorkspaceRunnable; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 26 27 28 34 public class ResourceMarkerAnnotationModel extends AbstractMarkerAnnotationModel { 35 36 37 40 class ResourceChangeListener implements IResourceChangeListener { 41 44 public void resourceChanged(IResourceChangeEvent e) { 45 IResourceDelta delta= e.getDelta(); 46 if (delta != null && fResource != null) { 47 IResourceDelta child= delta.findMember(fResource.getFullPath()); 48 if (child != null) 49 update(child.getMarkerDeltas()); 50 } 51 } 52 } 53 54 55 private IWorkspace fWorkspace; 56 57 private IResource fResource; 58 59 private IResourceChangeListener fResourceChangeListener= new ResourceChangeListener(); 60 61 62 68 public ResourceMarkerAnnotationModel(IResource resource) { 69 Assert.isNotNull(resource); 70 fResource= resource; 71 fWorkspace= resource.getWorkspace(); 72 } 73 74 77 protected boolean isAcceptable(IMarker marker) { 78 return marker != null && fResource.equals(marker.getResource()); 79 } 80 81 86 protected void update(IMarkerDelta[] markerDeltas) { 87 88 if (markerDeltas.length == 0) 89 return; 90 91 for (int i= 0; i < markerDeltas.length; i++) { 92 IMarkerDelta delta= markerDeltas[i]; 93 switch (delta.getKind()) { 94 case IResourceDelta.ADDED : 95 addMarkerAnnotation(delta.getMarker()); 96 break; 97 case IResourceDelta.REMOVED : 98 removeMarkerAnnotation(delta.getMarker()); 99 break; 100 case IResourceDelta.CHANGED : 101 modifyMarkerAnnotation(delta.getMarker()); 102 break; 103 } 104 } 105 106 fireModelChanged(); 107 } 108 109 112 protected void listenToMarkerChanges(boolean listen) { 113 if (listen) 114 fWorkspace.addResourceChangeListener(fResourceChangeListener); 115 else 116 fWorkspace.removeResourceChangeListener(fResourceChangeListener); 117 } 118 119 122 protected void deleteMarkers(final IMarker[] markers) throws CoreException { 123 fWorkspace.run(new IWorkspaceRunnable() { 124 public void run(IProgressMonitor monitor) throws CoreException { 125 for (int i= 0; i < markers.length; ++i) { 126 markers[i].delete(); 127 } 128 } 129 }, null, IWorkspace.AVOID_UPDATE, null); 130 } 131 132 135 protected IMarker[] retrieveMarkers() throws CoreException { 136 return fResource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO); 137 } 138 139 145 protected IResource getResource() { 146 return fResource; 147 } 148 } 149 | Popular Tags |