c1bbbdbb8ee0ab2db69f88107b83cdf412825bd7
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.JavaAnnotationIterator;
19 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
20 import net.sourceforge.phpeclipse.ui.WebUI;
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  * Abstract super class for annotation hovers.
36  * 
37  * @since 3.0
38  */
39 public abstract class AbstractAnnotationHover extends
40                 AbstractJavaEditorTextHover {
41
42         private IPreferenceStore fStore = WebUI.getDefault()
43                         .getCombinedPreferenceStore();
44
45         private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
46
47         private boolean fAllAnnotations;
48
49         public AbstractAnnotationHover(boolean allAnnotations) {
50                 fAllAnnotations = allAnnotations;
51         }
52
53         /*
54          * Formats a message as HTML text.
55          */
56         private String formatMessage(String message) {
57                 StringBuffer buffer = new StringBuffer();
58                 HTMLPrinter.addPageProlog(buffer);
59                 HTMLPrinter.addParagraph(buffer, HTMLPrinter
60                                 .convertToHTMLContent(message));
61                 HTMLPrinter.addPageEpilog(buffer);
62                 return buffer.toString();
63         }
64
65         /*
66          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
67          */
68         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
69
70                 if (getEditor() == null)
71                         return null;
72
73                 IDocumentProvider provider = WebUI.getDefault()
74                                 .getCompilationUnitDocumentProvider();
75                 IAnnotationModel model = provider.getAnnotationModel(getEditor()
76                                 .getEditorInput());
77
78                 if (model != null) {
79                         Iterator e = new JavaAnnotationIterator(model, true,
80                                         fAllAnnotations);
81                         int layer = -1;
82                         String message = null;
83                         while (e.hasNext()) {
84                                 Annotation a = (Annotation) e.next();
85
86                                 AnnotationPreference preference = getAnnotationPreference(a);
87                                 if (preference == null
88                                                 || !(preference.getTextPreferenceKey() != null
89                                                                 && fStore.getBoolean(preference
90                                                                                 .getTextPreferenceKey()) || (preference
91                                                                 .getHighlightPreferenceKey() != null && fStore
92                                                                 .getBoolean(preference
93                                                                                 .getHighlightPreferenceKey()))))
94                                         continue;
95
96                                 Position p = model.getPosition(a);
97
98                                 int l = fAnnotationAccess.getLayer(a);
99
100                                 if (l > layer
101                                                 && p != null
102                                                 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
103                                                                 .getLength())) {
104                                         String msg = a.getText();
105                                         if (msg != null && msg.trim().length() > 0) {
106                                                 message = msg;
107                                                 layer = l;
108                                         }
109                                 }
110                         }
111                         if (layer > -1)
112                                 return formatMessage(message);
113                 }
114
115                 return null;
116         }
117
118         /*
119          * @see IJavaEditorTextHover#setEditor(IEditorPart)
120          */
121         public void setEditor(IEditorPart editor) {
122                 if (editor instanceof PHPUnitEditor)
123                         super.setEditor(editor);
124                 else
125                         super.setEditor(null);
126         }
127
128         /**
129          * Returns the annotation preference for the given annotation.
130          * 
131          * @param annotation
132          *            the annotation
133          * @return the annotation preference or <code>null</code> if none
134          */
135         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
136
137                 if (annotation.isMarkedDeleted())
138                         return null;
139                 return EditorsUI.getAnnotationPreferenceLookup()
140                                 .getAnnotationPreference(annotation);
141         }
142 }