1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
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.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.phpeditor.JavaMarkerAnnotation;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IInformationControlExtension2;
25 import org.eclipse.jface.text.Position;
26 import org.eclipse.jface.text.source.Annotation;
27 import org.eclipse.jface.text.source.CompositeRuler;
28 import org.eclipse.jface.text.source.IAnnotationAccess;
29 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
30 import org.eclipse.jface.text.source.IAnnotationModel;
31 import org.eclipse.jface.text.source.IAnnotationPresentation;
32 import org.eclipse.jface.text.source.ISourceViewer;
33 import org.eclipse.jface.text.source.ImageUtilities;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.graphics.GC;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.graphics.Rectangle;
39 import org.eclipse.swt.widgets.Canvas;
40 import org.eclipse.ui.internal.texteditor.AnnotationExpandHover;
41 import org.eclipse.ui.internal.texteditor.AnnotationExpansionControl;
42 import org.eclipse.ui.internal.texteditor.AnnotationExpansionControl.AnnotationHoverInput;
43 import org.eclipse.ui.texteditor.AnnotationPreference;
44 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
51 public class JavaExpandHover extends AnnotationExpandHover {
53 /** Id of the no breakpoint fake annotation */
54 public static final String NO_BREAKPOINT_ANNOTATION= "net.sourceforge.phpdt.internal.ui.NoBreakpointAnnotation"; //$NON-NLS-1$
56 private static class NoBreakpointAnnotation extends Annotation implements IAnnotationPresentation {
58 public NoBreakpointAnnotation() {
59 super(NO_BREAKPOINT_ANNOTATION, false, JavaHoverMessages.getString("NoBreakpointAnnotation.addBreakpoint")); //$NON-NLS-1$
63 * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
65 public void paint(GC gc, Canvas canvas, Rectangle bounds) {
66 // draw affordance so the user know she can click here to get a breakpoint
67 Image fImage= PHPUiImages.get(PHPUiImages.IMG_FIELD_PUBLIC);
68 ImageUtilities.drawImage(fImage, gc, canvas, bounds, SWT.CENTER);
72 * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
74 public int getLayer() {
75 return IAnnotationPresentation.DEFAULT_LAYER;
79 private AnnotationPreferenceLookup fLookup= new AnnotationPreferenceLookup();
80 private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
82 public JavaExpandHover(CompositeRuler ruler, IAnnotationAccess access, IDoubleClickListener doubleClickListener) {
83 super(ruler, access, doubleClickListener);
87 * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getHoverInfoForLine(org.eclipse.jface.text.source.ISourceViewer, int)
89 protected Object getHoverInfoForLine(final ISourceViewer viewer, final int line) {
90 IAnnotationModel model= viewer.getAnnotationModel();
91 IDocument document= viewer.getDocument();
96 List exact= new ArrayList();
97 HashMap messagesAtPosition= new HashMap();
99 Iterator e= model.getAnnotationIterator();
100 while (e.hasNext()) {
101 Annotation annotation= (Annotation) e.next();
103 // don't prune deleted ones as we don't get many errors this way
104 // if (annotation.isMarkedDeleted())
107 if (fAnnotationAccess instanceof IAnnotationAccessExtension)
108 if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
111 // TODO need a new check the this one is not OK
113 // if (annotation instanceof IJavaAnnotation && annotation instanceof IAnnotationPresentation)
114 // if (((IJavaAnnotation) annotation).getImage(display) == null)
117 AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
119 String key= pref.getVerticalRulerPreferenceKey();
120 if (key != null && !fStore.getBoolean(key))
124 Position position= model.getPosition(annotation);
125 if (position == null)
128 if (compareRulerLine(position, document, line) == 1) {
130 if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
133 exact.add(annotation);
139 if (exact.size() > 0)
140 setLastRulerMouseLocation(viewer, line);
142 if (exact.size() > 0) {
143 Annotation first= (Annotation) exact.get(0);
144 if (!isBreakpointAnnotation(first))
145 exact.add(0, new NoBreakpointAnnotation());
148 if (exact.size() <= 1)
149 // if (exact.size() < 1)
152 AnnotationHoverInput input= new AnnotationHoverInput();
153 input.fAnnotations= (Annotation[]) exact.toArray(new Annotation[0]);
154 input.fViewer= viewer;
155 input.fRulerInfo= fCompositeRuler;
156 input.fAnnotationListener= fgListener;
157 input.fDoubleClickListener= fDblClickListener;
158 input.redoAction= new AnnotationExpansionControl.ICallback() {
160 public void run(IInformationControlExtension2 control) {
161 control.setInput(getHoverInfoForLine(viewer, line));
171 * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getOrder(org.eclipse.jface.text.source.Annotation)
173 protected int getOrder(Annotation annotation) {
174 if (isBreakpointAnnotation(annotation)) //$NON-NLS-1$
177 return super.getOrder(annotation);
180 private boolean isBreakpointAnnotation(Annotation a) {
181 if (a instanceof JavaMarkerAnnotation) {
182 JavaMarkerAnnotation jma= (JavaMarkerAnnotation) a;
183 // HACK to get breakpoints to show up first
184 return jma.getType().equals("org.eclipse.debug.core.breakpoint"); //$NON-NLS-1$