55579c3b55998193f6a599cc3ca3611ebdc9ac0c
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.util.Iterator;
15
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
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;
37
38 public class AnnotationHover extends AbstractJavaEditorTextHover {
39
40         private IPreferenceStore fStore = PHPeclipsePlugin.getDefault()
41                         .getPreferenceStore();
42
43         private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
44
45         private PHPTextHover fPHPTextHover = null;
46
47         /*
48          * Formats a message as HTML text.
49          */
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();
56         }
57
58         /*
59          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
60          */
61         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
62
63                 if (getEditor() == null)
64                         return null;
65
66                 IDocumentProvider provider = PHPeclipsePlugin.getDefault()
67                                 .getCompilationUnitDocumentProvider();
68                 IAnnotationModel model = provider.getAnnotationModel(getEditor()
69                                 .getEditorInput());
70                 String message = null;
71                 if (model != null) {
72                         Iterator e = new JavaAnnotationIterator(model, true);
73                         int layer = -1;
74
75                         while (e.hasNext()) {
76                                 Annotation a = (Annotation) e.next();
77
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()))))
85                                         continue;
86
87                                 Position p = model.getPosition(a);
88
89                                 int l = fAnnotationAccess.getLayer(a);
90
91                                 if (l > layer
92                                                 && p != null
93                                                 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
94                                                                 .getLength())) {
95                                         String msg = a.getText();
96                                         if (msg != null && msg.trim().length() > 0) {
97                                                 message = msg;
98                                                 layer = l;
99                                         }
100                                 }
101                         }
102                         if (layer > -1)
103                                 return formatMessage(message);
104                 }
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);
110                         }
111                 }
112                 return null;
113         }
114
115         /*
116          * @see IJavaEditorTextHover#setEditor(IEditorPart)
117          */
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) {
124                                         try {
125                                                 IFile f = ((IFileEditorInput) editorInput).getFile();
126                                                 fPHPTextHover = new PHPTextHover(f.getProject());
127                                                 return;
128                                         } catch (NullPointerException e) {
129                                                 // this exception occurs, if getTextHover is called by
130                                                 // preference pages !
131                                         }
132                                 }
133                         }
134                         fPHPTextHover = new PHPTextHover(null);
135                 } else {
136                         super.setEditor(null);
137                 }
138         }
139
140         /**
141          * Returns the annotation preference for the given annotation.
142          * 
143          * @param annotation
144          *            the annotation
145          * @return the annotation preference or <code>null</code> if none
146          */
147         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
148
149                 if (annotation.isMarkedDeleted())
150                         return null;
151                 return EditorsUI.getAnnotationPreferenceLookup()
152                                 .getAnnotationPreference(annotation);
153         }
154
155         static boolean isJavaProblemHover(String id) {
156                 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);
157         }
158 }