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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
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;
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;
52 public class JavaExpandHover extends AnnotationExpandHover {
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$
57 private static class NoBreakpointAnnotation extends Annotation implements IAnnotationPresentation {
59 public NoBreakpointAnnotation() {
60 super(NO_BREAKPOINT_ANNOTATION, false, JavaHoverMessages.getString("NoBreakpointAnnotation.addBreakpoint"));
64 * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
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);
73 * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
75 public int getLayer() {
76 return IAnnotationPresentation.DEFAULT_LAYER;
80 private AnnotationPreferenceLookup fLookup= new AnnotationPreferenceLookup();
81 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
83 public JavaExpandHover(CompositeRuler ruler, IAnnotationAccess access, IDoubleClickListener doubleClickListener) {
84 super(ruler, access, doubleClickListener);
88 * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getHoverInfoForLine(org.eclipse.jface.text.source.ISourceViewer, int)
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();
98 List exact= new ArrayList();
99 HashMap messagesAtPosition= new HashMap();
101 Iterator e= model.getAnnotationIterator();
102 while (e.hasNext()) {
103 Annotation annotation= (Annotation) e.next();
105 if (fAnnotationAccess instanceof IAnnotationAccessExtension)
106 if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
109 if (annotation instanceof IJavaAnnotation && !isIncluded((IJavaAnnotation)annotation, showTemporaryProblems))
112 AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
114 String key= pref.getVerticalRulerPreferenceKey();
115 if (key != null && !fStore.getBoolean(key))
119 Position position= model.getPosition(annotation);
120 if (position == null)
123 if (compareRulerLine(position, document, line) == 1) {
125 if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
128 exact.add(annotation);
134 if (exact.size() > 0)
135 setLastRulerMouseLocation(viewer, line);
137 if (exact.size() > 0) {
138 Annotation first= (Annotation) exact.get(0);
139 if (!isBreakpointAnnotation(first))
140 exact.add(0, new NoBreakpointAnnotation());
143 if (exact.size() <= 1)
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() {
154 public void run(IInformationControlExtension2 control) {
155 control.setInput(getHoverInfoForLine(viewer, line));
164 private boolean isIncluded(IJavaAnnotation annotation, boolean showTemporaryProblems) {
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()))
170 if (!annotation.isProblem())
173 if (annotation.isMarkedDeleted() && !annotation.hasOverlay())
176 if (annotation.hasOverlay() && !annotation.isMarkedDeleted())
180 if (annotation.hasOverlay())
181 return (!isIncluded(annotation.getOverlay(), showTemporaryProblems));
183 return showTemporaryProblems; //&& JavaCorrectionProcessor.hasCorrections((Annotation)annotation);
187 * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getOrder(org.eclipse.jface.text.source.Annotation)
189 protected int getOrder(Annotation annotation) {
190 if (isBreakpointAnnotation(annotation))
193 return super.getOrder(annotation);
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$