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;
35 * Abstract super class for annotation hovers.
39 public abstract class AbstractAnnotationHover extends AbstractJavaEditorTextHover {
41 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
42 private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
43 private boolean fAllAnnotations;
46 public AbstractAnnotationHover(boolean allAnnotations) {
47 fAllAnnotations= allAnnotations;
51 * Formats a message as HTML text.
53 private String formatMessage(String message) {
54 StringBuffer buffer= new StringBuffer();
55 HTMLPrinter.addPageProlog(buffer);
56 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
57 HTMLPrinter.addPageEpilog(buffer);
58 return buffer.toString();
62 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
64 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
66 if (getEditor() == null)
69 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
70 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
73 Iterator e= new JavaAnnotationIterator(model, true, fAllAnnotations);
77 Annotation a= (Annotation) e.next();
79 AnnotationPreference preference= getAnnotationPreference(a);
80 if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
83 Position p= model.getPosition(a);
85 int l= fAnnotationAccess.getLayer(a);
87 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
88 String msg= a.getText();
89 if (msg != null && msg.trim().length() > 0) {
96 return formatMessage(message);
103 * @see IJavaEditorTextHover#setEditor(IEditorPart)
105 public void setEditor(IEditorPart editor) {
106 if (editor instanceof PHPUnitEditor)
107 super.setEditor(editor);
109 super.setEditor(null);
113 * Returns the annotation preference for the given annotation.
115 * @param annotation the annotation
116 * @return the annotation preference or <code>null</code> if none
118 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
120 if (annotation.isMarkedDeleted())
122 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);