KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.jface.resource.ImageDescriptor;
21
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 /**
48  * Method declaration proposal.
49  */

50 public class MethodDeclarationCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
51
52
53     public static void evaluateProposals(IType type, String JavaDoc prefix, int offset, int length, int relevance, Set JavaDoc suggestedMethods, Collection JavaDoc result) throws CoreException {
54         IMethod[] methods= type.getMethods();
55         if (!type.isInterface()) {
56             String JavaDoc constructorName= type.getElementName();
57             if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) {
58                 result.add(new MethodDeclarationCompletionProposal(type, constructorName, null, offset, length, relevance + 500));
59             }
60         }
61
62         if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { //$NON-NLS-1$
63
if (!JavaConventions.validateMethodName(prefix).matches(IStatus.ERROR)) {
64                 result.add(new MethodDeclarationCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance));
65             }
66         }
67     }
68
69     private static boolean hasMethod(IMethod[] methods, String JavaDoc name) {
70         for (int i= 0; i < methods.length; i++) {
71             IMethod curr= methods[i];
72             if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) {
73                 return true;
74             }
75         }
76         return false;
77     }
78
79     private final IType fType;
80     private final String JavaDoc fReturnTypeSig;
81     private final String JavaDoc fMethodName;
82
83     public MethodDeclarationCompletionProposal(IType type, String JavaDoc methodName, String JavaDoc returnTypeSig, int start, int length, int relevance) {
84         super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); //$NON-NLS-1$
85
Assert.isNotNull(type);
86         Assert.isNotNull(methodName);
87
88         fType= type;
89         fMethodName= methodName;
90         fReturnTypeSig= returnTypeSig;
91
92         if (returnTypeSig == null) {
93             setProposalInfo(new ProposalInfo(type));
94
95             ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
96             setImage(JavaPlugin.getImageDescriptorRegistry().get(desc));
97         } else {
98             setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE));
99         }
100     }
101
102     private static String JavaDoc getDisplayName(String JavaDoc methodName, String JavaDoc returnTypeSig) {
103         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
104         buf.append(methodName);
105         buf.append('(');
106         buf.append(')');
107         if (returnTypeSig != null) {
108             buf.append(" "); //$NON-NLS-1$
109
buf.append(Signature.toString(returnTypeSig));
110             buf.append(" - "); //$NON-NLS-1$
111
buf.append(JavaTextMessages.MethodCompletionProposal_method_label);
112         } else {
113             buf.append(" - "); //$NON-NLS-1$
114
buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label);
115         }
116         return buf.toString();
117     }
118
119     /* (non-Javadoc)
120      * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
121      */

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

192     public boolean isAutoInsertable() {
193         return false;
194     }
195 }
196
Popular Tags