Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Christoph Kulla 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 Contributors: Christoph Kulla - Initial API and implementation /
 
 package org.eclipse.xtext.ui.editor.hover;
 
 import java.util.List;
 
 import  org.eclipse.jface.text.BadLocationException;
 import  org.eclipse.jface.text.IRegion;
 import  org.eclipse.jface.text.ITextViewer;
 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.ILineDiffInfo;
 import  org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
 
Base class for all hovers showing annotations and problems.

Author(s):
Christoph Kulla - Initial contribution and API
Holger Schill
 
 public abstract class AbstractProblemHover extends AbstractHover {
 
 	private DefaultMarkerAnnotationAccess markerAnnotationAccess;
 
 	public IRegion getHoverRegion(final ITextViewer textViewerfinal int offset) {
 		int lineNumber;
 		try {
 			lineNumber = textViewer.getDocument().getLineOfOffset(offset);
 			return getHoverRegionInternal(lineNumberoffset);
 		} catch (BadLocationException e) {
 		}
 		return null;
 	}
 
 	protected abstract IRegion getHoverRegionInternal(int lineNumberint offset);
 
 	public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
 		int lineNumber;
 		try {
 			lineNumber = getLineNumber(textViewerhoverRegion);
 			return getHoverInfoInternal(textViewerlineNumberhoverRegion.getOffset());
 		} catch (final BadLocationException e) {
 			return null;
 		}
 	}
 
 	protected abstract Object getHoverInfoInternal(ITextViewer textViewerint lineNumberint offset);
 
 	protected IAnnotationModel getAnnotationModel() {
 		return .getAnnotationModel();
 	}
 
 	public List<Annotation> getAnnotations(final int lineNumberfinal int offset) {
 		if (getAnnotationModel() == null) {
 			return Collections.emptyList();
 		}
 		final Iterator<?> iterator = getAnnotationModel().getAnnotationIterator();
 		List<Annotation> result = Lists.newArrayList();
 		while (iterator.hasNext()) {
 			final Annotation annotation = (Annotation) iterator.next();
 			if (isHandled(annotation)) {
 				Position position = getAnnotationModel().getPosition(annotation);
 				if (position != null) {
 					final int start = position.getOffset();
 					final int end = start + position.getLength();
 
 					if (offset > 0 && !(start <= offset && offset <= end)) {
 						continue;
 					}
 					try {
 						if (lineNumber != getDocument().getLineOfOffset(start)) {
 							continue;
 						}
 					} catch (final Exception x) {
 						continue;
 					}
 					if (!isLineDiffInfo(annotation)) {
 						result.add(annotation);
 					}
 				}
 			}
 		}
		return result;
	}
	protected boolean isLineDiffInfo(Annotation annotation) {
		return annotation instanceof ILineDiffInfo;
	}

Since:
2.1
	protected boolean isHandled(Annotation annotation) {
		return null != annotation
				&& !annotation.isMarkedDeleted()
				&& (.isSubtype(annotation.getType(), "org.eclipse.ui.workbench.texteditor.error"
						|| .isSubtype(annotation.getType(), "org.eclipse.ui.workbench.texteditor.warning")
						|| .isSubtype(annotation.getType(), "org.eclipse.ui.workbench.texteditor.info")
						|| .isSubtype(annotation.getType(), "org.eclipse.ui.workbench.texteditor.bookmark")
						|| .isSubtype(annotation.getType(), "org.eclipse.ui.workbench.texteditor.spelling"));
	}
New to GrepCode? Check out our FAQ X