1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
14 import java.util.Iterator;
16 import net.sourceforge.phpdt.internal.ui.text.HTMLPrinter;
17 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.JavaAnnotationIterator;
19 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
20 import net.sourceforge.phpeclipse.ui.WebUI;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.Position;
26 import org.eclipse.jface.text.source.Annotation;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.editors.text.EditorsUI;
30 import org.eclipse.ui.texteditor.AnnotationPreference;
31 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
32 import org.eclipse.ui.texteditor.IDocumentProvider;
35 * Abstract super class for annotation hovers.
39 public abstract class AbstractAnnotationHover extends
40 AbstractJavaEditorTextHover {
42 private IPreferenceStore fStore = WebUI.getDefault()
43 .getCombinedPreferenceStore();
45 private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
47 private boolean fAllAnnotations;
49 public AbstractAnnotationHover(boolean allAnnotations) {
50 fAllAnnotations = allAnnotations;
54 * Formats a message as HTML text.
56 private String formatMessage(String message) {
57 StringBuffer buffer = new StringBuffer();
58 HTMLPrinter.addPageProlog(buffer);
59 HTMLPrinter.addParagraph(buffer, HTMLPrinter
60 .convertToHTMLContent(message));
61 HTMLPrinter.addPageEpilog(buffer);
62 return buffer.toString();
66 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
68 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
70 if (getEditor() == null)
73 IDocumentProvider provider = WebUI.getDefault()
74 .getCompilationUnitDocumentProvider();
75 IAnnotationModel model = provider.getAnnotationModel(getEditor()
79 Iterator e = new JavaAnnotationIterator(model, true,
82 String message = null;
84 Annotation a = (Annotation) e.next();
86 AnnotationPreference preference = getAnnotationPreference(a);
87 if (preference == null
88 || !(preference.getTextPreferenceKey() != null
89 && fStore.getBoolean(preference
90 .getTextPreferenceKey()) || (preference
91 .getHighlightPreferenceKey() != null && fStore
92 .getBoolean(preference
93 .getHighlightPreferenceKey()))))
96 Position p = model.getPosition(a);
98 int l = fAnnotationAccess.getLayer(a);
102 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
104 String msg = a.getText();
105 if (msg != null && msg.trim().length() > 0) {
112 return formatMessage(message);
119 * @see IJavaEditorTextHover#setEditor(IEditorPart)
121 public void setEditor(IEditorPart editor) {
122 if (editor instanceof PHPUnitEditor)
123 super.setEditor(editor);
125 super.setEditor(null);
129 * Returns the annotation preference for the given annotation.
133 * @return the annotation preference or <code>null</code> if none
135 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
137 if (annotation.isMarkedDeleted())
139 return EditorsUI.getAnnotationPreferenceLookup()
140 .getAnnotationPreference(annotation);