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 *******************************************************************************/
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.phpdt.ui.PreferenceConstants;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.JavaAnnotationIterator;
20 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
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 public class AnnotationHover extends AbstractJavaEditorTextHover {
37 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
38 private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
41 * Formats a message as HTML text.
43 private String formatMessage(String message) {
44 StringBuffer buffer= new StringBuffer();
45 HTMLPrinter.addPageProlog(buffer);
46 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
47 HTMLPrinter.addPageEpilog(buffer);
48 return buffer.toString();
52 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
54 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
56 if (getEditor() == null)
59 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
60 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
63 Iterator e= new JavaAnnotationIterator(model, true);
67 Annotation a= (Annotation) e.next();
69 AnnotationPreference preference= getAnnotationPreference(a);
70 if (preference == null || !(fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
73 Position p= model.getPosition(a);
75 int l= fAnnotationAccess.getLayer(a);
77 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
78 String msg= a.getText();
79 if (msg != null && msg.trim().length() > 0) {
86 return formatMessage(message);
93 * @see IJavaEditorTextHover#setEditor(IEditorPart)
95 public void setEditor(IEditorPart editor) {
96 if (editor instanceof PHPUnitEditor)
97 super.setEditor(editor);
99 super.setEditor(null);
103 * Returns the annotation preference for the given annotation.
105 * @param annotation the annotation
106 * @return the annotation preference or <code>null</code> if none
108 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
110 if (annotation.isMarkedDeleted())
112 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
115 static boolean isJavaProblemHover(String id) {
116 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);