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