Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009 Michael Clay and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.editor.validation;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.core.resources.IMarker;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.NullProgressMonitor;
 import  org.eclipse.jface.text.Position;
 import  org.eclipse.jface.text.source.Annotation;
 import  org.eclipse.jface.text.source.IAnnotationModel;
 import  org.eclipse.jface.text.source.IAnnotationModelExtension;
 import  org.eclipse.jface.text.source.IAnnotationModelListener;
 import  org.eclipse.ui.texteditor.AnnotationTypeLookup;
 import  org.eclipse.ui.texteditor.MarkerAnnotation;
 

Author(s):
Sven Efftinge - Initial contribution and API
Michael Clay
Heiko Behrens
Sebastian Zarnekow
 
 public class AnnotationIssueProcessor implements IValidationIssueProcessor, IAnnotationModelListener {
 	private static final Logger LOG = Logger.getLogger(AnnotationIssueProcessor.class);
 	private final IAnnotationModel annotationModel;
 	private AnnotationTypeLookup lookup = new AnnotationTypeLookup();
 	private final IXtextDocument xtextDocument;
 	private boolean updateMarkersOnModelChange;
 
 	public AnnotationIssueProcessor(IXtextDocument xtextDocument, IAnnotationModel annotationModel,
 			IssueResolutionProvider issueResolutionProvider) {
 		super();
 		this. = annotationModel;
 		if(annotationModel == null
 			throw new IllegalArgumentException("Annotation model cannot be null");
 		annotationModel.addAnnotationModelListener(this);
 		this. = xtextDocument;
 		this. = issueResolutionProvider;
 	}
 
 	public void processIssues(List<Issueissues, IProgressMonitor monitor) {
 		List<Annotation> toBeRemoved = getAnnotationsToRemove(monitor);
 		Multimap<Position, Annotation> positionToAnnotations = ArrayListMultimap.create();
 		Map<Annotation, Position> annotationToPosition = getAnnotationsToAdd(positionToAnnotationsissuesmonitor);
 		updateAnnotations(monitortoBeRemovedannotationToPosition);
 	}
 
 	protected void updateAnnotations(IProgressMonitor monitorList<Annotation> toBeRemoved,
 			Map<Annotation, Position> annotationToPosition) {
 		if (monitor.isCanceled()) {
 			return;
 		}
 		if ( instanceof IAnnotationModelExtension) {
 			Annotation[] removedAnnotations = toBeRemoved.toArray(new Annotation[toBeRemoved.size()]);
 			((IAnnotationModelExtension) ).replaceAnnotations(removedAnnotationsannotationToPosition);
 		} else {
 			for (Annotation annotation : toBeRemoved) {
 				if (monitor.isCanceled()) {
 					return;
 				}
 				.removeAnnotation(annotation);
 			}
 			for (Map.Entry<Annotation, Position> entry : annotationToPosition.entrySet()) {
 				if (monitor.isCanceled()) {
 					return;
 				}
 				.addAnnotation(entry.getKey(), entry.getValue());
			}
		}
	}
	protected List<Annotation> getAnnotationsToRemove(IProgressMonitor monitor) {
		if (monitor.isCanceled() ||  == null) {
			return Lists.newArrayList();
		}
		@SuppressWarnings("unchecked")
		Iterator<Annotation> annotationIterator = .getAnnotationIterator();
		List<Annotation> toBeRemoved = Lists.newArrayList();
		while (annotationIterator.hasNext()) {
			if (monitor.isCanceled()) {
				return toBeRemoved;
			}
			Annotation annotation = annotationIterator.next();
			String type = annotation.getType();
				if (!(annotation instanceof MarkerAnnotation)) {
					toBeRemoved.add(annotation);
				}
			}
		}
		return toBeRemoved;
	}
	protected Map<Annotation, Position> getAnnotationsToAdd(Multimap<Position, Annotation> positionToAnnotations,
			List<Issueissues, IProgressMonitor monitor) {
		if (monitor.isCanceled()) {
			return HashBiMap.create();
		}
		Map<Annotation, Position> annotationToPosition = Maps.newHashMapWithExpectedSize(issues.size());
		for (Issue issue : issues) {
			if (monitor.isCanceled()) {
				return annotationToPosition;
			}
			if (isSet(issue.getOffset()) && isSet(issue.getLength()) && issue.getMessage() != null) {
				String type = .getAnnotationType(.getMarkerSeverity(issue.getSeverity()));
				boolean isQuickfixable = false;
else {
				}
				Annotation annotation = new XtextAnnotation(typefalseissueisQuickfixable);
				if (issue.getOffset() < 0 || issue.getLength() < 0) {
					.error("Invalid annotation position offset=" + issue.getOffset() + " length = "
issue.getLength());
				}
				Position position = new Position(Math.max(0, issue.getOffset()), Math.max(0, issue.getLength()));
				annotationToPosition.put(annotationposition);
				positionToAnnotations.put(positionannotation);
			}
		}
		return annotationToPosition;
	}
	protected boolean isSet(Integer length) {
		return length != null && length != -1;
	}
	protected int getMarkerSeverity(Severity severity) {
		switch (severity) {
			case :
				return IMarker.SEVERITY_ERROR;
			case :
				return IMarker.SEVERITY_WARNING;
			case :
				return IMarker.SEVERITY_INFO;
			default:
		}
	}
	protected void updateMarkerAnnotations(IProgressMonitor monitor) {
		if (monitor.isCanceled()) {
			return;
		}
		Iterator<MarkerAnnotation> annotationIterator = Iterators.filter(.getAnnotationIterator(),
				MarkerAnnotation.class);
		// every markerAnnotation produced by fast validation can be marked as deleted.
		// If its predicate still holds, the validation annotation will be covered anyway.
		while (annotationIterator.hasNext() && !monitor.isCanceled()) {
			final MarkerAnnotation annotation = annotationIterator.next();
			if (!annotation.isMarkedDeleted())
				try {
					if (isRelevantAnnotationType(annotation.getType())) {
						boolean markAsDeleted = annotation.getMarker().isSubtypeOf(.);
						if (markAsDeleted) {
							annotation.markDeleted(true);
						}
					}
catch (CoreException e) {
					// marker type cannot be resolved - keep state of annotation
				}
		}
	}
	protected void queueOrFireAnnotationChangedEvent(final MarkerAnnotation annotation) {
		else
	}
	protected void fireQueuedEvents() {
	}
	protected void announceAnnotationChanged(Annotation annotation) {
		else {
			Position position = .getPosition(annotation);
			if ( instanceof IAnnotationModelExtension)
				((IAnnotationModelExtension) ).modifyAnnotationPosition(annotationposition);
			else {
				.removeAnnotation(annotation);
				.addAnnotation(annotationposition);
			}
		}
	}
	protected boolean isRelevantAnnotationType(String type) {
	}
	public void modelChanged(IAnnotationModel model) {
			updateMarkerAnnotations(new NullProgressMonitor());
		}
	}
New to GrepCode? Check out our FAQ X