Eclipse 3M7
[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.PHPUnitEditor;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.Position;
26 import org.eclipse.jface.text.source.Annotation;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.editors.text.EditorsUI;
30 import org.eclipse.ui.texteditor.AnnotationPreference;
31 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
32 import org.eclipse.ui.texteditor.IDocumentProvider;
33
34
35 public class AnnotationHover extends AbstractJavaEditorTextHover {
36
37         private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
38         private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
39         
40         /*
41          * Formats a message as HTML text.
42          */
43         private String formatMessage(String message) {
44                 StringBuffer buffer= new StringBuffer();
45                 HTMLPrinter.addPageProlog(buffer);
46                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
47                 HTMLPrinter.addPageEpilog(buffer);
48                 return buffer.toString();
49         }
50         
51         /*
52          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
53          */
54         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
55                 
56                 if (getEditor() == null)
57                         return null;
58                 
59                 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
60                 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
61                 
62                 if (model != null) {
63                         Iterator e= new JavaAnnotationIterator(model, true);
64                         int layer= -1;
65                         String message= null;
66                         while (e.hasNext()) {
67                                 Annotation a= (Annotation) e.next();
68
69                                 AnnotationPreference preference= getAnnotationPreference(a);
70                                 if (preference == null || !(fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
71                                         continue;
72
73                                 Position p= model.getPosition(a);
74                                 
75                                 int l= fAnnotationAccess.getLayer(a);
76                                 
77                                 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
78                                         String msg= a.getText();
79                                         if (msg != null && msg.trim().length() > 0) {
80                                                 message= msg;
81                                                 layer= l;
82                                         }
83                                 }
84                         }
85                         if (layer > -1)
86                                 return formatMessage(message);
87                 }
88                 
89                 return null;
90         }
91         
92         /*
93          * @see IJavaEditorTextHover#setEditor(IEditorPart)
94          */
95         public void setEditor(IEditorPart editor) {
96                 if (editor instanceof PHPUnitEditor)
97                         super.setEditor(editor);
98                 else
99                         super.setEditor(null);
100         }
101
102         /**
103          * Returns the annotation preference for the given annotation.
104          *
105          * @param annotation the annotation
106          * @return the annotation preference or <code>null</code> if none
107          */     
108         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
109                 
110                 if (annotation.isMarkedDeleted())
111                         return null;
112                 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
113         }
114
115         static boolean isJavaProblemHover(String id) {
116                 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);
117         }
118 }