KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > contentassist > CUPositionCompletionProcessor


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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.refactoring.contentassist;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20
21 import org.eclipse.swt.graphics.Image;
22
23 import org.eclipse.jface.contentassist.IContentAssistSubjectControl;
24 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.resource.ImageDescriptor;
27
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.jface.text.contentassist.ICompletionProposal;
30 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
31 import org.eclipse.jface.text.contentassist.IContextInformation;
32 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
33
34 import org.eclipse.jdt.core.CompletionRequestor;
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaModelException;
38 import org.eclipse.jdt.core.WorkingCopyOwner;
39 import org.eclipse.jdt.core.compiler.IProblem;
40
41 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext;
42
43 import org.eclipse.jdt.ui.PreferenceConstants;
44 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
45
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
48 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
49 import org.eclipse.jdt.internal.ui.text.java.JavaTypeCompletionProposal;
50 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
51
52
53 public class CUPositionCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
54     
55     private static final ImageDescriptorRegistry IMAGE_DESC_REGISTRY= JavaPlugin.getImageDescriptorRegistry();
56     
57     private String JavaDoc fErrorMessage;
58     private char[] fProposalAutoActivationSet;
59     private CompletionProposalComparator fComparator;
60     
61     private CompletionContextRequestor fCompletionContextRequestor;
62
63     private CUPositionCompletionRequestor fCompletionRequestor;
64
65     /**
66      * Creates a <code>CUPositionCompletionProcessor</code>.
67      * The completion context must be set via {@link #setCompletionContext(ICompilationUnit,String,String)}.
68      * @param completionRequestor the completion requestor
69      */

70     public CUPositionCompletionProcessor(CUPositionCompletionRequestor completionRequestor) {
71         fCompletionRequestor= completionRequestor;
72         
73         fComparator= new CompletionProposalComparator();
74         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
75         String JavaDoc triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
76         fProposalAutoActivationSet = triggers.toCharArray();
77     }
78
79     /**
80      * @param cuHandle the {@link ICompilationUnit} in whose context codeComplete will be invoked.
81      * The given cu doesn't have to exist (and if it exists, it will not be modified).
82      * An independent working copy consisting of
83      * <code>beforeString</code> + ${current_input} + <code>afterString</code> will be used.
84      * @param beforeString the string before the input position
85      * @param afterString the string after the input position
86      */

87     public void setCompletionContext(final ICompilationUnit cuHandle, final String JavaDoc beforeString, final String JavaDoc afterString) {
88         fCompletionContextRequestor= new CompletionContextRequestor() {
89             final StubTypeContext fStubTypeContext= new StubTypeContext(cuHandle, beforeString, afterString);
90             public StubTypeContext getStubTypeContext() {
91                 return fStubTypeContext;
92             }
93         };
94         if (cuHandle != null)
95             fCompletionRequestor.setJavaProject(cuHandle.getJavaProject());
96     }
97     
98     public void setCompletionContextRequestor(CompletionContextRequestor completionContextRequestor) {
99         fCompletionContextRequestor= completionContextRequestor;
100     }
101
102     /**
103      * Computing proposals on a <code>ITextViewer</code> is not supported.
104      * @see #computeCompletionProposals(IContentAssistSubjectControl, int)
105      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
106      */

107     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
108         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
109
return null;
110     }
111     
112     /**
113      * Computing context information on a <code>ITextViewer</code> is not supported.
114      * @see #computeContextInformation(IContentAssistSubjectControl, int)
115      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
116      */

117     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
118         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
119
return null;
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
124      */

125     public char[] getCompletionProposalAutoActivationCharacters() {
126         return fProposalAutoActivationSet;
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
131      */

132     public char[] getContextInformationAutoActivationCharacters() {
133         return null;
134     }
135
136     /* (non-Javadoc)
137      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
138      */

139     public String JavaDoc getErrorMessage() {
140         return fErrorMessage;
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
145      */

146     public IContextInformationValidator getContextInformationValidator() {
147         return null; //no context
148
}
149
150     /*
151      * @see ISubjectControlContentAssistProcessor#computeContextInformation(IContentAssistSubjectControl, int)
152      */

153     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
154         return null;
155     }
156
157     /*
158      * @see ISubjectControlContentAssistProcessor#computeCompletionProposals(IContentAssistSubjectControl, int)
159      */

160     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
161         if (fCompletionContextRequestor.getOriginalCu() == null)
162             return null;
163         String JavaDoc input= contentAssistSubjectControl.getDocument().get();
164         if (documentOffset == 0)
165             return null;
166         ICompletionProposal[] proposals= internalComputeCompletionProposals(documentOffset, input);
167         Arrays.sort(proposals, fComparator);
168         return proposals;
169     }
170
171     private ICompletionProposal[] internalComputeCompletionProposals(int documentOffset, String JavaDoc input) {
172         String JavaDoc cuString= fCompletionContextRequestor.getBeforeString() + input + fCompletionContextRequestor.getAfterString();
173         ICompilationUnit cu= null;
174         try {
175             /*
176              * Explicitly create a new non-shared working copy.
177              *
178              * The WorkingCopy cannot easily be shared between calls, since IContentAssistProcessor
179              * has no dispose() lifecycle method. A workaround could be to pass in a WorkingCopyOwner
180              * and dispose the owner's working copies from the caller's dispose().
181              */

182             cu= fCompletionContextRequestor.getOriginalCu().getWorkingCopy(new WorkingCopyOwner() {/*subclass*/}, null, new NullProgressMonitor());
183             cu.getBuffer().setContents(cuString);
184             int cuPrefixLength= fCompletionContextRequestor.getBeforeString().length();
185             fCompletionRequestor.setOffsetReduction(cuPrefixLength);
186             cu.codeComplete(cuPrefixLength + documentOffset, fCompletionRequestor);
187             
188             JavaCompletionProposal[] proposals= fCompletionRequestor.getResults();
189             if (proposals.length == 0) {
190                 String JavaDoc errorMsg= fCompletionRequestor.getErrorMessage();
191                 if (errorMsg == null || errorMsg.trim().length() == 0)
192                     errorMsg= RefactoringMessages.JavaTypeCompletionProcessor_no_completion;
193                 fErrorMessage= errorMsg;
194             } else {
195                 fErrorMessage= fCompletionRequestor.getErrorMessage();
196             }
197             return proposals;
198         } catch (JavaModelException e) {
199             JavaPlugin.log(e);
200             return null;
201         } finally {
202             try {
203                 if (cu != null)
204                     cu.discardWorkingCopy();
205             } catch (JavaModelException e) {
206                 JavaPlugin.log(e);
207             }
208         }
209     }
210
211     protected abstract static class CUPositionCompletionRequestor extends CompletionRequestor {
212         public static final char[] TRIGGER_CHARACTERS= new char[] { '.' };
213         
214         private int fOffsetReduction;
215         private IJavaProject fJavaProject;
216         
217         private List JavaDoc fProposals;
218         private String JavaDoc fErrorMessage2;
219
220         public IJavaProject getJavaProject() {
221             return fJavaProject;
222         }
223         
224         private void setJavaProject(IJavaProject javaProject) {
225             fJavaProject= javaProject;
226         }
227         
228         private void setOffsetReduction(int offsetReduction) {
229             fOffsetReduction= offsetReduction;
230             fProposals= new ArrayList JavaDoc();
231         }
232         
233         public final void completionFailure(IProblem error) {
234             fErrorMessage2= error.getMessage();
235         }
236
237         public final JavaCompletionProposal[] getResults() {
238             return (JavaCompletionProposal[]) fProposals.toArray(new JavaCompletionProposal[fProposals.size()]);
239         }
240         
241         public final String JavaDoc getErrorMessage() {
242             return fErrorMessage2;
243         }
244         
245         protected final void addAdjustedCompletion(String JavaDoc name, String JavaDoc completion,
246                 int start, int end, int relevance, ImageDescriptor descriptor) {
247             JavaCompletionProposal javaCompletionProposal= new JavaCompletionProposal(completion, start - fOffsetReduction, end - start,
248                     getImage(descriptor), name, relevance);
249             javaCompletionProposal.setTriggerCharacters(TRIGGER_CHARACTERS);
250             fProposals.add(javaCompletionProposal);
251         }
252         
253         protected final void addAdjustedTypeCompletion(String JavaDoc name, String JavaDoc completion,
254                 int start, int end, int relevance, ImageDescriptor descriptor, String JavaDoc fullyQualifiedName) {
255             JavaTypeCompletionProposal javaCompletionProposal= new JavaTypeCompletionProposal(
256                     fullyQualifiedName == null ? completion : fullyQualifiedName, null,
257                     fullyQualifiedName == null ? start - fOffsetReduction : 0,
258                     end - start, getImage(descriptor), name, relevance, completion);
259             javaCompletionProposal.setTriggerCharacters(TRIGGER_CHARACTERS);
260             fProposals.add(javaCompletionProposal);
261         }
262
263         private static Image getImage(ImageDescriptor descriptor) {
264             return (descriptor == null) ? null : CUPositionCompletionProcessor.IMAGE_DESC_REGISTRY.get(descriptor);
265         }
266     }
267 }
268
Popular Tags