]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/java/MethodDeclarationCompletionProposal.java
9c828346ced0d1ae0d764b61f1d862950c50abb7
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / java / MethodDeclarationCompletionProposal.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.util.Set;
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 import org.eclipse.jface.viewers.StyledString;
22
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
26
27 import org.eclipse.jdt.core.IMethod;
28 import org.eclipse.jdt.core.IType;
29 import org.eclipse.jdt.core.Signature;
30 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
31
32 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
33 import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
34
35 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
36 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.JavaPluginImages;
40 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
41 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
42
43
44 /**
45  * Method declaration proposal.
46  */
47 public class MethodDeclarationCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
48
49
50         public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException {
51                 IMethod[] methods= type.getMethods();
52                 if (!type.isInterface()) {
53                         String constructorName= type.getElementName();
54                         if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) {
55                                 result.add(new MethodDeclarationCompletionProposal(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 (!JavaConventionsUtil.validateMethodName(prefix, type).matches(IStatus.ERROR))
61                                 result.add(new MethodDeclarationCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance));
62                 }
63         }
64
65         private static boolean hasMethod(IMethod[] methods, String name) {
66                 for (int i= 0; i < methods.length; i++) {
67                         IMethod curr= methods[i];
68                         if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) {
69                                 return true;
70                         }
71                 }
72                 return false;
73         }
74
75         public final IType fType;
76         public final String fReturnTypeSig;
77         public final String fMethodName;
78
79         public MethodDeclarationCompletionProposal(IType type, String methodName, String returnTypeSig, int start, int length, int relevance) {
80                 super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); //$NON-NLS-1$
81                 Assert.isNotNull(type);
82                 Assert.isNotNull(methodName);
83
84                 fType= type;
85                 fMethodName= methodName;
86                 fReturnTypeSig= returnTypeSig;
87
88                 if (returnTypeSig == null) {
89                         setProposalInfo(new ProposalInfo(type));
90
91                         ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
92                         setImage(JavaPlugin.getImageDescriptorRegistry().get(desc));
93                 } else {
94                         setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE));
95                 }
96         }
97
98         private static StyledString getDisplayName(String methodName, String returnTypeSig) {
99                 StyledString buf= new StyledString();
100                 buf.append(methodName);
101                 buf.append('(');
102                 buf.append(')');
103                 if (returnTypeSig != null) {
104                         buf.append(" : "); //$NON-NLS-1$
105                         buf.append(Signature.toString(returnTypeSig));
106                         buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
107                         buf.append(JavaTextMessages.MethodCompletionProposal_method_label, StyledString.QUALIFIER_STYLER);
108                 } else {
109                         buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
110                         buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label, StyledString.QUALIFIER_STYLER);
111                 }
112                 return buf;
113         }
114
115         /* (non-Javadoc)
116          * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
117          */
118         @Override
119         protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
120
121                 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
122                 settings.generated_1584584742950606520(document, this);
123                 return true;
124         }
125
126         @Override
127         public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
128                 return new String(); // don't let method stub proposals complete incrementally
129         }
130
131         /*
132          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
133          */
134         public boolean isAutoInsertable() {
135                 return false;
136         }
137
138 }