1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation 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 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
19 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
20 import net.sourceforge.phpeclipse.phpeditor.IJavaAnnotation;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationHover;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.jface.text.source.ISourceViewer;
30 // TODO: delete this class ? we use PHPAnnotationHover instead !
32 * Determines all markers for the given line and collects, concatenates, and formates
35 public class JavaAnnotationHover implements IAnnotationHover {
38 * Returns the distance to the ruler line.
40 protected int compareRulerLine(Position position, IDocument document, int line) {
42 if (position.getOffset() > -1 && position.getLength() > -1) {
44 int javaAnnotationLine= document.getLineOfOffset(position.getOffset());
45 if (line == javaAnnotationLine)
47 if (javaAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
49 } catch (BadLocationException x) {
57 * Selects a set of markers from the two lists. By default, it just returns
58 * the set of exact matches.
60 protected List select(List exactMatch, List including) {
65 * Returns one marker which includes the ruler's line of activity.
67 protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
69 IDocument document= viewer.getDocument();
70 IAnnotationModel model= viewer.getAnnotationModel();
75 List exact= new ArrayList();
76 List including= new ArrayList();
78 Iterator e= model.getAnnotationIterator();
79 HashMap messagesAtPosition= new HashMap();
82 if (o instanceof IJavaAnnotation) {
83 IJavaAnnotation a= (IJavaAnnotation)o;
84 if (!a.hasOverlay()) {
85 Position position= model.getPosition((Annotation)a);
89 if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
92 switch (compareRulerLine(position, document, line)) {
104 return select(exact, including);
107 private boolean isDuplicateJavaAnnotation(Map messagesAtPosition, Position position, String message) {
108 if (messagesAtPosition.containsKey(position)) {
109 Object value= messagesAtPosition.get(position);
110 if (message.equals(value))
113 if (value instanceof List) {
114 List messages= (List)value;
115 if (messages.contains(message))
118 messages.add(message);
120 ArrayList messages= new ArrayList();
122 messages.add(message);
123 messagesAtPosition.put(position, messages);
126 messagesAtPosition.put(position, message);
131 * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
133 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
134 List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
135 if (javaAnnotations != null) {
137 if (javaAnnotations.size() == 1) {
140 IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
141 String message= javaAnnotation.getMessage();
142 if (message != null && message.trim().length() > 0)
143 return formatSingleMessage(message);
147 List messages= new ArrayList();
149 Iterator e= javaAnnotations.iterator();
150 while (e.hasNext()) {
151 IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
152 String message= javaAnnotation.getMessage();
153 if (message != null && message.trim().length() > 0)
154 messages.add(message.trim());
157 if (messages.size() == 1)
158 return formatSingleMessage((String) messages.get(0));
160 if (messages.size() > 1)
161 return formatMultipleMessages(messages);
169 * Formats a message as HTML text.
171 private String formatSingleMessage(String message) {
172 StringBuffer buffer= new StringBuffer();
173 HTMLPrinter.addPageProlog(buffer);
174 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
175 HTMLPrinter.addPageEpilog(buffer);
176 return buffer.toString();
180 * Formats several message as HTML text.
182 private String formatMultipleMessages(List messages) {
183 StringBuffer buffer= new StringBuffer();
184 HTMLPrinter.addPageProlog(buffer);
185 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(PHPUIMessages.getString("JavaAnnotationHover.multipleMarkersAtThisLine"))); //$NON-NLS-1$
187 HTMLPrinter.startBulletList(buffer);
188 Iterator e= messages.iterator();
190 HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
191 HTMLPrinter.endBulletList(buffer);
193 HTMLPrinter.addPageEpilog(buffer);
194 return buffer.toString();