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: CssAnnotationHover.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.Position;
23 import org.eclipse.jface.text.source.Annotation;
24 import org.eclipse.jface.text.source.IAnnotationHover;
25 import org.eclipse.jface.text.source.IAnnotationModel;
26 import org.eclipse.jface.text.source.ISourceViewer;
30 * Implements simple annotation hover to show the associated messages.
32 public class CssAnnotationHover implements IAnnotationHover {
34 * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
36 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
37 List annotations = getAnnotationsForLine(sourceViewer, lineNumber);
38 if (annotations != null) {
39 List messages = new ArrayList();
41 Iterator e = annotations.iterator();
43 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 = model.getAnnotationIterator();
96 Annotation a = (Annotation) e.next();
97 Position position = model.getPosition(a);
98 if (position != null) {
100 int annotationLine = document
101 .getLineOfOffset(position.getOffset());
102 if (annotationLine == line) {
105 } catch (BadLocationException e1) {