Make PHPeclipse compile correctly under Eclipse Version: 3.2.0 Build id: I20060519...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / JavaExpandHover.java
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 net.sourceforge.phpdt.internal.ui.text.java.hover;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
19 import net.sourceforge.phpdt.internal.ui.text.java.hover.AnnotationExpansionControl.AnnotationHoverInput;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import net.sourceforge.phpeclipse.phpeditor.IJavaAnnotation;
23 import net.sourceforge.phpeclipse.phpeditor.JavaMarkerAnnotation;
24 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider.ProblemAnnotation;
25
26 import org.eclipse.jface.preference.IPreferenceStore;
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 import org.eclipse.jface.viewers.IDoubleClickListener;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.graphics.GC;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.graphics.Rectangle;
43 import org.eclipse.swt.widgets.Canvas;
44 import org.eclipse.ui.texteditor.AnnotationPreference;
45 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
46
47 /**
48  *
49  *
50  * @since 3.0
51  */
52 public class JavaExpandHover extends AnnotationExpandHover {
53
54         /** Id of the no breakpoint fake annotation */
55         public static final String NO_BREAKPOINT_ANNOTATION= "net.sourceforge.phpdt.internal.ui.NoBreakpointAnnotation"; //$NON-NLS-1$
56
57         private static class NoBreakpointAnnotation extends Annotation implements IAnnotationPresentation {
58
59                 public NoBreakpointAnnotation() {
60                         super(NO_BREAKPOINT_ANNOTATION, false, JavaHoverMessages.getString("NoBreakpointAnnotation.addBreakpoint"));
61                 }
62
63                 /*
64                  * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
65                  */
66                 public void paint(GC gc, Canvas canvas, Rectangle bounds) {
67                         // draw affordance so the user know she can click here to get a breakpoint
68                         Image fImage= PHPUiImages.get(PHPUiImages.IMG_FIELD_PUBLIC);
69                         ImageUtilities.drawImage(fImage, gc, canvas, bounds, SWT.CENTER);
70                 }
71
72                 /*
73                  * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
74                  */
75                 public int getLayer() {
76                         return IAnnotationPresentation.DEFAULT_LAYER;
77                 }
78         }
79
80         private AnnotationPreferenceLookup fLookup= new AnnotationPreferenceLookup();
81         private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
82
83         public JavaExpandHover(CompositeRuler ruler, IAnnotationAccess access, IDoubleClickListener doubleClickListener) {
84                 super(ruler, access, doubleClickListener);
85         }
86
87         /*
88          * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getHoverInfoForLine(org.eclipse.jface.text.source.ISourceViewer, int)
89          */
90         protected Object getHoverInfoForLine(final ISourceViewer viewer, final int line) {
91                 final boolean showTemporaryProblems= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
92                 IAnnotationModel model= viewer.getAnnotationModel();
93                 IDocument document= viewer.getDocument();
94
95                 if (model == null)
96                         return null;
97
98                 List exact= new ArrayList();
99                 HashMap messagesAtPosition= new HashMap();
100
101                 Iterator e= model.getAnnotationIterator();
102                 while (e.hasNext()) {
103                         Annotation annotation= (Annotation) e.next();
104
105                         if (fAnnotationAccess instanceof IAnnotationAccessExtension)
106                                 if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
107                                         continue;
108                         
109                         if (annotation instanceof IJavaAnnotation && !isIncluded((IJavaAnnotation)annotation, showTemporaryProblems))
110                                 continue;
111
112                         AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
113                         if (pref != null) {
114                                 String key= pref.getVerticalRulerPreferenceKey();
115                                 if (key != null && !fStore.getBoolean(key))
116                                         continue;
117                         }
118
119                         Position position= model.getPosition(annotation);
120                         if (position == null)
121                                 continue;
122
123                         if (compareRulerLine(position, document, line) == 1) {
124
125                                 if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
126                                         continue;
127
128                                 exact.add(annotation);
129                         }
130                 }
131
132                 sort(exact, model);
133
134                 if (exact.size() > 0)
135                         setLastRulerMouseLocation(viewer, line);
136
137                 if (exact.size() > 0) {
138                         Annotation first= (Annotation) exact.get(0);
139                         if (!isBreakpointAnnotation(first))
140                                 exact.add(0, new NoBreakpointAnnotation());
141                 }
142
143                 if (exact.size() <= 1)
144                         return null;
145
146                 AnnotationHoverInput input= new AnnotationHoverInput();
147                 input.fAnnotations= (Annotation[]) exact.toArray(new Annotation[0]);
148                 input.fViewer= viewer;
149                 input.fRulerInfo= fCompositeRuler;
150                 input.fAnnotationListener= fgListener;
151                 input.fDoubleClickListener= fDblClickListener;
152                 input.redoAction= new AnnotationExpansionControl.ICallback() {
153
154                         public void run(IInformationControlExtension2 control) {
155                                 control.setInput(getHoverInfoForLine(viewer, line));
156                         }
157
158                 };
159                 input.model= model;
160
161                 return input;
162         }
163
164         private boolean isIncluded(IJavaAnnotation annotation, boolean showTemporaryProblems) {
165                 
166                 // XXX: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=138601
167                 if (annotation instanceof ProblemAnnotation && JavaMarkerAnnotation.TASK_ANNOTATION_TYPE.equals(annotation.getType()))
168                         return false;
169                 
170                 if (!annotation.isProblem())
171                         return true;
172                 
173                 if (annotation.isMarkedDeleted() && !annotation.hasOverlay())
174                         return true;
175                 
176                 if (annotation.hasOverlay() && !annotation.isMarkedDeleted())
177                         return true;
178                 
179                 
180                 if (annotation.hasOverlay())
181                         return (!isIncluded(annotation.getOverlay(), showTemporaryProblems));
182                 
183                 return showTemporaryProblems; //&& JavaCorrectionProcessor.hasCorrections((Annotation)annotation);
184         }
185
186         /*
187          * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getOrder(org.eclipse.jface.text.source.Annotation)
188          */
189         protected int getOrder(Annotation annotation) {
190                 if (isBreakpointAnnotation(annotation)) 
191                         return 1000;
192                 else
193                         return super.getOrder(annotation);
194         }
195
196         private boolean isBreakpointAnnotation(Annotation a) {
197                 if (a instanceof JavaMarkerAnnotation) {
198                         JavaMarkerAnnotation jma= (JavaMarkerAnnotation) a;
199                         // HACK to get breakpoints to show up first
200                         return jma.getType().equals("org.eclipse.debug.core.breakpoint"); //$NON-NLS-1$
201                 }
202                 return false;
203         }
204 }