added old hover behaviour;
[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
39 public class AnnotationHover extends AbstractJavaEditorTextHover {
40
41         private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
42         private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
43         private PHPTextHover fPHPTextHover = null;
44         /*
45          * Formats a message as HTML text.
46          */
47         private String formatMessage(String message) {
48                 StringBuffer buffer= new StringBuffer();
49                 HTMLPrinter.addPageProlog(buffer);
50                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
51                 HTMLPrinter.addPageEpilog(buffer);
52                 return buffer.toString();
53         }
54         
55         /*
56          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
57          */
58         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
59                 
60                 if (getEditor() == null)
61                         return null;
62                 
63                 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
64                 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
65                 String message= null;
66                 if (model != null) {
67                         Iterator e= new JavaAnnotationIterator(model, true);
68                         int layer= -1;
69                         
70                         while (e.hasNext()) {
71                                 Annotation a= (Annotation) e.next();
72
73                                 AnnotationPreference preference= getAnnotationPreference(a);
74                                 if (preference == null || !(fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
75                                         continue;
76
77                                 Position p= model.getPosition(a);
78                                 
79                                 int l= fAnnotationAccess.getLayer(a);
80                                 
81                                 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
82                                         String msg= a.getText();
83                                         if (msg != null && msg.trim().length() > 0) {
84                                                 message= msg;
85                                                 layer= l;
86                                         }
87                                 }
88                         }
89                         if (layer > -1)
90                                 return formatMessage(message);
91                 }
92                 // Added as long as the above doesn't work
93                 if (fPHPTextHover!=null) {
94                   message = fPHPTextHover.getHoverInfo(textViewer, hoverRegion);
95                   if (message!=null) {
96                     return formatMessage(message);
97                   }
98                 }
99                 return null;
100         }
101         
102         /*
103          * @see IJavaEditorTextHover#setEditor(IEditorPart)
104          */
105         public void setEditor(IEditorPart editor) {
106                 if (editor instanceof PHPUnitEditor) {
107                         super.setEditor(editor);
108                         if (editor != null) {
109                                 IEditorInput editorInput = editor.getEditorInput();
110                                 if (editorInput instanceof IFileEditorInput) {
111                                         try {
112                                                 IFile f = ((IFileEditorInput) editorInput).getFile();
113                                                 fPHPTextHover = new PHPTextHover(f.getProject());
114                                             return;
115                                         } catch (NullPointerException e) {
116                                                 // this exception occurs, if getTextHover is called by
117                                                 // preference pages !
118                                         }
119                                 }
120                         }
121                         fPHPTextHover = new PHPTextHover(null);
122                 } else {
123                         super.setEditor(null);
124                 }
125         }
126
127         /**
128          * Returns the annotation preference for the given annotation.
129          *
130          * @param annotation the annotation
131          * @return the annotation preference or <code>null</code> if none
132          */     
133         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
134                 
135                 if (annotation.isMarkedDeleted())
136                         return null;
137                 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
138         }
139
140         static boolean isJavaProblemHover(String id) {
141                 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);
142         }
143 }