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 = PHPeclipsePlugin.getDefault()
41 .getPreferenceStore();
43 private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
45 private PHPTextHover fPHPTextHover = null;
48 * Formats a message as HTML text.
50 private String formatMessage(String message) {
51 StringBuffer buffer = new StringBuffer();
52 HTMLPrinter.addPageProlog(buffer);
53 HTMLPrinter.addParagraph(buffer, message); // HTMLPrinter.convertToHTMLContent(message));
54 HTMLPrinter.addPageEpilog(buffer);
55 return buffer.toString();
59 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
61 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
63 if (getEditor() == null)
66 IDocumentProvider provider = PHPeclipsePlugin.getDefault()
67 .getCompilationUnitDocumentProvider();
68 IAnnotationModel model = provider.getAnnotationModel(getEditor()
70 String message = null;
72 Iterator e = new JavaAnnotationIterator(model, true);
76 Annotation a = (Annotation) e.next();
78 AnnotationPreference preference = getAnnotationPreference(a);
79 if (preference == null
80 || !(fStore.getBoolean(preference
81 .getTextPreferenceKey()) || (preference
82 .getHighlightPreferenceKey() != null && fStore
83 .getBoolean(preference
84 .getHighlightPreferenceKey()))))
87 Position p = model.getPosition(a);
89 int l = fAnnotationAccess.getLayer(a);
93 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
95 String msg = a.getText();
96 if (msg != null && msg.trim().length() > 0) {
103 return formatMessage(message);
105 // Added as long as the above doesn't work
106 if (fPHPTextHover != null) {
107 message = fPHPTextHover.getHoverInfo(textViewer, hoverRegion);
108 if (message != null) {
109 return formatMessage(message);
116 * @see IJavaEditorTextHover#setEditor(IEditorPart)
118 public void setEditor(IEditorPart editor) {
119 if (editor instanceof PHPUnitEditor) {
120 super.setEditor(editor);
121 if (editor != null) {
122 IEditorInput editorInput = editor.getEditorInput();
123 if (editorInput instanceof IFileEditorInput) {
125 IFile f = ((IFileEditorInput) editorInput).getFile();
126 fPHPTextHover = new PHPTextHover(f.getProject());
128 } catch (NullPointerException e) {
129 // this exception occurs, if getTextHover is called by
130 // preference pages !
134 fPHPTextHover = new PHPTextHover(null);
136 super.setEditor(null);
141 * Returns the annotation preference for the given annotation.
145 * @return the annotation preference or <code>null</code> if none
147 private AnnotationPreference getAnnotationPreference(Annotation annotation) {
149 if (annotation.isMarkedDeleted())
151 return EditorsUI.getAnnotationPreferenceLookup()
152 .getAnnotationPreference(annotation);
155 static boolean isJavaProblemHover(String id) {
156 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);