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;
22 import net.sourceforge.phpeclipse.ui.WebUI;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.text.Position;
29 import org.eclipse.jface.text.source.Annotation;
30 import org.eclipse.jface.text.source.IAnnotationModel;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.editors.text.EditorsUI;
35 import org.eclipse.ui.texteditor.AnnotationPreference;
36 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
37 import org.eclipse.ui.texteditor.IDocumentProvider;
39 public class AnnotationHover extends AbstractJavaEditorTextHover {
41 // private IPreferenceStore fStore =
42 // PHPeclipsePlugin.getDefault().getPreferenceStore();
43 private IPreferenceStore fStore = EditorsUI.getPreferenceStore();
45 private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
47 private PHPTextHover fPHPTextHover = null;
50 * Formats a message as HTML text.
52 private String formatMessage(String message) {
53 StringBuffer buffer = new StringBuffer();
54 HTMLPrinter.addPageProlog(buffer);
55 HTMLPrinter.addParagraph(buffer, message); // HTMLPrinter.convertToHTMLContent(message));
56 HTMLPrinter.addPageEpilog(buffer);
57 return buffer.toString();
61 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
63 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
65 if (getEditor() == null)
68 IDocumentProvider provider = WebUI.getDefault()
69 .getCompilationUnitDocumentProvider();
70 IAnnotationModel model = provider.getAnnotationModel(getEditor()
72 String message = null;
74 Iterator e = new JavaAnnotationIterator(model, true);
78 Annotation a = (Annotation) e.next();
80 AnnotationPreference preference = getAnnotationPreference(a);
81 if (preference == null
82 || !(fStore.getBoolean(preference
83 .getTextPreferenceKey()) || (preference
84 .getHighlightPreferenceKey() != null && fStore
85 .getBoolean(preference
86 .getHighlightPreferenceKey()))))
89 Position p = model.getPosition(a);
91 int l = fAnnotationAccess.getLayer(a);
95 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
97 String msg = a.getText();
98 if (msg != null && msg.trim().length() > 0) {
105 return formatMessage(message);
107 // Added as long as the above doesn't work
108 if (fPHPTextHover != null) {
109 message = fPHPTextHover.getHoverInfo(textViewer, hoverRegion);
110 if (message != null) {
111 return formatMessage(message);
118 * @see IJavaEditorTextHover#setEditor(IEditorPart)
120 public void setEditor(IEditorPart editor) {
121 if (editor instanceof PHPUnitEditor) {
122 super.setEditor(editor);
123 if (editor != null) {
124 IEditorInput editorInput = editor.getEditorInput();
125 if (editorInput instanceof IFileEditorInput) {
127 IFile f = ((IFileEditorInput) editorInput).getFile();
128 fPHPTextHover = new PHPTextHover(f.getProject());
130 } catch (NullPointerException e) {
131 // this exception occurs, if getTextHover is called by
132 // preference pages !
136 fPHPTextHover = new PHPTextHover(null);
138 super.setEditor(null);
143 * Returns the annotation preference for the given annotation.
147 * @return the annotation preference or <code>null</code> if none
149 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
151 if (annotation.isMarkedDeleted())
153 return EditorsUI.getAnnotationPreferenceLookup()
154 .getAnnotationPreference(annotation);
157 // static boolean isJavaProblemHover(String id) {
158 // return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);