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.corext.Assert;
20 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.Position;
27 import org.eclipse.jface.text.source.Annotation;
28 import org.eclipse.jface.text.source.IAnnotationHover;
29 import org.eclipse.jface.text.source.IAnnotationModel;
30 import org.eclipse.jface.text.source.ISourceViewer;
31 import org.eclipse.ui.editors.text.EditorsUI;
32 import org.eclipse.ui.texteditor.AnnotationPreference;
34 // TODO: delete this class ? we use PHPAnnotationHover instead !
36 * Determines all markers for the given line and collects, concatenates, and formates
39 public class JavaAnnotationHover implements IAnnotationHover {
40 private static class JavaAnnotationHoverType {
43 public static final JavaAnnotationHoverType OVERVIEW_RULER_HOVER= new JavaAnnotationHoverType();
44 public static final JavaAnnotationHoverType TEXT_RULER_HOVER= new JavaAnnotationHoverType();
45 public static final JavaAnnotationHoverType VERTICAL_RULER_HOVER= new JavaAnnotationHoverType();
47 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
49 private JavaAnnotationHoverType fType;
51 public JavaAnnotationHover(JavaAnnotationHoverType type) {
52 Assert.isTrue(OVERVIEW_RULER_HOVER.equals(type) || TEXT_RULER_HOVER.equals(type) || VERTICAL_RULER_HOVER.equals(type));
57 * Returns the distance to the ruler line.
59 protected int compareRulerLine(Position position, IDocument document, int line) {
61 if (position.getOffset() > -1 && position.getLength() > -1) {
63 int javaAnnotationLine= document.getLineOfOffset(position.getOffset());
64 if (line == javaAnnotationLine)
66 if (javaAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
68 } catch (BadLocationException x) {
76 * Selects a set of markers from the two lists. By default, it just returns
77 * the set of exact matches.
79 protected List select(List exactMatch, List including) {
83 * Returns one marker which includes the ruler's line of activity.
85 protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
87 IDocument document= viewer.getDocument();
88 IAnnotationModel model= viewer.getAnnotationModel();
93 List exact= new ArrayList();
94 List including= new ArrayList();
96 Iterator e= model.getAnnotationIterator();
97 HashMap messagesAtPosition= new HashMap();
99 Annotation annotation= (Annotation) e.next();
101 if (annotation.getText() == null)
104 Position position= model.getPosition(annotation);
105 if (position == null)
108 AnnotationPreference preference= getAnnotationPreference(annotation);
109 if (preference == null)
112 if (OVERVIEW_RULER_HOVER.equals(fType)) {
113 String key= preference.getOverviewRulerPreferenceKey();
114 if (key == null || !fStore.getBoolean(key))
116 } else if (TEXT_RULER_HOVER.equals(fType)) {
117 String key= preference.getTextPreferenceKey();
119 if (!fStore.getBoolean(key))
122 key= preference.getHighlightPreferenceKey();
123 if (key == null || !fStore.getBoolean(key))
126 } else if (VERTICAL_RULER_HOVER.equals(fType)) {
127 String key= preference.getVerticalRulerPreferenceKey();
128 // backward compatibility
129 if (key != null && !fStore.getBoolean(key))
133 if (isDuplicateJavaAnnotation(messagesAtPosition, position, annotation.getText()))
136 switch (compareRulerLine(position, document, line)) {
138 exact.add(annotation);
141 including.add(annotation);
146 return select(exact, including);
149 // * Returns one marker which includes the ruler's line of activity.
151 // protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
153 // IDocument document= viewer.getDocument();
154 // IAnnotationModel model= viewer.getAnnotationModel();
156 // if (model == null)
159 // List exact= new ArrayList();
160 // List including= new ArrayList();
162 // Iterator e= model.getAnnotationIterator();
163 // HashMap messagesAtPosition= new HashMap();
164 // while (e.hasNext()) {
165 // Object o= e.next();
166 // if (o instanceof IJavaAnnotation) {
167 // IJavaAnnotation a= (IJavaAnnotation)o;
168 // if (!a.hasOverlay()) {
169 // Position position= model.getPosition((Annotation)a);
170 // if (position == null)
173 // if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
176 // switch (compareRulerLine(position, document, line)) {
188 // return select(exact, including);
191 private boolean isDuplicateJavaAnnotation(Map messagesAtPosition, Position position, String message) {
192 if (messagesAtPosition.containsKey(position)) {
193 Object value= messagesAtPosition.get(position);
194 if (message.equals(value))
197 if (value instanceof List) {
198 List messages= (List)value;
199 if (messages.contains(message))
202 messages.add(message);
204 ArrayList messages= new ArrayList();
206 messages.add(message);
207 messagesAtPosition.put(position, messages);
210 messagesAtPosition.put(position, message);
215 * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
217 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
218 List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
219 if (javaAnnotations != null) {
221 if (javaAnnotations.size() == 1) {
224 Annotation annotation= (Annotation) javaAnnotations.get(0);
225 String message= annotation.getText();
226 if (message != null && message.trim().length() > 0)
227 return formatSingleMessage(message);
231 List messages= new ArrayList();
233 Iterator e= javaAnnotations.iterator();
234 while (e.hasNext()) {
235 Annotation annotation= (Annotation) e.next();
236 String message= annotation.getText();
237 if (message != null && message.trim().length() > 0)
238 messages.add(message.trim());
241 if (messages.size() == 1)
242 return formatSingleMessage((String) messages.get(0));
244 if (messages.size() > 1)
245 return formatMultipleMessages(messages);
252 * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
254 // public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
255 // List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
256 // if (javaAnnotations != null) {
258 // if (javaAnnotations.size() == 1) {
261 // IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
262 // String message= javaAnnotation.getMessage();
263 // if (message != null && message.trim().length() > 0)
264 // return formatSingleMessage(message);
268 // List messages= new ArrayList();
270 // Iterator e= javaAnnotations.iterator();
271 // while (e.hasNext()) {
272 // IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
273 // String message= javaAnnotation.getMessage();
274 // if (message != null && message.trim().length() > 0)
275 // messages.add(message.trim());
278 // if (messages.size() == 1)
279 // return formatSingleMessage((String) messages.get(0));
281 // if (messages.size() > 1)
282 // return formatMultipleMessages(messages);
290 * Formats a message as HTML text.
292 private String formatSingleMessage(String message) {
293 StringBuffer buffer= new StringBuffer();
294 HTMLPrinter.addPageProlog(buffer);
295 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
296 HTMLPrinter.addPageEpilog(buffer);
297 return buffer.toString();
301 * Formats several message as HTML text.
303 private String formatMultipleMessages(List messages) {
304 StringBuffer buffer= new StringBuffer();
305 HTMLPrinter.addPageProlog(buffer);
306 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(PHPUIMessages.getString("JavaAnnotationHover.multipleMarkersAtThisLine"))); //$NON-NLS-1$
308 HTMLPrinter.startBulletList(buffer);
309 Iterator e= messages.iterator();
311 HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
312 HTMLPrinter.endBulletList(buffer);
314 HTMLPrinter.addPageEpilog(buffer);
315 return buffer.toString();
319 * Returns the annotation preference for the given annotation.
321 * @param annotation the annotation
322 * @return the annotation preference or <code>null</code> if none
324 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
325 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);