KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > java > JavaContentAssistInvocationContext


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 org.eclipse.jdt.ui.text.java;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.text.ITextViewer;
16
17 import org.eclipse.ui.IEditorPart;
18
19 import org.eclipse.jdt.core.CompletionContext;
20 import org.eclipse.jdt.core.CompletionProposal;
21 import org.eclipse.jdt.core.ICodeAssist;
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
29
30 import org.eclipse.jdt.internal.ui.JavaPlugin;
31 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
32 import org.eclipse.jdt.internal.ui.text.java.ContentAssistHistory.RHSHistory;
33
34 /**
35  * Describes the context of a content assist invocation in a Java editor.
36  * <p>
37  * Clients may use but not subclass this class.
38  * </p>
39  *
40  * @since 3.2
41  */

42 public class JavaContentAssistInvocationContext extends ContentAssistInvocationContext {
43     private final IEditorPart fEditor;
44     
45     private ICompilationUnit fCU= null;
46     private boolean fCUComputed= false;
47     
48     private CompletionProposalLabelProvider fLabelProvider;
49     private CompletionProposalCollector fCollector;
50     private RHSHistory fRHSHistory;
51     private IType fType;
52
53     private IJavaCompletionProposal[] fKeywordProposals= null;
54     private CompletionContext fCoreContext= null;
55
56     /**
57      * Creates a new context.
58      *
59      * @param viewer the viewer used by the editor
60      * @param offset the invocation offset
61      * @param editor the editor that content assist is invoked in
62      */

63     public JavaContentAssistInvocationContext(ITextViewer viewer, int offset, IEditorPart editor) {
64         super(viewer, offset);
65         Assert.isNotNull(editor);
66         fEditor= editor;
67     }
68     
69     /**
70      * Creates a new context.
71      *
72      * @param unit the compilation unit in <code>document</code>
73      */

74     public JavaContentAssistInvocationContext(ICompilationUnit unit) {
75         super();
76         fCU= unit;
77         fCUComputed= true;
78         fEditor= null;
79     }
80     
81     /**
82      * Returns the compilation unit that content assist is invoked in, <code>null</code> if there
83      * is none.
84      *
85      * @return the compilation unit that content assist is invoked in, possibly <code>null</code>
86      */

87     public ICompilationUnit getCompilationUnit() {
88         if (!fCUComputed) {
89             fCUComputed= true;
90             if (fCollector != null)
91                 fCU= fCollector.getCompilationUnit();
92             else {
93                 IJavaElement je= EditorUtility.getEditorInputJavaElement(fEditor, false);
94                 if (je instanceof ICompilationUnit)
95                     fCU= (ICompilationUnit)je;
96             }
97         }
98         return fCU;
99     }
100     
101     /**
102      * Returns the project of the compilation unit that content assist is invoked in,
103      * <code>null</code> if none.
104      *
105      * @return the current java project, possibly <code>null</code>
106      */

107     public IJavaProject getProject() {
108         ICompilationUnit unit= getCompilationUnit();
109         return unit == null ? null : unit.getJavaProject();
110     }
111     
112     /**
113      * Returns the keyword proposals that are available in this context, possibly none.
114      * <p>
115      * <strong>Note:</strong> This method may run
116      * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
117      * on the compilation unit.
118      * </p>
119      *
120      * @return the available keyword proposals
121      */

122     public IJavaCompletionProposal[] getKeywordProposals() {
123         if (fKeywordProposals == null) {
124             if (fCollector != null && !fCollector.isIgnored(CompletionProposal.KEYWORD) && fCollector.getContext() != null) {
125                 // use the existing collector if it exists, collects keywords, and has already been invoked
126
fKeywordProposals= fCollector.getKeywordCompletionProposals();
127             } else {
128                 // otherwise, retrieve keywords ourselves
129
computeKeywordsAndContext();
130             }
131         }
132         
133         return fKeywordProposals;
134     }
135
136     /**
137      * Returns the {@link CompletionContext core completion context} if available, <code>null</code>
138      * otherwise.
139      * <p>
140      * <strong>Note:</strong> This method may run
141      * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
142      * on the compilation unit.
143      * </p>
144      *
145      * @return the core completion context if available, <code>null</code> otherwise
146      */

147     public CompletionContext getCoreContext() {
148         if (fCoreContext == null) {
149             // use the context from the existing collector if it exists, retrieve one ourselves otherwise
150
if (fCollector != null)
151                 fCoreContext= fCollector.getContext();
152             if (fCoreContext == null)
153                 computeKeywordsAndContext();
154         }
155         return fCoreContext;
156     }
157
158     /**
159      * Returns an float in [0.0,&nbsp;1.0] based on whether the type has been recently used as a
160      * right hand side for the type expected in the current context. 0 signals that the
161      * <code>qualifiedTypeName</code> does not match the expected type, while 1.0 signals that
162      * <code>qualifiedTypeName</code> has most recently been used in a similar context.
163      * <p>
164      * <strong>Note:</strong> This method may run
165      * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
166      * on the compilation unit.
167      * </p>
168      *
169      * @param qualifiedTypeName the type name of the type of interest
170      * @return a relevance in [0.0,&nbsp;1.0] based on previous content assist invocations
171      */

172     public float getHistoryRelevance(String JavaDoc qualifiedTypeName) {
173         return getRHSHistory().getRank(qualifiedTypeName);
174     }
175     
176     /**
177      * Returns the content assist type history for the expected type.
178      *
179      * @return the content assist type history for the expected type
180      */

181     private RHSHistory getRHSHistory() {
182         if (fRHSHistory == null) {
183             CompletionContext context= getCoreContext();
184             if (context != null) {
185                 char[][] expectedTypes= context.getExpectedTypesSignatures();
186                 if (expectedTypes != null && expectedTypes.length > 0) {
187                     String JavaDoc expected= SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0]));
188                     fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(expected);
189                 }
190             }
191             if (fRHSHistory == null)
192                 fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(null);
193         }
194         return fRHSHistory;
195     }
196     
197     /**
198      * Returns the expected type if any, <code>null</code> otherwise.
199      * <p>
200      * <strong>Note:</strong> This method may run
201      * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
202      * on the compilation unit.
203      * </p>
204      *
205      * @return the expected type if any, <code>null</code> otherwise
206      */

207     public IType getExpectedType() {
208         if (fType == null && getCompilationUnit() != null) {
209             CompletionContext context= getCoreContext();
210             if (context != null) {
211                 char[][] expectedTypes= context.getExpectedTypesSignatures();
212                 if (expectedTypes != null && expectedTypes.length > 0) {
213                     IJavaProject project= getCompilationUnit().getJavaProject();
214                     if (project != null) {
215                         try {
216                             fType= project.findType(SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0])));
217                         } catch (JavaModelException x) {
218                             JavaPlugin.log(x);
219                         }
220                     }
221                 }
222             }
223         }
224         return fType;
225     }
226     
227     /**
228      * Returns a label provider that can be used to compute proposal labels.
229      *
230      * @return a label provider that can be used to compute proposal labels
231      */

232     public CompletionProposalLabelProvider getLabelProvider() {
233         if (fLabelProvider == null) {
234             if (fCollector != null)
235                 fLabelProvider= fCollector.getLabelProvider();
236             else
237                 fLabelProvider= new CompletionProposalLabelProvider();
238         }
239
240         return fLabelProvider;
241     }
242     
243     /**
244      * Sets the collector, which is used to access the compilation unit, the core context and the
245      * label provider. This is a performance optimization: {@link IJavaCompletionProposalComputer}s
246      * may instantiate a {@link CompletionProposalCollector} and set this invocation context via
247      * {@link CompletionProposalCollector#setInvocationContext(JavaContentAssistInvocationContext)},
248      * which in turn calls this method. This allows the invocation context to retrieve the core
249      * context and keyword proposals from the existing collector, instead of computing theses values
250      * itself via {@link #computeKeywordsAndContext()}.
251      *
252      * @param collector the collector
253      */

254     void setCollector(CompletionProposalCollector collector) {
255         fCollector= collector;
256     }
257     
258     /**
259      * Fallback to retrieve a core context and keyword proposals when no collector is available.
260      * Runs code completion on the cu and collects keyword proposals. {@link #fKeywordProposals} is
261      * non-<code>null</code> after this call.
262      *
263      * @since 3.3
264      */

265     private void computeKeywordsAndContext() {
266         ICompilationUnit cu= getCompilationUnit();
267         if (cu == null) {
268             if (fKeywordProposals == null)
269                 fKeywordProposals= new IJavaCompletionProposal[0];
270             return;
271         }
272         
273         CompletionProposalCollector collector= new CompletionProposalCollector(cu);
274         collector.setIgnored(CompletionProposal.KEYWORD, false);
275         collector.setIgnored(CompletionProposal.ANNOTATION_ATTRIBUTE_REF, true);
276         collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
277         collector.setIgnored(CompletionProposal.FIELD_REF, true);
278         collector.setIgnored(CompletionProposal.LABEL_REF, true);
279         collector.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
280         collector.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
281         collector.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
282         collector.setIgnored(CompletionProposal.METHOD_REF, true);
283         collector.setIgnored(CompletionProposal.PACKAGE_REF, true);
284         collector.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
285         collector.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
286         collector.setIgnored(CompletionProposal.JAVADOC_BLOCK_TAG, true);
287         collector.setIgnored(CompletionProposal.JAVADOC_FIELD_REF, true);
288         collector.setIgnored(CompletionProposal.JAVADOC_INLINE_TAG, true);
289         collector.setIgnored(CompletionProposal.JAVADOC_METHOD_REF, true);
290         collector.setIgnored(CompletionProposal.JAVADOC_PARAM_REF, true);
291         collector.setIgnored(CompletionProposal.JAVADOC_TYPE_REF, true);
292         collector.setIgnored(CompletionProposal.JAVADOC_VALUE_REF, true);
293         collector.setIgnored(CompletionProposal.TYPE_REF, true);
294         
295         try {
296             cu.codeComplete(getInvocationOffset(), collector);
297             if (fCoreContext == null)
298                 fCoreContext= collector.getContext();
299             if (fKeywordProposals == null)
300                 fKeywordProposals= collector.getKeywordCompletionProposals();
301             if (fLabelProvider == null)
302                 fLabelProvider= collector.getLabelProvider();
303         } catch (JavaModelException x) {
304             JavaPlugin.log(x);
305             if (fKeywordProposals == null)
306                 fKeywordProposals= new IJavaCompletionProposal[0];
307         }
308     }
309     
310     /*
311      * Implementation note: There is no need to override hashCode and equals, as we only add cached
312      * values shared across one assist invocation.
313      */

314 }
315
Popular Tags