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