KickJava   Java API By Example, From Geeks To Geeks.

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


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 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  * Bin to collect the proposal of the infrastructure on code assist in a java text.
30  */

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

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

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

102     private final boolean shouldProposeGenerics(IJavaProject project) {
103         String JavaDoc sourceVersion;
104         if (project != null)
105             sourceVersion= project.getOption(JavaCore.COMPILER_SOURCE, true);
106         else
107             sourceVersion= JavaCore.getOption(JavaCore.COMPILER_SOURCE);
108
109         return sourceVersion != null && JavaCore.VERSION_1_5.compareTo(sourceVersion) <= 0;
110     }
111 }
112
Popular Tags