package tool.markers;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
import tool.model.ToolMethod;
public class MethodMarkerAnnotationModel extends AbstractMarkerAnnotationModel {
private ToolMethod method;
private IWorkspace workspace;
private IResourceChangeListener changeListener= new ChangeListener();
class ChangeListener implements IResourceChangeListener {
public void resourceChanged(IResourceChangeEvent e) {
IResourceDelta delta= e.getDelta();
if (delta != null && method != null) {
IResourceDelta child= delta.findMember(method.getFile().getFullPath());
if (child != null)
update(child.getMarkerDeltas());
}
}
}
public MethodMarkerAnnotationModel(ToolMethod method) {
super();
Assert.isNotNull(method);
this.method = method;
this.workspace = method.getFile().getWorkspace();
}
@Override
protected void deleteMarkers(final IMarker[] markers) throws CoreException {
workspace.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
for (int i= 0; i < markers.length; ++i) {
markers[i].delete();
}
}
}, null, IWorkspace.AVOID_UPDATE, null);
}
@Override
protected boolean isAcceptable(IMarker marker) {
return marker != null && method.getFile().equals(marker.getResource());
}
@Override
protected void listenToMarkerChanges(boolean listen) {
if (listen)
workspace.addResourceChangeListener(changeListener);
else
workspace.removeResourceChangeListener(changeListener);
}
@Override
protected IMarker[] retrieveMarkers() throws CoreException {
return method.getFile().findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
}
protected void update(IMarkerDelta[] markerDeltas) {
if (markerDeltas.length == 0)
return;
for (int i= 0; i < markerDeltas.length; i++) {
IMarkerDelta delta= markerDeltas[i];
switch (delta.getKind()) {
case IResourceDelta.ADDED :
addMarkerAnnotation(delta.getMarker());
break;
case IResourceDelta.REMOVED :
removeMarkerAnnotation(delta.getMarker());
break;
case IResourceDelta.CHANGED :
modifyMarkerAnnotation(delta.getMarker());
break;
}
}
fireModelChanged();
}
}