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;
38 public class AnnotationHover extends AbstractJavaEditorTextHover {
40 // private IPreferenceStore fStore =
41 // PHPeclipsePlugin.getDefault().getPreferenceStore();
42 private IPreferenceStore fStore = EditorsUI.getPreferenceStore();
44 private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
46 private PHPTextHover fPHPTextHover = null;
49 * Formats a message as HTML text.
51 private String formatMessage(String message) {
52 StringBuffer buffer = new StringBuffer();
53 HTMLPrinter.addPageProlog(buffer);
54 HTMLPrinter.addParagraph(buffer, message); // HTMLPrinter.convertToHTMLContent(message));
55 HTMLPrinter.addPageEpilog(buffer);
56 return buffer.toString();
60 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
62 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
64 if (getEditor() == null)
67 IDocumentProvider provider = PHPeclipsePlugin.getDefault()
68 .getCompilationUnitDocumentProvider();
69 IAnnotationModel model = provider.getAnnotationModel(getEditor()
71 String message = null;
73 Iterator e = new JavaAnnotationIterator(model, true);
77 Annotation a = (Annotation) e.next();
79 AnnotationPreference preference = getAnnotationPreference(a);
80 if (preference == null
81 || !(fStore.getBoolean(preference
82 .getTextPreferenceKey()) || (preference
83 .getHighlightPreferenceKey() != null && fStore
84 .getBoolean(preference
85 .getHighlightPreferenceKey()))))
88 Position p = model.getPosition(a);
90 int l = fAnnotationAccess.getLayer(a);
94 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
96 String msg = a.getText();
97 if (msg != null && msg.trim().length() > 0) {
104 return formatMessage(message);
106 // Added as long as the above doesn't work
107 if (fPHPTextHover != null) {
108 message = fPHPTextHover.getHoverInfo(textViewer, hoverRegion);
109 if (message != null) {
110 return formatMessage(message);
117 * @see IJavaEditorTextHover#setEditor(IEditorPart)
119 public void setEditor(IEditorPart editor) {
120 if (editor instanceof PHPUnitEditor) {
121 super.setEditor(editor);
122 if (editor != null) {
123 IEditorInput editorInput = editor.getEditorInput();
124 if (editorInput instanceof IFileEditorInput) {
126 IFile f = ((IFileEditorInput) editorInput).getFile();
127 fPHPTextHover = new PHPTextHover(f.getProject());
129 } catch (NullPointerException e) {
130 // this exception occurs, if getTextHover is called by
131 // preference pages !
135 fPHPTextHover = new PHPTextHover(null);
137 super.setEditor(null);
142 * Returns the annotation preference for the given annotation.
146 * @return the annotation preference or <code>null</code> if none
148 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
150 if (annotation.isMarkedDeleted())
152 return EditorsUI.getAnnotationPreferenceLookup()
153 .getAnnotationPreference(annotation);
156 static boolean isJavaProblemHover(String id) {
157 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);