Goto line/Goto Matching Bracket Shortcuts should work
[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 /**
35  * Abstract super class for annotation hovers.
36  * 
37  * @since 3.0
38  */
39 public abstract class AbstractAnnotationHover extends AbstractJavaEditorTextHover {
40
41         private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
42         private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
43         private boolean fAllAnnotations;
44         
45         
46         public AbstractAnnotationHover(boolean allAnnotations) {
47                 fAllAnnotations= allAnnotations;
48         }
49         
50         /*
51          * Formats a message as HTML text.
52          */
53         private String formatMessage(String message) {
54                 StringBuffer buffer= new StringBuffer();
55                 HTMLPrinter.addPageProlog(buffer);
56                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
57                 HTMLPrinter.addPageEpilog(buffer);
58                 return buffer.toString();
59         }
60         
61         /*
62          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
63          */
64         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
65                 
66                 if (getEditor() == null)
67                         return null;
68                 
69                 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
70                 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
71                 
72                 if (model != null) {
73                         Iterator e= new JavaAnnotationIterator(model, true, fAllAnnotations);
74                         int layer= -1;
75                         String message= null;
76                         while (e.hasNext()) {
77                                 Annotation a= (Annotation) e.next();
78
79                                 AnnotationPreference preference= getAnnotationPreference(a);
80                                 if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
81                                         continue;
82
83                                 Position p= model.getPosition(a);
84                                 
85                                 int l= fAnnotationAccess.getLayer(a);
86                                 
87                                 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
88                                         String msg= a.getText();
89                                         if (msg != null && msg.trim().length() > 0) {
90                                                 message= msg;
91                                                 layer= l;
92                                         }
93                                 }
94                         }
95                         if (layer > -1)
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                 else
109                         super.setEditor(null);
110         }
111
112         /**
113          * Returns the annotation preference for the given annotation.
114          *
115          * @param annotation the annotation
116          * @return the annotation preference or <code>null</code> if none
117          */     
118         private AnnotationPreference getAnnotationPreference(Annotation annotation) {
119                 
120                 if (annotation.isMarkedDeleted())
121                         return null;
122                 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
123         }
124 }