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.PHPTextHover;
21 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.IRegion;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.Position;
28 import org.eclipse.jface.text.source.Annotation;
29 import org.eclipse.jface.text.source.IAnnotationModel;
30 import org.eclipse.ui.IEditorInput;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IFileEditorInput;
33 import org.eclipse.ui.editors.text.EditorsUI;
34 import org.eclipse.ui.texteditor.AnnotationPreference;
35 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
36 import org.eclipse.ui.texteditor.IDocumentProvider;
39 public class AnnotationHover extends AbstractJavaEditorTextHover {
41 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
42 private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
43 private PHPTextHover fPHPTextHover = null;
45 * Formats a message as HTML text.
47 private String formatMessage(String message) {
48 StringBuffer buffer= new StringBuffer();
49 HTMLPrinter.addPageProlog(buffer);
50 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
51 HTMLPrinter.addPageEpilog(buffer);
52 return buffer.toString();
56 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
58 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
60 if (getEditor() == null)
63 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
64 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
67 Iterator e= new JavaAnnotationIterator(model, true);
71 Annotation a= (Annotation) e.next();
73 AnnotationPreference preference= getAnnotationPreference(a);
74 if (preference == null || !(fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
77 Position p= model.getPosition(a);
79 int l= fAnnotationAccess.getLayer(a);
81 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
82 String msg= a.getText();
83 if (msg != null && msg.trim().length() > 0) {
90 return formatMessage(message);
92 // Added as long as the above doesn't work
93 if (fPHPTextHover!=null) {
94 message = fPHPTextHover.getHoverInfo(textViewer, hoverRegion);
96 return formatMessage(message);
103 * @see IJavaEditorTextHover#setEditor(IEditorPart)
105 public void setEditor(IEditorPart editor) {
106 if (editor instanceof PHPUnitEditor) {
107 super.setEditor(editor);
108 if (editor != null) {
109 IEditorInput editorInput = editor.getEditorInput();
110 if (editorInput instanceof IFileEditorInput) {
112 IFile f = ((IFileEditorInput) editorInput).getFile();
113 fPHPTextHover = new PHPTextHover(f.getProject());
115 } catch (NullPointerException e) {
116 // this exception occurs, if getTextHover is called by
117 // preference pages !
121 fPHPTextHover = new PHPTextHover(null);
123 super.setEditor(null);
128 * Returns the annotation preference for the given annotation.
130 * @param annotation the annotation
131 * @return the annotation preference or <code>null</code> if none
133 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
135 if (annotation.isMarkedDeleted())
137 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
140 static boolean isJavaProblemHover(String id) {
141 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);