KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jdt.internal.ui.text.java.hover;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.Canvas;
23
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.viewers.IDoubleClickListener;
26
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IInformationControlExtension2;
29 import org.eclipse.jface.text.Position;
30 import org.eclipse.jface.text.source.Annotation;
31 import org.eclipse.jface.text.source.CompositeRuler;
32 import org.eclipse.jface.text.source.IAnnotationAccess;
33 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
34 import org.eclipse.jface.text.source.IAnnotationModel;
35 import org.eclipse.jface.text.source.IAnnotationPresentation;
36 import org.eclipse.jface.text.source.ISourceViewer;
37 import org.eclipse.jface.text.source.ImageUtilities;
38
39 import org.eclipse.ui.texteditor.AnnotationPreference;
40 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
41
42 import org.eclipse.jdt.ui.PreferenceConstants;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.JavaPluginImages;
46 import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation;
47 import org.eclipse.jdt.internal.ui.javaeditor.JavaMarkerAnnotation;
48 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider.ProblemAnnotation;
49 import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
50 import org.eclipse.jdt.internal.ui.text.java.hover.AnnotationExpansionControl.AnnotationHoverInput;
51
52 /**
53  *
54  *
55  * @since 3.0
56  */

57 public class JavaExpandHover extends AnnotationExpandHover {
58
59     /** Id of the no breakpoint fake annotation */
60     public static final String JavaDoc NO_BREAKPOINT_ANNOTATION= "org.eclipse.jdt.internal.ui.NoBreakpointAnnotation"; //$NON-NLS-1$
61

62     private static class NoBreakpointAnnotation extends Annotation implements IAnnotationPresentation {
63
64         public NoBreakpointAnnotation() {
65             super(NO_BREAKPOINT_ANNOTATION, false, JavaHoverMessages.NoBreakpointAnnotation_addBreakpoint);
66         }
67
68         /*
69          * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
70          */

71         public void paint(GC gc, Canvas canvas, Rectangle bounds) {
72             // draw affordance so the user know she can click here to get a breakpoint
73
Image fImage= JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PUBLIC);
74             ImageUtilities.drawImage(fImage, gc, canvas, bounds, SWT.CENTER);
75         }
76
77         /*
78          * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
79          */

80         public int getLayer() {
81             return IAnnotationPresentation.DEFAULT_LAYER;
82         }
83     }
84
85     private AnnotationPreferenceLookup fLookup= new AnnotationPreferenceLookup();
86     private IPreferenceStore fStore= JavaPlugin.getDefault().getCombinedPreferenceStore();
87
88     public JavaExpandHover(CompositeRuler ruler, IAnnotationAccess access, IDoubleClickListener doubleClickListener) {
89         super(ruler, access, doubleClickListener);
90     }
91
92     /*
93      * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getHoverInfoForLine(org.eclipse.jface.text.source.ISourceViewer, int)
94      */

95     protected Object JavaDoc getHoverInfoForLine(final ISourceViewer viewer, final int line) {
96         final boolean showTemporaryProblems= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
97         IAnnotationModel model= viewer.getAnnotationModel();
98         IDocument document= viewer.getDocument();
99
100         if (model == null)
101             return null;
102
103         List JavaDoc exact= new ArrayList JavaDoc();
104         HashMap JavaDoc messagesAtPosition= new HashMap JavaDoc();
105
106         Iterator JavaDoc e= model.getAnnotationIterator();
107         while (e.hasNext()) {
108             Annotation annotation= (Annotation) e.next();
109
110             if (fAnnotationAccess instanceof IAnnotationAccessExtension)
111                 if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
112                     continue;
113             
114             if (annotation instanceof IJavaAnnotation && !isIncluded((IJavaAnnotation)annotation, showTemporaryProblems))
115                 continue;
116
117             AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
118             if (pref != null) {
119                 String JavaDoc key= pref.getVerticalRulerPreferenceKey();
120                 if (key != null && !fStore.getBoolean(key))
121                     continue;
122             }
123
124             Position position= model.getPosition(annotation);
125             if (position == null)
126                 continue;
127
128             if (compareRulerLine(position, document, line) == 1) {
129
130                 if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
131                     continue;
132
133                 exact.add(annotation);
134             }
135         }
136
137         sort(exact, model);
138
139         if (exact.size() > 0)
140             setLastRulerMouseLocation(viewer, line);
141
142         if (exact.size() > 0) {
143             Annotation first= (Annotation) exact.get(0);
144             if (!isBreakpointAnnotation(first))
145                 exact.add(0, new NoBreakpointAnnotation());
146         }
147
148         if (exact.size() <= 1)
149             return null;
150
151         AnnotationHoverInput input= new AnnotationHoverInput();
152         input.fAnnotations= (Annotation[]) exact.toArray(new Annotation[0]);
153         input.fViewer= viewer;
154         input.fRulerInfo= fCompositeRuler;
155         input.fAnnotationListener= fgListener;
156         input.fDoubleClickListener= fDblClickListener;
157         input.redoAction= new AnnotationExpansionControl.ICallback() {
158
159             public void run(IInformationControlExtension2 control) {
160                 control.setInput(getHoverInfoForLine(viewer, line));
161             }
162
163         };
164         input.model= model;
165
166         return input;
167     }
168
169     private boolean isIncluded(IJavaAnnotation annotation, boolean showTemporaryProblems) {
170         
171         // XXX: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=138601
172
if (annotation instanceof ProblemAnnotation && JavaMarkerAnnotation.TASK_ANNOTATION_TYPE.equals(annotation.getType()))
173             return false;
174         
175         if (!annotation.isProblem())
176             return true;
177         
178         if (annotation.isMarkedDeleted() && !annotation.hasOverlay())
179             return true;
180         
181         if (annotation.hasOverlay() && !annotation.isMarkedDeleted())
182             return true;
183         
184         
185         if (annotation.hasOverlay())
186             return (!isIncluded(annotation.getOverlay(), showTemporaryProblems));
187         
188         return showTemporaryProblems && JavaCorrectionProcessor.hasCorrections((Annotation)annotation);
189     }
190
191     /*
192      * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getOrder(org.eclipse.jface.text.source.Annotation)
193      */

194     protected int getOrder(Annotation annotation) {
195         if (isBreakpointAnnotation(annotation))
196             return 1000;
197         else
198             return super.getOrder(annotation);
199     }
200
201     private boolean isBreakpointAnnotation(Annotation a) {
202         if (a instanceof JavaMarkerAnnotation) {
203             JavaMarkerAnnotation jma= (JavaMarkerAnnotation) a;
204             // HACK to get breakpoints to show up first
205
return jma.getType().equals("org.eclipse.debug.core.breakpoint"); //$NON-NLS-1$
206
}
207         return false;
208     }
209 }
210
Popular Tags