KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > AnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.Position;
20 import org.eclipse.jface.text.source.IAnnotationHover;
21 import org.eclipse.jface.text.source.IAnnotationModel;
22 import org.eclipse.jface.text.source.ISourceViewer;
23 import org.eclipse.ui.texteditor.MarkerAnnotation;
24
25 public class AnnotationHover implements IAnnotationHover {
26
27     public String JavaDoc getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
28         String JavaDoc[] messages = getMessagesForLine(sourceViewer, lineNumber);
29
30         if (messages.length == 0)
31             return null;
32
33         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc[] getMessagesForLine(ISourceViewer viewer, int line) {
43         IDocument document = viewer.getDocument();
44         IAnnotationModel model = viewer.getAnnotationModel();
45
46         if (model == null)
47             return new String JavaDoc[0];
48
49         ArrayList JavaDoc messages = new ArrayList JavaDoc();
50
51         Iterator JavaDoc iter = model.getAnnotationIterator();
52         while (iter.hasNext()) {
53             Object JavaDoc object = iter.next();
54             if (object instanceof MarkerAnnotation) {
55                 MarkerAnnotation annotation = (MarkerAnnotation) object;
56                 if (compareRulerLine(model.getPosition(annotation),
57                     document,
58                     line)) {
59                     IMarker marker = annotation.getMarker();
60                     String JavaDoc message =
61                         marker.getAttribute(IMarker.MESSAGE, (String JavaDoc) null);
62                     if (message != null && message.trim().length() > 0)
63                         messages.add(message);
64                 }
65             }
66         }
67         return (String JavaDoc[]) messages.toArray(new String JavaDoc[messages.size()]);
68     }
69
70     private boolean compareRulerLine(
71         Position position,
72         IDocument document,
73         int line) {
74
75         try {
76             if (position.getOffset() > -1 && position.getLength() > -1) {
77                 return document.getLineOfOffset(position.getOffset()) == line;
78             }
79         } catch (BadLocationException e) {
80         }
81         return false;
82     }
83 }
84
Popular Tags