01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14:
15: import org.eclipse.core.resources.IMarker;
16: import org.eclipse.jface.text.BadLocationException;
17: import org.eclipse.jface.text.IDocument;
18: import org.eclipse.jface.text.Position;
19: import org.eclipse.jface.text.source.IAnnotationHover;
20: import org.eclipse.jface.text.source.IAnnotationModel;
21: import org.eclipse.jface.text.source.ISourceViewer;
22: import org.eclipse.ui.texteditor.MarkerAnnotation;
23:
24: public class AnnotationHover implements IAnnotationHover {
25:
26: public String getHoverInfo(ISourceViewer sourceViewer,
27: int lineNumber) {
28: String[] messages = getMessagesForLine(sourceViewer, lineNumber);
29:
30: if (messages.length == 0)
31: return null;
32:
33: StringBuffer buffer = new StringBuffer();
34: for (int i = 0; i < messages.length; i++) {
35: buffer.append(messages[i]);
36: if (i < messages.length - 1)
37: buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
38: }
39: return buffer.toString();
40: }
41:
42: private String[] getMessagesForLine(ISourceViewer viewer, int line) {
43: IDocument document = viewer.getDocument();
44: IAnnotationModel model = viewer.getAnnotationModel();
45:
46: if (model == null)
47: return new String[0];
48:
49: ArrayList messages = new ArrayList();
50:
51: Iterator iter = model.getAnnotationIterator();
52: while (iter.hasNext()) {
53: Object object = iter.next();
54: if (object instanceof MarkerAnnotation) {
55: MarkerAnnotation annotation = (MarkerAnnotation) object;
56: if (compareRulerLine(model.getPosition(annotation),
57: document, line)) {
58: IMarker marker = annotation.getMarker();
59: String message = marker.getAttribute(
60: IMarker.MESSAGE, (String) null);
61: if (message != null && message.trim().length() > 0)
62: messages.add(message);
63: }
64: }
65: }
66: return (String[]) messages.toArray(new String[messages.size()]);
67: }
68:
69: private boolean compareRulerLine(Position position,
70: IDocument document, int line) {
71:
72: try {
73: if (position.getOffset() > -1 && position.getLength() > -1) {
74: return document.getLineOfOffset(position.getOffset()) == line;
75: }
76: } catch (BadLocationException e) {
77: }
78: return false;
79: }
80: }
|