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