64fc808293f47b438000cb4d0e31dc1bee70b853
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / AbstractAnnotationHover.java
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
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationModel;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.editors.text.EditorsUI;
29 import org.eclipse.ui.texteditor.AnnotationPreference;
30 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
31 import org.eclipse.ui.texteditor.IDocumentProvider;
32
33 /**
34  * Abstract super class for annotation hovers.
35  * 
36  * @since 3.0
37  */
38 public abstract class AbstractAnnotationHover extends
39                 AbstractJavaEditorTextHover {
40
41         private IPreferenceStore fStore = PHPeclipsePlugin.getDefault()
42                         .getCombinedPreferenceStore();
43
44         private DefaultMarkerAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
45
46         private boolean fAllAnnotations;
47
48         public AbstractAnnotationHover(boolean allAnnotations) {
49                 fAllAnnotations = allAnnotations;
50         }
51
52         /*
53          * Formats a message as HTML text.
54          */
55         private String formatMessage(String message) {
56                 StringBuffer buffer = new StringBuffer();
57                 HTMLPrinter.addPageProlog(buffer);
58                 HTMLPrinter.addParagraph(buffer, HTMLPrinter
59                                 .convertToHTMLContent(message));
60                 HTMLPrinter.addPageEpilog(buffer);
61                 return buffer.toString();
62         }
63
64         /*
65          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
66          */
67         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
68
69                 if (getEditor() == null)
70                         return null;
71
72                 IDocumentProvider provider = PHPeclipsePlugin.getDefault()
73                                 .getCompilationUnitDocumentProvider();
74                 IAnnotationModel model = provider.getAnnotationModel(getEditor()
75                                 .getEditorInput());
76
77                 if (model != null) {
78                         Iterator e = new JavaAnnotationIterator(model, true,
79                                         fAllAnnotations);
80                         int layer = -1;
81                         String message = null;
82                         while (e.hasNext()) {
83                                 Annotation a = (Annotation) e.next();
84
85                                 AnnotationPreference preference = getAnnotationPreference(a);
86                                 if (preference == null
87                                                 || !(preference.getTextPreferenceKey() != null
88                                                                 && fStore.getBoolean(preference
89                                                                                 .getTextPreferenceKey()) || (preference
90                                                                 .getHighlightPreferenceKey() != null && fStore
91                                                                 .getBoolean(preference
92                                                                                 .getHighlightPreferenceKey()))))
93                                         continue;
94
95                                 Position p = model.getPosition(a);
96
97                                 int l = fAnnotationAccess.getLayer(a);
98
99                                 if (l > layer
100                                                 && p != null
101                                                 && p.overlapsWith(hoverRegion.getOffset(), hoverRegion
102                                                                 .getLength())) {
103                                         String msg = a.getText();
104                                         if (msg != null && msg.trim().length() > 0) {
105                                                 message = msg;
106                                                 layer = l;
107                                         }
108                                 }
109                         }
110                         if (layer > -1)
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                 else
124                         super.setEditor(null);
125         }
126
127         /**
128          * Returns the annotation preference for the given annotation.
129          * 
130          * @param annotation
131          *            the annotation
132          * @return the annotation preference or <code>null</code> if none
133          */
134         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
135
136                 if (annotation.isMarkedDeleted())
137                         return null;
138                 return EditorsUI.getAnnotationPreferenceLookup()
139                                 .getAnnotationPreference(annotation);
140         }
141 }