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;
29 import org.eclipse.ui.externaltools.internal.ant.editor.derived.HTMLPrinter;
31 // TODO: delete this class ? we use PHPAnnotationHover instead !
33 * Determines all markers for the given line and collects, concatenates, and formates
36 public class JavaAnnotationHover implements IAnnotationHover {
39 * Returns the distance to the ruler line.
41 protected int compareRulerLine(Position position, IDocument document, int line) {
43 if (position.getOffset() > -1 && position.getLength() > -1) {
45 int javaAnnotationLine= document.getLineOfOffset(position.getOffset());
46 if (line == javaAnnotationLine)
48 if (javaAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
50 } catch (BadLocationException x) {
58 * Selects a set of markers from the two lists. By default, it just returns
59 * the set of exact matches.
61 protected List select(List exactMatch, List including) {
66 * Returns one marker which includes the ruler's line of activity.
68 protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
70 IDocument document= viewer.getDocument();
71 IAnnotationModel model= viewer.getAnnotationModel();
76 List exact= new ArrayList();
77 List including= new ArrayList();
79 Iterator e= model.getAnnotationIterator();
80 HashMap messagesAtPosition= new HashMap();
83 if (o instanceof IJavaAnnotation) {
84 IJavaAnnotation a= (IJavaAnnotation)o;
85 if (!a.hasOverlay()) {
86 Position position= model.getPosition((Annotation)a);
90 if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
93 switch (compareRulerLine(position, document, line)) {
105 return select(exact, including);
108 private boolean isDuplicateJavaAnnotation(Map messagesAtPosition, Position position, String message) {
109 if (messagesAtPosition.containsKey(position)) {
110 Object value= messagesAtPosition.get(position);
111 if (message.equals(value))
114 if (value instanceof List) {
115 List messages= (List)value;
116 if (messages.contains(message))
119 messages.add(message);
121 ArrayList messages= new ArrayList();
123 messages.add(message);
124 messagesAtPosition.put(position, messages);
127 messagesAtPosition.put(position, message);
132 * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
134 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
135 List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
136 if (javaAnnotations != null) {
138 if (javaAnnotations.size() == 1) {
141 IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
142 String message= javaAnnotation.getMessage();
143 if (message != null && message.trim().length() > 0)
144 return formatSingleMessage(message);
148 List messages= new ArrayList();
150 Iterator e= javaAnnotations.iterator();
151 while (e.hasNext()) {
152 IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
153 String message= javaAnnotation.getMessage();
154 if (message != null && message.trim().length() > 0)
155 messages.add(message.trim());
158 if (messages.size() == 1)
159 return formatSingleMessage((String) messages.get(0));
161 if (messages.size() > 1)
162 return formatMultipleMessages(messages);
170 * Formats a message as HTML text.
172 private String formatSingleMessage(String message) {
173 StringBuffer buffer= new StringBuffer();
174 HTMLPrinter.addPageProlog(buffer);
175 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
176 HTMLPrinter.addPageEpilog(buffer);
177 return buffer.toString();
181 * Formats several message as HTML text.
183 private String formatMultipleMessages(List messages) {
184 StringBuffer buffer= new StringBuffer();
185 HTMLPrinter.addPageProlog(buffer);
186 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(PHPUIMessages.getString("JavaAnnotationHover.multipleMarkersAtThisLine"))); //$NON-NLS-1$
188 HTMLPrinter.startBulletList(buffer);
189 Iterator e= messages.iterator();
191 HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
192 HTMLPrinter.endBulletList(buffer);
194 HTMLPrinter.addPageEpilog(buffer);
195 return buffer.toString();