KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jdt.internal.ui.text.java;
12
13   
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.swt.graphics.Image;
21
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.text.ITextViewer;
24
25 import org.eclipse.jdt.core.CompletionRequestorAdapter;
26 import org.eclipse.jdt.core.Flags;
27 import org.eclipse.jdt.core.ICompilationUnit;
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IType;
31 import org.eclipse.jdt.core.Signature;
32 import org.eclipse.jdt.core.compiler.IProblem;
33
34 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
35
36 import org.eclipse.jdt.internal.codeassist.IExtendedCompletionRequestor;
37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
38 import org.eclipse.jdt.internal.corext.util.TypeFilter;
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.JavaPluginImages;
41 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
42 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
43
44 /**
45  * Bin to collect the proposal of the infrastructure on code assist in a java text.
46  */

47 public class ResultCollector extends CompletionRequestorAdapter implements IExtendedCompletionRequestor {
48         
49     private final static char[] METHOD_WITH_ARGUMENTS_TRIGGERS= new char[] { '(', '-', ' ' };
50     private final static char[] METHOD_TRIGGERS= new char[] { ';', ',', '.', '\t', '[', ' ' };
51     private final static char[] TYPE_TRIGGERS= new char[] { '.', '\t', '[', '(', ' ' };
52     private final static char[] VAR_TRIGGER= new char[] { '\t', ' ', '=', ';', '.' };
53
54     protected IJavaProject fJavaProject;
55     protected ICompilationUnit fCompilationUnit; // set when imports can be added
56
protected int fCodeAssistOffset;
57     protected int fContextOffset;
58     protected ITextViewer fTextViewer;
59     
60     private ArrayList JavaDoc fFields=
61         new ArrayList JavaDoc(),
62         fKeywords= new ArrayList JavaDoc(10),
63         fLabels= new ArrayList JavaDoc(10),
64         fMethods= new ArrayList JavaDoc(),
65         fModifiers= new ArrayList JavaDoc(10),
66         fPackages= new ArrayList JavaDoc(),
67         fTypes= new ArrayList JavaDoc(),
68         fVariables= new ArrayList JavaDoc();
69
70     private IProblem fLastProblem;
71     private ImageDescriptorRegistry fRegistry= JavaPlugin.getImageDescriptorRegistry();
72     
73     private ArrayList JavaDoc[] fResults = new ArrayList JavaDoc[] {
74         fPackages, fLabels, fModifiers, fKeywords, fTypes, fMethods, fFields, fVariables
75     };
76     
77     private Set JavaDoc fSuggestedMethodNames= new HashSet JavaDoc();
78     
79     private int fUserReplacementLength;
80
81     /*
82      * Is eating code assist enabled or disabled? PR #3666
83      * When eating is enabled, JavaCompletionProposal must be revisited: PR #5533
84      */

85     private boolean fPreventEating= true;
86     
87     /*
88      * @see ICompletionRequestor#acceptClass
89      */

90     public void acceptClass(char[] packageName, char[] typeName, char[] completionName, int modifiers, int start, int end, int relevance) {
91         if (TypeFilter.isFiltered(packageName)) {
92             return;
93         }
94         
95         ImageDescriptor descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, false, modifiers);
96         if (Flags.isDeprecated(modifiers))
97             descriptor= getDeprecatedDescriptor(descriptor);
98
99         ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName);
100         fTypes.add(createTypeCompletion(start, end, new String JavaDoc(completionName), descriptor, new String JavaDoc(typeName), new String JavaDoc(packageName), info, relevance));
101     }
102     
103     /*
104      * @see ICompletionRequestor#acceptError
105      */

106     public void acceptError(IProblem error) {
107         fLastProblem= error;
108     }
109     
110     /*
111      * @see ICompletionRequestor#acceptField
112      */

113     public void acceptField(
114         char[] declaringTypePackageName, char[] declaringTypeName, char[] name,
115         char[] typePackageName, char[] typeName, char[] completionName,
116         int modifiers, int start, int end, int relevance) {
117         
118         if (TypeFilter.isFiltered(declaringTypePackageName)) {
119             return;
120         }
121         
122
123         ImageDescriptor descriptor= getFieldDescriptor(modifiers);
124         
125         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
126         nameBuffer.append(name);
127         if (typeName.length > 0) {
128             nameBuffer.append(" "); //$NON-NLS-1$
129
nameBuffer.append(typeName);
130         }
131         if (declaringTypeName != null && declaringTypeName.length > 0) {
132             nameBuffer.append(" - "); //$NON-NLS-1$
133
nameBuffer.append(declaringTypeName);
134         }
135         
136         JavaCompletionProposal proposal= createCompletion(start, end, new String JavaDoc(completionName), descriptor, nameBuffer.toString(), relevance);
137         proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name));
138         proposal.setTriggerCharacters(VAR_TRIGGER);
139         
140         fFields.add(proposal);
141     }
142     
143     /*
144      * @see ICompletionRequestor#acceptInterface
145      */

146     public void acceptInterface(char[] packageName, char[] typeName, char[] completionName, int modifiers, int start, int end, int relevance) {
147         if (TypeFilter.isFiltered(packageName)) {
148             return;
149         }
150         
151         ImageDescriptor descriptor= JavaElementImageProvider.getTypeImageDescriptor(true, false, false, modifiers);
152         if (Flags.isDeprecated(modifiers))
153             descriptor= getDeprecatedDescriptor(descriptor);
154
155         ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName);
156         fTypes.add(createTypeCompletion(start, end, new String JavaDoc(completionName), descriptor, new String JavaDoc(typeName), new String JavaDoc(packageName), info, relevance));
157     }
158     
159     /*
160      * @see ICompletionRequestor#acceptAnonymousType
161      */

162     public void acceptAnonymousType(char[] superTypePackageName, char[] superTypeName, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames,
163             char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
164
165         if (TypeFilter.isFiltered(superTypePackageName)) {
166             return;
167         }
168         
169         JavaCompletionProposal proposal= createAnonymousTypeCompletion(superTypePackageName, superTypeName, parameterTypeNames, parameterNames, completionName, completionStart, completionEnd, relevance);
170         proposal.setProposalInfo(new ProposalInfo(fJavaProject, superTypePackageName, superTypeName));
171         fTypes.add(proposal);
172     }
173     
174     /*
175      * @see ICompletionRequestor#acceptKeyword
176      */

177     public void acceptKeyword(char[] keyword, int start, int end, int relevance) {
178         String JavaDoc kw= new String JavaDoc(keyword);
179         fKeywords.add(createCompletion(start, end, kw, null, kw, relevance));
180     }
181     
182     /*
183      * @see ICompletionRequestor#acceptLabel
184      */

185     public void acceptLabel(char[] labelName, int start, int end, int relevance) {
186         String JavaDoc ln= new String JavaDoc(labelName);
187         fLabels.add(createCompletion(start, end, ln, null, ln, relevance));
188     }
189     
190     /*
191      * @see ICompletionRequestor#acceptLocalVariable
192      */

193     public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName, int modifiers, int start, int end, int relevance) {
194         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
195         buf.append(name);
196         if (typeName != null) {
197             buf.append(" "); //$NON-NLS-1$
198
buf.append(typeName);
199         }
200         JavaCompletionProposal proposal= createCompletion(start, end, new String JavaDoc(name), JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE, buf.toString(), relevance);
201         proposal.setTriggerCharacters(VAR_TRIGGER);
202         fVariables.add(proposal);
203     }
204     
205     protected String JavaDoc getParameterSignature(char[][] parameterTypeNames, char[][] parameterNames) {
206         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
207         if (parameterTypeNames != null) {
208             for (int i = 0; i < parameterTypeNames.length; i++) {
209                 if (i > 0) {
210                     buf.append(',');
211                     buf.append(' ');
212                 }
213                 buf.append(parameterTypeNames[i]);
214                 if (parameterNames != null && parameterNames[i] != null) {
215                     buf.append(' ');
216                     buf.append(parameterNames[i]);
217                 }
218             }
219         }
220         return buf.toString();
221     }
222     
223     /*
224      * @see ICompletionRequestor#acceptMethod
225      */

226     public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, char[] name,
227         char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames,
228         char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers,
229         int start, int end, int relevance) {
230         
231         if (completionName == null)
232             return;
233         
234         if (TypeFilter.isFiltered(declaringTypePackageName)) {
235             return;
236         }
237     
238         JavaCompletionProposal proposal= createMethodCallCompletion(declaringTypeName, name, parameterPackageNames, parameterTypeNames, parameterNames, returnTypeName, completionName, modifiers, start, end, relevance);
239         boolean isConstructor= returnTypeName == null ? true : returnTypeName.length == 0;
240         proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name, parameterPackageNames, parameterTypeNames, isConstructor));
241
242         boolean hasOpeningBracket= completionName.length == 0 || (completionName.length > 0 && completionName[completionName.length - 1] == ')');
243     
244         ProposalContextInformation contextInformation= null;
245         if (hasOpeningBracket && parameterTypeNames.length > 0) {
246             contextInformation= new ProposalContextInformation();
247             contextInformation.setInformationDisplayString(getParameterSignature(parameterTypeNames, parameterNames));
248             contextInformation.setContextDisplayString(proposal.getDisplayString());
249             contextInformation.setImage(proposal.getImage());
250             int position= (completionName.length == 0) ? fContextOffset : -1;
251             contextInformation.setContextInformationPosition(position);
252             proposal.setContextInformation(contextInformation);
253         }
254     
255         boolean userMustCompleteParameters= (contextInformation != null && completionName.length > 0);
256         char[] triggers= userMustCompleteParameters ? METHOD_WITH_ARGUMENTS_TRIGGERS : METHOD_TRIGGERS;
257         proposal.setTriggerCharacters(triggers);
258         
259         if (userMustCompleteParameters) {
260             // set the cursor before the closing bracket
261
proposal.setCursorPosition(completionName.length - 1);
262         }
263         
264         fMethods.add(proposal);
265     }
266
267     
268     /*
269      * @see ICompletionRequestor#acceptModifier
270      */

271     public void acceptModifier(char[] modifier, int start, int end, int relevance) {
272         String JavaDoc mod= new String JavaDoc(modifier);
273         fModifiers.add(createCompletion(start, end, mod, null, mod, relevance));
274     }
275     
276     /*
277      * @see ICompletionRequestor#acceptPackage
278      */

279     public void acceptPackage(char[] packageName, char[] completionName, int start, int end, int relevance) {
280         if (TypeFilter.isFiltered(packageName)) {
281             return;
282         }
283         
284         fPackages.add(createCompletion(start, end, new String JavaDoc(completionName), JavaPluginImages.DESC_OBJS_PACKAGE, new String JavaDoc(packageName), relevance));
285     }
286     
287     /*
288      * @see ICompletionRequestor#acceptType
289      */

290     public void acceptType(char[] packageName, char[] typeName, char[] completionName, int start, int end, int relevance) {
291         if (TypeFilter.isFiltered(packageName)) {
292             return;
293         }
294         
295         ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName);
296         fTypes.add(createTypeCompletion(start, end, new String JavaDoc(completionName), JavaPluginImages.DESC_OBJS_CLASS, new String JavaDoc(typeName), new String JavaDoc(packageName), info, relevance));
297     }
298     
299     /*
300      * @see ICodeCompletionRequestor#acceptMethodDeclaration
301      */

302     public void acceptMethodDeclaration(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers, int start, int end, int relevance) {
303         StringBuffer JavaDoc displayString= getMethodDisplayString(null, name, parameterTypeNames, parameterNames, returnTypeName);
304         displayString.append(" - "); //$NON-NLS-1$
305
displayString.append(JavaTextMessages.getFormattedString("ResultCollector.overridingmethod", new String JavaDoc(declaringTypeName))); //$NON-NLS-1$
306

307         StringBuffer JavaDoc typeName= new StringBuffer JavaDoc();
308         if (declaringTypePackageName.length > 0) {
309             typeName.append(declaringTypePackageName);
310             typeName.append('.');
311         }
312         typeName.append(declaringTypeName);
313         
314         String JavaDoc[] paramTypes= new String JavaDoc[parameterTypeNames.length];
315         for (int i= 0; i < parameterTypeNames.length; i++) {
316             paramTypes[i]= Signature.createTypeSignature(parameterTypeNames[i], true);
317         }
318
319         JavaCompletionProposal proposal= new OverrideCompletionProposal(fJavaProject, fCompilationUnit, typeName.toString(), new String JavaDoc(name), paramTypes, start, getLength(start, end), displayString.toString(), new String JavaDoc(completionName));
320         proposal.setImage(getImage(getMemberDescriptor(modifiers)));
321         proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name, parameterPackageNames, parameterTypeNames, returnTypeName.length == 0));
322         proposal.setRelevance(relevance + 100);
323         fMethods.add(proposal);
324         
325         fSuggestedMethodNames.add(new String JavaDoc(name));
326         
327     }
328     
329     /*
330      * @see IExtendedCompletionRequestor#acceptPotentialMethodDeclaration
331      */

332     public void acceptPotentialMethodDeclaration(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, int completionStart, int completionEnd, int relevance) {
333         if (fCompilationUnit == null) {
334             return;
335         }
336         String JavaDoc prefix= new String JavaDoc(selector);
337     
338         try {
339             IJavaElement element= fCompilationUnit.getElementAt(fCodeAssistOffset);
340             if (element != null) {
341                 IType type= (IType) element.getAncestor(IJavaElement.TYPE);
342                 if (type != null) {
343                     GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 100, fSuggestedMethodNames, fMethods);
344                     MethodCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 99, fSuggestedMethodNames, fMethods);
345                 }
346             }
347         } catch (CoreException e) {
348             JavaPlugin.log(e);
349         }
350     }
351     
352     /*
353      * @see ICodeCompletionRequestor#acceptVariableName
354      */

355     public void acceptVariableName(char[] typePackageName, char[] typeName, char[] name, char[] completionName, int start, int end, int relevance) {
356         // XXX: To be revised
357
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
358         buf.append(name);
359         if (typeName != null && typeName.length > 0) {
360             buf.append(" - "); //$NON-NLS-1$
361
buf.append(typeName);
362         }
363         JavaCompletionProposal proposal= createCompletion(start, end, new String JavaDoc(completionName), null, buf.toString(), relevance);
364         proposal.setTriggerCharacters(VAR_TRIGGER);
365         fVariables.add(proposal);
366     }
367     
368     public String JavaDoc getErrorMessage() {
369         if (fLastProblem != null)
370             return fLastProblem.getMessage();
371         return ""; //$NON-NLS-1$
372
}
373
374     public JavaCompletionProposal[] getResults() {
375         // return unsorted
376
int totLen= 0;
377         for (int i= 0; i < fResults.length; i++) {
378             totLen += fResults[i].size();
379         }
380         JavaCompletionProposal[] result= new JavaCompletionProposal[totLen];
381         int k= 0;
382         for (int i= 0; i < fResults.length; i++) {
383             ArrayList JavaDoc curr= fResults[i];
384             int currLen= curr.size();
385             for (int j= 0; j < currLen; j++) {
386                 JavaCompletionProposal proposal= (JavaCompletionProposal) curr.get(j);
387                 // for equal relevance, take categories
388
proposal.setRelevance(proposal.getRelevance() * 16 + i);
389                 result[k++]= proposal;
390             }
391         }
392         return result;
393     }
394     
395     public JavaCompletionProposal[] getKeywordCompletions() {
396         return (JavaCompletionProposal[]) fKeywords.toArray(new JavaCompletionProposal[fKeywords.size()]);
397     }
398
399     private StringBuffer JavaDoc getMethodDisplayString(char[] declaringTypeName, char[] name, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypeName) {
400         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
401         nameBuffer.append(name);
402         nameBuffer.append('(');
403         if (parameterTypeNames != null && parameterTypeNames.length > 0) {
404             nameBuffer.append(getParameterSignature(parameterTypeNames, parameterNames));
405         }
406         nameBuffer.append(')');
407         if (returnTypeName != null && returnTypeName.length > 0) {
408             nameBuffer.append(" "); //$NON-NLS-1$
409
nameBuffer.append(returnTypeName);
410         }
411         if (declaringTypeName != null && declaringTypeName.length > 0) {
412             nameBuffer.append(" - "); //$NON-NLS-1$
413
nameBuffer.append(declaringTypeName);
414         }
415         return nameBuffer;
416     }
417
418     protected JavaCompletionProposal createMethodCallCompletion(char[] declaringTypeName, char[] name, char[][] parameterTypePackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypeName, char[] completionName, int modifiers, int start, int end, int relevance) {
419         ImageDescriptor descriptor= getMemberDescriptor(modifiers);
420         StringBuffer JavaDoc nameBuffer= getMethodDisplayString(declaringTypeName, name, parameterTypeNames, parameterNames, returnTypeName);
421         return createCompletion(start, end, new String JavaDoc(completionName), descriptor, nameBuffer.toString(), relevance);
422     }
423
424
425     protected JavaCompletionProposal createAnonymousTypeCompletion(char[] declaringTypePackageName, char[] declaringTypeName, char[][] parameterTypeNames, char[][] parameterNames, char[] completionName, int start, int end, int relevance) {
426         StringBuffer JavaDoc declTypeBuf= new StringBuffer JavaDoc();
427         if (declaringTypePackageName.length > 0) {
428             declTypeBuf.append(declaringTypePackageName);
429             declTypeBuf.append('.');
430         }
431         declTypeBuf.append(declaringTypeName);
432         
433         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
434         nameBuffer.append(declaringTypeName);
435         nameBuffer.append('(');
436         if (parameterTypeNames.length > 0) {
437             nameBuffer.append(getParameterSignature(parameterTypeNames, parameterNames));
438         }
439         nameBuffer.append(')');
440         nameBuffer.append(" "); //$NON-NLS-1$
441
nameBuffer.append(JavaTextMessages.getString("ResultCollector.anonymous_type")); //$NON-NLS-1$
442

443         int length= end - start;
444         
445         return new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, start, length, new String JavaDoc(completionName), nameBuffer.toString(), declTypeBuf.toString(), relevance);
446     }
447
448     
449     protected JavaCompletionProposal createTypeCompletion(int start, int end, String JavaDoc completion, ImageDescriptor descriptor, String JavaDoc typeName, String JavaDoc containerName, ProposalInfo proposalInfo, int relevance) {
450         
451         String JavaDoc fullName= JavaModelUtil.concatenateName(containerName, typeName); // containername can be null
452

453         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(Signature.getSimpleName(fullName));
454         String JavaDoc typeQualifier= Signature.getQualifier(fullName);
455         if (typeQualifier.length() > 0) {
456             buf.append(" - "); //$NON-NLS-1$
457
buf.append(typeQualifier);
458         } else if (containerName != null) {
459             buf.append(JavaTextMessages.getString("ResultCollector.default_package")); //$NON-NLS-1$
460
}
461         String JavaDoc name= buf.toString();
462
463         ICompilationUnit cu= null;
464         if (containerName != null && fCompilationUnit != null) {
465             if (completion.equals(fullName)) {
466                 cu= fCompilationUnit;
467             }
468         }
469     
470         JavaCompletionProposal proposal= new JavaTypeCompletionProposal(completion, cu, start, getLength(start, end), getImage(descriptor), name, relevance, typeName, containerName);
471         proposal.setProposalInfo(proposalInfo);
472         proposal.setTriggerCharacters(TYPE_TRIGGERS);
473         return proposal;
474     }
475
476     protected ImageDescriptor getMemberDescriptor(int modifiers) {
477         ImageDescriptor desc= JavaElementImageProvider.getMethodImageDescriptor(false, modifiers);
478
479         if (Flags.isDeprecated(modifiers))
480             desc= getDeprecatedDescriptor(desc);
481
482         if (Flags.isStatic(modifiers))
483             desc= getStaticDescriptor(desc);
484         
485         return desc;
486     }
487     
488     protected ImageDescriptor getFieldDescriptor(int modifiers) {
489         ImageDescriptor desc= JavaElementImageProvider.getFieldImageDescriptor(false, modifiers);
490
491         if (Flags.isDeprecated(modifiers))
492             desc= getDeprecatedDescriptor(desc);
493             
494         if (Flags.isStatic(modifiers))
495             desc= getStaticDescriptor(desc);
496         
497         return desc;
498     }
499     
500     protected ImageDescriptor getDeprecatedDescriptor(ImageDescriptor descriptor) {
501         return new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.DEPRECATED, JavaElementImageProvider.SMALL_SIZE);
502     }
503     
504     protected ImageDescriptor getStaticDescriptor(ImageDescriptor descriptor) {
505         return new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
506     }
507     
508     protected JavaCompletionProposal createCompletion(int start, int end, String JavaDoc completion, ImageDescriptor descriptor, String JavaDoc name, int relevance) {
509         return new JavaCompletionProposal(completion, start, getLength(start, end), getImage(descriptor), name, relevance, fTextViewer);
510     }
511
512     private int getLength(int start, int end) {
513         int length;
514         if (fUserReplacementLength == -1) {
515             length= fPreventEating ? fCodeAssistOffset - start : end - start;
516         } else {
517             length= fUserReplacementLength;
518             // extend length to begin at start
519
if (start < fCodeAssistOffset) {
520                 length+= fCodeAssistOffset - start;
521             }
522         }
523         return length;
524     }
525     
526     private Image getImage(ImageDescriptor descriptor) {
527         return (descriptor == null) ? null : fRegistry.get(descriptor);
528     }
529     
530     /**
531      * Specifies the context of the code assist operation.
532      * @param codeAssistOffset The Offset at which the code assist will be called.
533      * Used to modify the offsets of the created proposals. ('Non Eating')
534      * @param contextOffset The offset at which the context presumable start or -1.
535      * @param jproject The Java project to which the underlying source belongs.
536      * Needed to find types referred.
537      * @param cu The compilation unit that is edited. Used to add import statements.
538      * Can be <code>null</code> if no import statements should be added.
539      */

540     public void reset(int codeAssistOffset, int contextOffset, IJavaProject jproject, ICompilationUnit cu) {
541         fJavaProject= jproject;
542         fCompilationUnit= cu;
543         fCodeAssistOffset= codeAssistOffset;
544         fContextOffset= contextOffset;
545         
546         fUserReplacementLength= -1;
547         
548         fLastProblem= null;
549         
550         for (int i= 0; i < fResults.length; i++)
551             fResults[i].clear();
552         
553         fSuggestedMethodNames.clear();
554     }
555
556     /**
557      * Specifies the context of the code assist operation.
558      * @param codeAssistOffset The Offset on which the code assist will be called.
559      * Used to modify the offsets of the created proposals. ('Non Eating')
560      * @param jproject The Java project to which the underlying source belongs.
561      * Needed to find types referred.
562      * @param cu The compilation unit that is edited. Used to add import statements.
563      * Can be <code>null</code> if no import statements should be added.
564      */

565     public void reset(int codeAssistOffset, IJavaProject jproject, ICompilationUnit cu) {
566         reset(codeAssistOffset, -1, jproject, cu);
567     }
568     
569     /**
570      * Sets the text viewer.
571      */

572     public void setViewer(ITextViewer viewer) {
573         fTextViewer= viewer;
574     }
575     
576     /**
577      * If the replacement length is set, it overrides the length returned from
578      * the content assist infrastructure.
579      * Use this setting if code assist is called with a none empty selection.
580      */

581     public void setReplacementLength(int length) {
582         fUserReplacementLength= length;
583     }
584
585     /**
586      * If set, proposals created will not remove characters after the code assist position
587      * @param preventEating The preventEating to set
588      */

589     public void setPreventEating(boolean preventEating) {
590         fPreventEating= preventEating;
591     }
592
593 }
594
Popular Tags