2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: XMLAnnotationHover.java,v 1.2 2006-10-21 23:14:13 pombredanne Exp $
13 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.source.Annotation;
23 import org.eclipse.jface.text.source.IAnnotationHover;
24 import org.eclipse.jface.text.source.IAnnotationModel;
25 import org.eclipse.jface.text.source.ISourceViewer;
28 * Implements simple annotation hover to show the associated messages.
30 public class XMLAnnotationHover implements IAnnotationHover {
32 * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer,
35 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
36 List annotations = getAnnotationsForLine(sourceViewer, lineNumber);
37 if (annotations != null) {
38 List messages = new ArrayList();
40 Iterator e = annotations.iterator();
42 Annotation annotation = (Annotation) e.next();
44 String message = annotation.getText();
45 if (message != null) {
46 message = message.trim();
47 if (message.length() > 0) {
48 messages.add(message);
53 if (messages.size() == 1) {
54 return (String) messages.get(0);
57 if (messages.size() > 1) {
58 return formatMessages(messages);
66 * Formats multiple annotation messages for display.
68 private String formatMessages(List messages) {
69 StringBuffer buffer = new StringBuffer();
71 Iterator e = messages.iterator();
73 buffer.append("- "); //$NON-NLS-1$
74 buffer.append(e.next());
78 return buffer.toString();
82 * Returns annotations for the ruler's line of activity.
84 private List getAnnotationsForLine(ISourceViewer viewer, int line) {
85 IDocument document = viewer.getDocument();
87 IAnnotationModel model = viewer.getAnnotationModel();
92 List retVal = new ArrayList();
94 Iterator e = new XMLAnnotationIterator(model, true);
96 Annotation a = (Annotation) e.next();
98 Position position = model.getPosition(a);
99 if (position != null) {
101 int annotationLine = document.getLineOfOffset(position
103 if (annotationLine == line) {
106 } catch (BadLocationException e1) {