929f858d3d1e85c2ee6739f858281467ddc12ac7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / JavaExpandHover.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
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.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.phpeditor.JavaMarkerAnnotation;
21
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;
45
46 /**
47  * 
48  * 
49  * @since 3.0
50  */
51 public class JavaExpandHover extends AnnotationExpandHover {
52         
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$
55
56         private static class NoBreakpointAnnotation extends Annotation implements IAnnotationPresentation {
57                 
58                 public NoBreakpointAnnotation() {
59                         super(NO_BREAKPOINT_ANNOTATION, false, JavaHoverMessages.getString("NoBreakpointAnnotation.addBreakpoint")); //$NON-NLS-1$
60                 }
61                 
62                 /*
63                  * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
64                  */
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);
69                 }
70                 
71                 /*
72                  * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
73                  */
74                 public int getLayer() {
75                         return IAnnotationPresentation.DEFAULT_LAYER;
76                 }
77         }
78         
79         private AnnotationPreferenceLookup fLookup= new AnnotationPreferenceLookup();
80         private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
81         
82         public JavaExpandHover(CompositeRuler ruler, IAnnotationAccess access, IDoubleClickListener doubleClickListener) {
83                 super(ruler, access, doubleClickListener);
84         }
85         
86         /*
87          * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getHoverInfoForLine(org.eclipse.jface.text.source.ISourceViewer, int)
88          */
89         protected Object getHoverInfoForLine(final ISourceViewer viewer, final int line) {
90                 IAnnotationModel model= viewer.getAnnotationModel();
91                 IDocument document= viewer.getDocument();
92                 
93                 if (model == null)
94                         return null;
95                 
96                 List exact= new ArrayList();
97                 HashMap messagesAtPosition= new HashMap();
98                 
99                 Iterator e= model.getAnnotationIterator();
100                 while (e.hasNext()) {
101                         Annotation annotation= (Annotation) e.next();
102                         
103                         // don't prune deleted ones as we don't get many errors this way
104 //                      if (annotation.isMarkedDeleted())
105 //                              continue;
106                         
107                         if (fAnnotationAccess instanceof IAnnotationAccessExtension)
108                                 if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
109                                         continue;
110                                 
111 // TODO need a new check the this one is not OK
112 //
113 //                      if (annotation instanceof IJavaAnnotation && annotation instanceof IAnnotationPresentation)
114 //                              if (((IJavaAnnotation) annotation).getImage(display) == null)
115 //                                      continue;
116                                 
117                         AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
118                         if (pref != null) {
119                                 String 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 //              if (exact.size() < 1)
150                         return null;
151                 
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() {
159
160                         public void run(IInformationControlExtension2 control) {
161                                 control.setInput(getHoverInfoForLine(viewer, line));
162                         }
163                         
164                 };
165                 input.model= model;
166                 
167                 return input;
168         }
169         
170         /*
171          * @see org.eclipse.ui.internal.texteditor.AnnotationExpandHover#getOrder(org.eclipse.jface.text.source.Annotation)
172          */
173         protected int getOrder(Annotation annotation) {
174                 if (isBreakpointAnnotation(annotation)) //$NON-NLS-1$
175                         return 1000;
176                 else
177                         return super.getOrder(annotation);
178         }
179
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$
185                 }
186                 return false;
187         }
188 }