KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jface.preference.IPreferenceStore;
14
15 import org.eclipse.jdt.core.CompletionProposal;
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.JavaCore;
19 import org.eclipse.jdt.core.Signature;
20
21 import org.eclipse.jdt.ui.PreferenceConstants;
22 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector;
23 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
24 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27
28 /**
29  * Completion proposal collector which creates proposals with
30  * filled in argument names.
31  * <p>
32  * This collector is used when
33  * {@link PreferenceConstants#CODEASSIST_FILL_ARGUMENT_NAMES} is enabled.
34  * <p/>
35  */

36 public final class FillArgumentNamesCompletionProposalCollector extends CompletionProposalCollector {
37
38     private final boolean fIsGuessArguments;
39
40     public FillArgumentNamesCompletionProposalCollector(JavaContentAssistInvocationContext context) {
41         super(context.getCompilationUnit());
42         setInvocationContext(context);
43         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
44         fIsGuessArguments= preferenceStore.getBoolean(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS);
45     }
46
47     /*
48      * @see org.eclipse.jdt.internal.ui.text.java.ResultCollector#createJavaCompletionProposal(org.eclipse.jdt.core.CompletionProposal)
49      */

50     protected IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
51         switch (proposal.getKind()) {
52             case CompletionProposal.METHOD_REF:
53                 return createMethodReferenceProposal(proposal);
54             case CompletionProposal.TYPE_REF:
55                 return createTypeProposal(proposal);
56             default:
57                 return super.createJavaCompletionProposal(proposal);
58         }
59     }
60
61     private IJavaCompletionProposal createMethodReferenceProposal(CompletionProposal methodProposal) {
62         String JavaDoc completion= String.valueOf(methodProposal.getCompletion());
63         // super class' behavior if this is not a normal completion or has no
64
// parameters
65
if ((completion.length() == 0) || ((completion.length() == 1) && completion.charAt(0) == ')') || Signature.getParameterCount(methodProposal.getSignature()) == 0 || getContext().isInJavadoc())
66             return super.createJavaCompletionProposal(methodProposal);
67
68         LazyJavaCompletionProposal proposal;
69         ICompilationUnit compilationUnit= getCompilationUnit();
70         if (compilationUnit != null && fIsGuessArguments)
71             proposal= new ParameterGuessingProposal(methodProposal, getInvocationContext());
72         else
73             proposal= new FilledArgumentNamesMethodProposal(methodProposal, getInvocationContext());
74         return proposal;
75     }
76
77     /*
78      * @see org.eclipse.jdt.internal.ui.text.java.ResultCollector#createTypeCompletion(org.eclipse.jdt.core.CompletionProposal)
79      */

80     private IJavaCompletionProposal createTypeProposal(CompletionProposal typeProposal) {
81         final ICompilationUnit cu= getCompilationUnit();
82         if (cu == null || getContext().isInJavadoc())
83             return super.createJavaCompletionProposal(typeProposal);
84
85         IJavaProject project= cu.getJavaProject();
86         if (!shouldProposeGenerics(project))
87             return super.createJavaCompletionProposal(typeProposal);
88
89         char[] completion= typeProposal.getCompletion();
90         // don't add parameters for import-completions nor for proposals with an empty completion (e.g. inside the type argument list)
91
if (completion.length > 0 && (completion[completion.length - 1] == ';' || completion[completion.length - 1] == '.'))
92             return super.createJavaCompletionProposal(typeProposal);
93
94         LazyJavaCompletionProposal newProposal= new LazyGenericTypeProposal(typeProposal, getInvocationContext());
95         return newProposal;
96     }
97
98     /**
99      * Returns <code>true</code> if generic proposals should be allowed,
100      * <code>false</code> if not. Note that even though code (in a library)
101      * may be referenced that uses generics, it is still possible that the
102      * current source does not allow generics.
103      *
104      * @param project the Java project
105      * @return <code>true</code> if the generic proposals should be allowed,
106      * <code>false</code> if not
107      */

108     private final boolean shouldProposeGenerics(IJavaProject project) {
109         String JavaDoc sourceVersion;
110         if (project != null)
111             sourceVersion= project.getOption(JavaCore.COMPILER_SOURCE, true);
112         else
113             sourceVersion= JavaCore.getOption(JavaCore.COMPILER_SOURCE);
114
115         return sourceVersion != null && JavaCore.VERSION_1_5.compareTo(sourceVersion) <= 0;
116     }
117 }
118
Popular Tags