KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaCompletionProposalComputer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.ui.text.java;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.MessageDialog;
27
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.jface.text.contentassist.ICompletionProposal;
30 import org.eclipse.jface.text.contentassist.IContextInformation;
31 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
32
33 import org.eclipse.jdt.core.CompletionProposal;
34 import org.eclipse.jdt.core.CompletionRequestor;
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.JavaModelException;
37
38 import org.eclipse.jdt.ui.PreferenceConstants;
39 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector;
40 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
41 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
42 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
43
44 /**
45  * Computes Java completion proposals and context infos.
46  *
47  * @since 3.2
48  */

49 public class JavaCompletionProposalComputer implements IJavaCompletionProposalComputer {
50
51     private static final class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
52
53         private final IContextInformation fContextInformation;
54         private int fPosition;
55
56         public ContextInformationWrapper(IContextInformation contextInformation) {
57             fContextInformation= contextInformation;
58         }
59
60         /*
61          * @see IContextInformation#getContextDisplayString()
62          */

63         public String JavaDoc getContextDisplayString() {
64             return fContextInformation.getContextDisplayString();
65         }
66
67             /*
68          * @see IContextInformation#getImage()
69          */

70         public Image getImage() {
71             return fContextInformation.getImage();
72         }
73
74         /*
75          * @see IContextInformation#getInformationDisplayString()
76          */

77         public String JavaDoc getInformationDisplayString() {
78             return fContextInformation.getInformationDisplayString();
79         }
80
81         /*
82          * @see IContextInformationExtension#getContextInformationPosition()
83          */

84         public int getContextInformationPosition() {
85             return fPosition;
86         }
87
88         public void setContextInformationPosition(int position) {
89             fPosition= position;
90         }
91
92         /*
93          * @see org.eclipse.jface.text.contentassist.IContextInformation#equals(java.lang.Object)
94          */

95         public boolean equals(Object JavaDoc object) {
96             if (object instanceof ContextInformationWrapper)
97                 return fContextInformation.equals(((ContextInformationWrapper) object).fContextInformation);
98             else
99                 return fContextInformation.equals(object);
100         }
101     }
102     
103     private String JavaDoc fErrorMessage;
104     
105     public JavaCompletionProposalComputer() {
106     }
107
108     protected int guessContextInformationPosition(ContentAssistInvocationContext context) {
109         return context.getInvocationOffset();
110     }
111
112     private List JavaDoc addContextInformations(JavaContentAssistInvocationContext context, int offset, IProgressMonitor monitor) {
113         List JavaDoc proposals= internalComputeCompletionProposals(offset, context, monitor);
114         List JavaDoc result= new ArrayList JavaDoc(proposals.size());
115
116         for (Iterator JavaDoc it= proposals.iterator(); it.hasNext();) {
117             ICompletionProposal proposal= (ICompletionProposal) it.next();
118             IContextInformation contextInformation= proposal.getContextInformation();
119             if (contextInformation != null) {
120                 ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation);
121                 wrapper.setContextInformationPosition(offset);
122                 result.add(wrapper);
123             }
124         }
125         return result;
126     }
127
128     /*
129      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
130      */

131     public List JavaDoc computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
132         if (context instanceof JavaContentAssistInvocationContext) {
133             JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
134             
135             int contextInformationPosition= guessContextInformationPosition(javaContext);
136             List JavaDoc result= addContextInformations(javaContext, contextInformationPosition, monitor);
137             return result;
138         }
139         return Collections.EMPTY_LIST;
140     }
141
142     /*
143      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
144      */

145     public List JavaDoc computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
146         if (context instanceof JavaContentAssistInvocationContext) {
147             JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
148             return internalComputeCompletionProposals(context.getInvocationOffset(), javaContext, monitor);
149         }
150         return Collections.EMPTY_LIST;
151     }
152
153     private List JavaDoc internalComputeCompletionProposals(int offset, JavaContentAssistInvocationContext context, IProgressMonitor monitor) {
154         ICompilationUnit unit= context.getCompilationUnit();
155         if (unit == null)
156             return Collections.EMPTY_LIST;
157         
158         ITextViewer viewer= context.getViewer();
159         
160         CompletionProposalCollector collector= createCollector(context);
161         collector.setInvocationContext(context);
162         
163         // Allow completions for unresolved types - since 3.3
164
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
165         collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
166         collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
167         
168         collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
169         collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
170         collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
171
172         // Set the favorite list to propose static members - since 3.3
173
collector.setFavoriteReferences(getFavoriteStaticMembers());
174
175         try {
176             Point selection= viewer.getSelectedRange();
177             if (selection.y > 0)
178                 collector.setReplacementLength(selection.y);
179             
180                 unit.codeComplete(offset, collector);
181         } catch (JavaModelException x) {
182             Shell shell= viewer.getTextWidget().getShell();
183             if (x.isDoesNotExist() && !unit.getJavaProject().isOnClasspath(unit))
184                 MessageDialog.openInformation(shell, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_title, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_message);
185             else
186                 ErrorDialog.openError(shell, JavaTextMessages.CompletionProcessor_error_accessing_title, JavaTextMessages.CompletionProcessor_error_accessing_message, x.getStatus());
187         }
188
189         ICompletionProposal[] javaProposals= collector.getJavaCompletionProposals();
190         int contextInformationOffset= guessContextInformationPosition(context);
191         if (contextInformationOffset != offset) {
192             for (int i= 0; i < javaProposals.length; i++) {
193                 if (javaProposals[i] instanceof JavaMethodCompletionProposal) {
194                     JavaMethodCompletionProposal jmcp= (JavaMethodCompletionProposal) javaProposals[i];
195                     jmcp.setContextInformationPosition(contextInformationOffset);
196                 }
197             }
198         }
199         
200         List JavaDoc proposals= new ArrayList JavaDoc(Arrays.asList(javaProposals));
201         if (proposals.size() == 0) {
202             String JavaDoc error= collector.getErrorMessage();
203             if (error.length() > 0)
204                 fErrorMessage= error;
205         }
206         return proposals;
207     }
208
209     /**
210      * Returns the array with favorite static members.
211      *
212      * @return the <code>String</code> array with with favorite static members
213      * @see CompletionRequestor#setFavoriteReferences(String[])
214      * @since 3.3
215      */

216     private String JavaDoc[] getFavoriteStaticMembers() {
217         String JavaDoc serializedFavorites= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
218         if (serializedFavorites != null && serializedFavorites.length() > 0)
219             return serializedFavorites.split(";"); //$NON-NLS-1$
220
return new String JavaDoc[0];
221     }
222
223     /**
224      * Creates the collector used to get proposals from core.
225      */

226     protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
227         if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES))
228             return new FillArgumentNamesCompletionProposalCollector(context);
229         else
230             return new CompletionProposalCollector(context.getCompilationUnit());
231     }
232
233     /*
234      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
235      */

236     public String JavaDoc getErrorMessage() {
237         return fErrorMessage;
238     }
239
240     /*
241      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
242      */

243     public void sessionStarted() {
244     }
245
246     /*
247      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
248      */

249     public void sessionEnded() {
250         fErrorMessage= null;
251     }
252 }
253
Popular Tags