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;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationModel;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.editors.text.EditorsUI;
29 import org.eclipse.ui.texteditor.AnnotationPreference;
30 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
31 import org.eclipse.ui.texteditor.IDocumentProvider;
34 * Abstract super class for annotation hovers.
38 public abstract class AbstractAnnotationHover extends
39 AbstractJavaEditorTextHover {
41 private IPreferenceStore fStore = PHPeclipsePlugin.getDefault()
42 .getCombinedPreferenceStore();
44 private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
46 private boolean fAllAnnotations;
48 public AbstractAnnotationHover(boolean allAnnotations) {
49 fAllAnnotations = allAnnotations;
53 * Formats a message as HTML text.
55 private String formatMessage(String message) {
56 StringBuffer buffer = new StringBuffer();
57 HTMLPrinter.addPageProlog(buffer);
58 HTMLPrinter.addParagraph(buffer, HTMLPrinter
59 .convertToHTMLContent(message));
60 HTMLPrinter.addPageEpilog(buffer);
61 return buffer.toString();
65 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
67 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
69 if (getEditor() == null)
72 IDocumentProvider provider = PHPeclipsePlugin.getDefault()
73 .getCompilationUnitDocumentProvider();
74 IAnnotationModel model = provider.getAnnotationModel(getEditor()
78 Iterator e = new JavaAnnotationIterator(model, true,
81 String message = null;
83 Annotation a = (Annotation) e.next();
85 AnnotationPreference preference = getAnnotationPreference(a);
86 if (preference == null
87 || !(preference.getTextPreferenceKey() != null
88 && fStore.getBoolean(preference
89 .getTextPreferenceKey()) || (preference
90 .getHighlightPreferenceKey() != null && fStore
91 .getBoolean(preference
92 .getHighlightPreferenceKey()))))
95 Position p = model.getPosition(a);
97 int l = fAnnotationAccess.getLayer(a);
101 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
103 String msg = a.getText();
104 if (msg != null && msg.trim().length() > 0) {
111 return formatMessage(message);
118 * @see IJavaEditorTextHover#setEditor(IEditorPart)
120 public void setEditor(IEditorPart editor) {
121 if (editor instanceof PHPUnitEditor)
122 super.setEditor(editor);
124 super.setEditor(null);
128 * Returns the annotation preference for the given annotation.
132 * @return the annotation preference or <code>null</code> if none
134 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
136 if (annotation.isMarkedDeleted())
138 return EditorsUI.getAnnotationPreferenceLookup()
139 .getAnnotationPreference(annotation);