KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.jface.resource.ImageDescriptor;
20
21 import org.eclipse.jface.text.Assert;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.TextUtilities;
26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
27
28 import org.eclipse.jdt.core.IMethod;
29 import org.eclipse.jdt.core.IType;
30 import org.eclipse.jdt.core.JavaConventions;
31 import org.eclipse.jdt.core.Signature;
32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
33 import org.eclipse.jdt.core.formatter.CodeFormatter;
34
35 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
36 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
37 import org.eclipse.jdt.internal.corext.util.Strings;
38
39 import org.eclipse.jdt.ui.CodeGeneration;
40 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
41
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.JavaPluginImages;
44 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
45 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
46
47 public class MethodCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
48
49
50     public static void evaluateProposals(IType type, String JavaDoc prefix, int offset, int length, int relevance, Set JavaDoc suggestedMethods, Collection JavaDoc result) throws CoreException {
51         IMethod[] methods= type.getMethods();
52         if (!type.isInterface()) {
53             String JavaDoc constructorName= type.getElementName();
54             if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) {
55                 result.add(new MethodCompletionProposal(type, constructorName, null, offset, length, relevance + 500));
56             }
57         }
58
59         if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { //$NON-NLS-1$
60
if (!JavaConventions.validateMethodName(prefix).matches(IStatus.ERROR)) {
61                 result.add(new MethodCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance));
62             }
63         }
64     }
65
66     private static boolean hasMethod(IMethod[] methods, String JavaDoc name) {
67         for (int i= 0; i < methods.length; i++) {
68             IMethod curr= methods[i];
69             if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) {
70                 return true;
71             }
72         }
73         return false;
74     }
75
76     private final IType fType;
77     private final String JavaDoc fReturnTypeSig;
78     private final String JavaDoc fMethodName;
79
80     public MethodCompletionProposal(IType type, String JavaDoc methodName, String JavaDoc returnTypeSig, int start, int length, int relevance) {
81         super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); //$NON-NLS-1$
82
Assert.isNotNull(type);
83         Assert.isNotNull(methodName);
84
85         fType= type;
86         fMethodName= methodName;
87         fReturnTypeSig= returnTypeSig;
88
89         if (returnTypeSig == null) {
90             setProposalInfo(new ProposalInfo(type));
91
92             ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
93             setImage(JavaPlugin.getImageDescriptorRegistry().get(desc));
94         } else {
95             setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE));
96         }
97     }
98
99     private static String JavaDoc getDisplayName(String JavaDoc methodName, String JavaDoc returnTypeSig) {
100         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
101         buf.append(methodName);
102         buf.append('(');
103         buf.append(')');
104         if (returnTypeSig != null) {
105             buf.append(" "); //$NON-NLS-1$
106
buf.append(Signature.toString(returnTypeSig));
107             buf.append(" - "); //$NON-NLS-1$
108
buf.append(JavaTextMessages.MethodCompletionProposal_method_label);
109         } else {
110             buf.append(" - "); //$NON-NLS-1$
111
buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label);
112         }
113         return buf.toString();
114     }
115
116     /* (non-Javadoc)
117      * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
118      */

119     protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
120
121         CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
122         boolean addComments= settings.createComments;
123
124         String JavaDoc[] empty= new String JavaDoc[0];
125         String JavaDoc lineDelim= TextUtilities.getDefaultLineDelimiter(document);
126         String JavaDoc declTypeName= fType.getTypeQualifiedName('.');
127         boolean isInterface= fType.isInterface();
128
129         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
130         if (addComments) {
131             String JavaDoc comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
132             if (comment != null) {
133                 buf.append(comment);
134                 buf.append(lineDelim);
135             }
136         }
137         if (fReturnTypeSig != null) {
138             if (!isInterface) {
139                 buf.append("private "); //$NON-NLS-1$
140
}
141         } else {
142             buf.append("public "); //$NON-NLS-1$
143
}
144
145         if (fReturnTypeSig != null) {
146             buf.append(Signature.toString(fReturnTypeSig));
147         }
148         buf.append(' ');
149         buf.append(fMethodName);
150         if (isInterface) {
151             buf.append("();"); //$NON-NLS-1$
152
buf.append(lineDelim);
153         } else {
154             buf.append("() {"); //$NON-NLS-1$
155
buf.append(lineDelim);
156
157             String JavaDoc body= CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", lineDelim); //$NON-NLS-1$
158
if (body != null) {
159                 buf.append(body);
160                 buf.append(lineDelim);
161             }
162             buf.append("}"); //$NON-NLS-1$
163
buf.append(lineDelim);
164         }
165         String JavaDoc stub= buf.toString();
166
167         // use the code formatter
168
IRegion region= document.getLineInformationOfOffset(getReplacementOffset());
169         int lineStart= region.getOffset();
170         int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);
171
172         String JavaDoc replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, null, lineDelim, fType.getJavaProject());
173
174         if (replacement.endsWith(lineDelim)) {
175             replacement= replacement.substring(0, replacement.length() - lineDelim.length());
176         }
177
178         setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
179         return true;
180     }
181
182     public CharSequence JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
183         return new String JavaDoc(); // don't let method stub proposals complete incrementally
184
}
185
186     /*
187      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
188      */

189     public boolean isAutoInsertable() {
190         return false;
191     }
192 }
193
Popular Tags