KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > AbstractAnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.text.java.hover;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18
19 import org.eclipse.core.filebuffers.FileBuffers;
20 import org.eclipse.core.filebuffers.ITextFileBufferManager;
21 import org.eclipse.core.filebuffers.LocationKind;
22
23 import org.eclipse.jface.internal.text.html.HTMLPrinter;
24 import org.eclipse.jface.preference.IPreferenceStore;
25
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.text.Position;
29 import org.eclipse.jface.text.source.Annotation;
30 import org.eclipse.jface.text.source.IAnnotationModel;
31 import org.eclipse.jface.text.source.ISourceViewer;
32
33 import org.eclipse.ui.editors.text.EditorsUI;
34
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IStorageEditorInput;
37 import org.eclipse.ui.texteditor.AnnotationPreference;
38 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaAnnotationIterator;
42
43
44 /**
45  * Abstract super class for annotation hovers.
46  *
47  * @since 3.0
48  */

49 public abstract class AbstractAnnotationHover extends AbstractJavaEditorTextHover {
50
51     private IPreferenceStore fStore= JavaPlugin.getDefault().getCombinedPreferenceStore();
52     private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess();
53     private boolean fAllAnnotations;
54
55
56     public AbstractAnnotationHover(boolean allAnnotations) {
57         fAllAnnotations= allAnnotations;
58     }
59
60     /*
61      * Formats a message as HTML text.
62      */

63     private String JavaDoc formatMessage(String JavaDoc message) {
64         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
65         HTMLPrinter.insertPageProlog(buffer, 0, getStyleSheet());
66         buffer.append(HTMLPrinter.convertToHTMLContent(message));
67         HTMLPrinter.addPageEpilog(buffer);
68         return buffer.toString();
69     }
70
71     /*
72      * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
73      */

74     public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
75         IPath path;
76         IAnnotationModel model;
77         if (textViewer instanceof ISourceViewer) {
78             path= null;
79             model= ((ISourceViewer)textViewer).getAnnotationModel();
80         } else {
81             // Get annotation model from file buffer manager
82
path= getEditorInputPath();
83             model= getAnnotationModel(path);
84         }
85         if (model == null)
86             return null;
87
88         try {
89             Iterator JavaDoc e= new JavaAnnotationIterator(model, true, fAllAnnotations);
90             int layer= -1;
91             String JavaDoc message= null;
92             while (e.hasNext()) {
93                 Annotation a= (Annotation) e.next();
94
95                 AnnotationPreference preference= getAnnotationPreference(a);
96                 if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
97                     continue;
98
99                 Position p= model.getPosition(a);
100
101                 int l= fAnnotationAccess.getLayer(a);
102
103                 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
104                     String JavaDoc 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         } finally {
115             try {
116                 if (path != null) {
117                     ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
118                     manager.disconnect(path, LocationKind.NORMALIZE, null);
119                 }
120             } catch (CoreException ex) {
121                 JavaPlugin.log(ex.getStatus());
122             }
123         }
124
125         return null;
126     }
127
128     private IPath getEditorInputPath() {
129         if (getEditor() == null)
130             return null;
131
132         IEditorInput input= getEditor().getEditorInput();
133         if (input instanceof IStorageEditorInput) {
134             try {
135                 return ((IStorageEditorInput)input).getStorage().getFullPath();
136             } catch (CoreException ex) {
137                 JavaPlugin.log(ex.getStatus());
138             }
139         }
140         return null;
141     }
142
143     private IAnnotationModel getAnnotationModel(IPath path) {
144         if (path == null)
145             return null;
146
147         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
148         try {
149             manager.connect(path, LocationKind.NORMALIZE, null);
150         } catch (CoreException ex) {
151             JavaPlugin.log(ex.getStatus());
152             return null;
153         }
154
155         IAnnotationModel model= null;
156         try {
157             model= manager.getTextFileBuffer(path, LocationKind.NORMALIZE).getAnnotationModel();
158             return model;
159         } finally {
160             if (model == null) {
161                 try {
162                     manager.disconnect(path, LocationKind.NORMALIZE, null);
163                 } catch (CoreException ex) {
164                     JavaPlugin.log(ex.getStatus());
165                 }
166             }
167         }
168     }
169
170     /**
171      * Returns the annotation preference for the given annotation.
172      *
173      * @param annotation the annotation
174      * @return the annotation preference or <code>null</code> if none
175      */

176     private AnnotationPreference getAnnotationPreference(Annotation annotation) {
177
178         if (annotation.isMarkedDeleted())
179             return null;
180         return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
181     }
182 }
183
Popular Tags