KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.java;
13
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.graphics.Image;
18
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.contentassist.IContextInformation;
21
22 import org.eclipse.jdt.core.CompletionProposal;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.Signature;
27 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
28
29 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32
33
34 public class LazyJavaCompletionProposal extends AbstractJavaCompletionProposal {
35     
36     protected static final String JavaDoc LPAREN= "("; //$NON-NLS-1$
37
protected static final String JavaDoc RPAREN= ")"; //$NON-NLS-1$
38
protected static final String JavaDoc COMMA= ","; //$NON-NLS-1$
39
protected static final String JavaDoc SPACE= " "; //$NON-NLS-1$
40

41     protected static final class FormatterPrefs {
42         /* Methods & constructors */
43         public final boolean beforeOpeningParen;
44         public final boolean afterOpeningParen;
45         public final boolean beforeComma;
46         public final boolean afterComma;
47         public final boolean beforeClosingParen;
48         public final boolean inEmptyList;
49         
50         /* type parameters */
51         public final boolean beforeOpeningBracket;
52         public final boolean afterOpeningBracket;
53         public final boolean beforeTypeArgumentComma;
54         public final boolean afterTypeArgumentComma;
55         public final boolean beforeClosingBracket;
56     
57         FormatterPrefs(IJavaProject project) {
58             beforeOpeningParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_METHOD_INVOCATION, false);
59             afterOpeningParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_PAREN_IN_METHOD_INVOCATION, false);
60             beforeComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_METHOD_INVOCATION_ARGUMENTS, false);
61             afterComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_METHOD_INVOCATION_ARGUMENTS, true);
62             beforeClosingParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_PAREN_IN_METHOD_INVOCATION, false);
63             inEmptyList= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BETWEEN_EMPTY_PARENS_IN_METHOD_INVOCATION, false);
64             
65             beforeOpeningBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false);
66             afterOpeningBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false);
67             beforeTypeArgumentComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, false);
68             afterTypeArgumentComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, true);
69             beforeClosingBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false);
70         }
71     
72         protected final boolean getCoreOption(IJavaProject project, String JavaDoc key, boolean def) {
73             String JavaDoc option= getCoreOption(project, key);
74             if (JavaCore.INSERT.equals(option))
75                 return true;
76             if (JavaCore.DO_NOT_INSERT.equals(option))
77                 return false;
78             return def;
79         }
80     
81         protected final String JavaDoc getCoreOption(IJavaProject project, String JavaDoc key) {
82             if (project == null)
83                 return JavaCore.getOption(key);
84             return project.getOption(key, true);
85         }
86     }
87
88     private boolean fDisplayStringComputed;
89     private boolean fReplacementStringComputed;
90     private boolean fReplacementOffsetComputed;
91     private boolean fReplacementLengthComputed;
92     private boolean fCursorPositionComputed;
93     private boolean fImageComputed;
94     private boolean fContextInformationComputed;
95     private boolean fProposalInfoComputed;
96     private boolean fTriggerCharactersComputed;
97     private boolean fSortStringComputed;
98     private boolean fRelevanceComputed;
99     private FormatterPrefs fFormatterPrefs;
100
101     /**
102      * The core proposal wrapped by this completion proposal.
103      */

104     protected final CompletionProposal fProposal;
105     protected int fContextInformationPosition;
106     
107     public LazyJavaCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) {
108         super(context);
109         Assert.isNotNull(proposal);
110         Assert.isNotNull(context);
111         Assert.isNotNull(context.getCoreContext());
112         fProposal= proposal;
113     }
114
115     /*
116      * @see ICompletionProposalExtension#getTriggerCharacters()
117      */

118     public final char[] getTriggerCharacters() {
119         if (!fTriggerCharactersComputed)
120             setTriggerCharacters(computeTriggerCharacters());
121         return super.getTriggerCharacters();
122     }
123     
124     protected char[] computeTriggerCharacters() {
125         return new char[0];
126     }
127
128     /**
129      * Sets the trigger characters.
130      * @param triggerCharacters The set of characters which can trigger the application of this completion proposal
131      */

132     public final void setTriggerCharacters(char[] triggerCharacters) {
133         fTriggerCharactersComputed= true;
134         super.setTriggerCharacters(triggerCharacters);
135     }
136
137     /**
138      * Sets the proposal info.
139      * @param proposalInfo The additional information associated with this proposal or <code>null</code>
140      */

141     public final void setProposalInfo(ProposalInfo proposalInfo) {
142         fProposalInfoComputed= true;
143         super.setProposalInfo(proposalInfo);
144     }
145
146     /**
147      * Returns the additional proposal info, or <code>null</code> if none
148      * exists.
149      *
150      * @return the additional proposal info, or <code>null</code> if none
151      * exists
152      */

153     protected final ProposalInfo getProposalInfo() {
154         if (!fProposalInfoComputed)
155             setProposalInfo(computeProposalInfo());
156         return super.getProposalInfo();
157     }
158
159     protected ProposalInfo computeProposalInfo() {
160         return null;
161     }
162
163     /**
164      * Sets the cursor position relative to the insertion offset. By default this is the length of the completion string
165      * (Cursor positioned after the completion)
166      * @param cursorPosition The cursorPosition to set
167      */

168     public final void setCursorPosition(int cursorPosition) {
169         fCursorPositionComputed= true;
170         super.setCursorPosition(cursorPosition);
171     }
172     
173     protected final int getCursorPosition() {
174         if (!fCursorPositionComputed)
175             setCursorPosition(computeCursorPosition());
176         return super.getCursorPosition();
177     }
178
179     protected int computeCursorPosition() {
180         return getReplacementString().length();
181     }
182
183     /*
184      * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isInJavadoc()
185      */

186     protected final boolean isInJavadoc() {
187         return fInvocationContext.getCoreContext().isInJavadoc();
188     }
189
190     /*
191      * @see ICompletionProposal#getContextInformation()
192      */

193     public final IContextInformation getContextInformation() {
194         if (!fContextInformationComputed)
195             setContextInformation(computeContextInformation());
196         return super.getContextInformation();
197     }
198
199     protected IContextInformation computeContextInformation() {
200         return null;
201     }
202
203     /**
204      * Sets the context information.
205      * @param contextInformation The context information associated with this proposal
206      */

207     public final void setContextInformation(IContextInformation contextInformation) {
208         fContextInformationComputed= true;
209         super.setContextInformation(contextInformation);
210     }
211     
212     /*
213      * @see ICompletionProposal#getDisplayString()
214      */

215     public final String JavaDoc getDisplayString() {
216         if (!fDisplayStringComputed)
217             setDisplayString(computeDisplayString());
218         return super.getDisplayString();
219     }
220
221     protected final void setDisplayString(String JavaDoc string) {
222         fDisplayStringComputed= true;
223         super.setDisplayString(string);
224     }
225
226     protected String JavaDoc computeDisplayString() {
227         return fInvocationContext.getLabelProvider().createLabel(fProposal);
228     }
229
230     /*
231      * @see ICompletionProposal#getAdditionalProposalInfo()
232      */

233     public final String JavaDoc getAdditionalProposalInfo() {
234         return super.getAdditionalProposalInfo();
235     }
236
237     /*
238      * @see ICompletionProposalExtension#getContextInformationPosition()
239      */

240     public final int getContextInformationPosition() {
241         if (getContextInformation() == null)
242             return getReplacementOffset() - 1;
243         return getReplacementOffset() + getCursorPosition();
244     }
245
246     /**
247      * Gets the replacement offset.
248      * @return Returns a int
249      */

250     public final int getReplacementOffset() {
251         if (!fReplacementOffsetComputed)
252             setReplacementOffset(fProposal.getReplaceStart());
253         return super.getReplacementOffset();
254     }
255
256     /**
257      * Sets the replacement offset.
258      * @param replacementOffset The replacement offset to set
259      */

260     public final void setReplacementOffset(int replacementOffset) {
261         fReplacementOffsetComputed= true;
262         super.setReplacementOffset(replacementOffset);
263     }
264
265     /*
266      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getCompletionOffset()
267      */

268     public final int getPrefixCompletionStart(IDocument document, int completionOffset) {
269         return getReplacementOffset();
270     }
271
272     /**
273      * Gets the replacement length.
274      * @return Returns a int
275      */

276     public final int getReplacementLength() {
277         if (!fReplacementLengthComputed)
278             setReplacementLength(fProposal.getReplaceEnd() - fProposal.getReplaceStart());
279         return super.getReplacementLength();
280     }
281
282     /**
283      * Sets the replacement length.
284      * @param replacementLength The replacementLength to set
285      */

286     public final void setReplacementLength(int replacementLength) {
287         fReplacementLengthComputed= true;
288         super.setReplacementLength(replacementLength);
289     }
290
291     /**
292      * Gets the replacement string.
293      * @return Returns a String
294      */

295     public final String JavaDoc getReplacementString() {
296         if (!fReplacementStringComputed)
297             setReplacementString(computeReplacementString());
298         return super.getReplacementString();
299     }
300
301     protected String JavaDoc computeReplacementString() {
302         return String.valueOf(fProposal.getCompletion());
303     }
304
305     /**
306      * Sets the replacement string.
307      * @param replacementString The replacement string to set
308      */

309     public final void setReplacementString(String JavaDoc replacementString) {
310         fReplacementStringComputed= true;
311         super.setReplacementString(replacementString);
312     }
313
314     /*
315      * @see ICompletionProposal#getImage()
316      */

317     public final Image getImage() {
318         if (!fImageComputed)
319             setImage(computeImage());
320         return super.getImage();
321     }
322
323     protected Image computeImage() {
324         return JavaPlugin.getImageDescriptorRegistry().get(fInvocationContext.getLabelProvider().createImageDescriptor(fProposal));
325     }
326
327     /**
328      * Sets the image.
329      * @param image The image to set
330      */

331     public final void setImage(Image image) {
332         fImageComputed= true;
333         super.setImage(image);
334     }
335
336     /*
337      * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String)
338      */

339     protected boolean isValidPrefix(String JavaDoc prefix) {
340         if (super.isValidPrefix(prefix))
341             return true;
342         
343         if (fProposal.getKind() == CompletionProposal.METHOD_NAME_REFERENCE) {
344             // static imports - includes package & type name
345
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
346             buf.append(Signature.toCharArray(fProposal.getDeclarationSignature()));
347             buf.append('.');
348             buf.append(getDisplayString());
349             return isPrefix(prefix, buf.toString());
350         }
351         
352         return false;
353     }
354     
355     /**
356      * Gets the proposal's relevance.
357      * @return Returns a int
358      */

359     public final int getRelevance() {
360         if (!fRelevanceComputed)
361             setRelevance(computeRelevance());
362         return super.getRelevance();
363     }
364
365     /**
366      * Sets the proposal's relevance.
367      * @param relevance The relevance to set
368      */

369     public final void setRelevance(int relevance) {
370         fRelevanceComputed= true;
371         super.setRelevance(relevance);
372     }
373
374     protected int computeRelevance() {
375         final int baseRelevance= fProposal.getRelevance() * 16;
376         switch (fProposal.getKind()) {
377             case CompletionProposal.PACKAGE_REF:
378                 return baseRelevance + 0;
379             case CompletionProposal.LABEL_REF:
380                 return baseRelevance + 1;
381             case CompletionProposal.KEYWORD:
382                 return baseRelevance + 2;
383             case CompletionProposal.TYPE_REF:
384             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
385                 return baseRelevance + 3;
386             case CompletionProposal.METHOD_REF:
387             case CompletionProposal.METHOD_NAME_REFERENCE:
388             case CompletionProposal.METHOD_DECLARATION:
389             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
390                 return baseRelevance + 4;
391             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
392                 return baseRelevance + 4 /* + 99 */;
393             case CompletionProposal.FIELD_REF:
394                 return baseRelevance + 5;
395             case CompletionProposal.LOCAL_VARIABLE_REF:
396             case CompletionProposal.VARIABLE_DECLARATION:
397                 return baseRelevance + 6;
398             default:
399                 return baseRelevance;
400         }
401     }
402     
403     public final String JavaDoc getSortString() {
404         if (!fSortStringComputed)
405             setSortString(computeSortString());
406         return super.getSortString();
407     }
408
409     protected final void setSortString(String JavaDoc string) {
410         fSortStringComputed= true;
411         super.setSortString(string);
412     }
413
414     protected String JavaDoc computeSortString() {
415         return getDisplayString();
416     }
417
418     protected FormatterPrefs getFormatterPrefs() {
419         if (fFormatterPrefs == null) {
420             ICompilationUnit cu= fInvocationContext.getCompilationUnit();
421             fFormatterPrefs= new FormatterPrefs(cu == null ? null : cu.getJavaProject());
422         }
423         return fFormatterPrefs;
424     }
425
426     /**
427      * Overrides the default context information position. Ignored if set to zero.
428      *
429      * @param contextInformationPosition the replaced position.
430      */

431     public void setContextInformationPosition(int contextInformationPosition) {
432         fContextInformationPosition= contextInformationPosition;
433     }
434 }
435
Popular Tags