]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/java/GetterSetterCompletionProposal.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / java / GetterSetterCompletionProposal.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
19 import org.eclipse.jface.viewers.StyledString;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
24
25 import org.eclipse.jdt.core.IField;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
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.codemanipulation.GetterSetterUtil;
34 import org.eclipse.jdt.internal.corext.util.JdtFlags;
35 import org.eclipse.jdt.internal.corext.util.Messages;
36 import org.eclipse.jdt.internal.corext.util.Strings;
37
38 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
39
40 import org.eclipse.jdt.internal.ui.JavaPluginImages;
41 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
42 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
43
44 public class GetterSetterCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
45
46         public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException {
47                 if (prefix.length() == 0) {
48                         relevance--;
49                 }
50
51                 IField[] fields= type.getFields();
52                 IMethod[] methods= type.getMethods();
53                 for (int i= 0; i < fields.length; i++) {
54                         IField curr= fields[i];
55                         if (!JdtFlags.isEnum(curr)) {
56                                 String getterName= GetterSetterUtil.getGetterName(curr, null);
57                                 if (Strings.startsWithIgnoreCase(getterName, prefix) && !hasMethod(methods, getterName)) {
58                                         suggestedMethods.add(getterName);
59                                         int getterRelevance= relevance;
60                                         if (JdtFlags.isStatic(curr) && JdtFlags.isFinal(curr))
61                                                 getterRelevance= relevance - 1;
62                                         result.add(new GetterSetterCompletionProposal(curr, offset, length, true, getterRelevance));
63                                 }
64
65                                 if (!JdtFlags.isFinal(curr)) {
66                                         String setterName= GetterSetterUtil.getSetterName(curr, null);
67                                         if (Strings.startsWithIgnoreCase(setterName, prefix) && !hasMethod(methods, setterName)) {
68                                                 suggestedMethods.add(setterName);
69                                                 result.add(new GetterSetterCompletionProposal(curr, offset, length, false, relevance));
70                                         }
71                                 }
72                         }
73                 }
74         }
75
76         private static boolean hasMethod(IMethod[] methods, String name) {
77                 for (int i= 0; i < methods.length; i++) {
78                         if (methods[i].getElementName().equals(name)) {
79                                 return true;
80                         }
81                 }
82                 return false;
83         }
84
85         public final IField fField;
86         public final boolean fIsGetter;
87
88         public GetterSetterCompletionProposal(IField field, int start, int length, boolean isGetter, int relevance) throws JavaModelException {
89                 super("", field.getCompilationUnit(), start, length, JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC), getDisplayName(field, isGetter), relevance); //$NON-NLS-1$
90                 Assert.isNotNull(field);
91
92                 fField= field;
93                 fIsGetter= isGetter;
94                 setProposalInfo(new ProposalInfo(field));
95         }
96
97         private static StyledString getDisplayName(IField field, boolean isGetter) throws JavaModelException {
98                 StyledString buf= new StyledString();
99                 String fieldTypeName= Signature.toString(field.getTypeSignature());
100                 String fieldNameLabel= BasicElementLabels.getJavaElementName(field.getElementName());
101                 if (isGetter) {
102                         buf.append(BasicElementLabels.getJavaElementName(GetterSetterUtil.getGetterName(field, null) + "() : " + fieldTypeName)); //$NON-NLS-1$
103                         buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
104                         buf.append(Messages.format(JavaTextMessages.GetterSetterCompletionProposal_getter_label, fieldNameLabel), StyledString.QUALIFIER_STYLER);
105                 } else {
106                         buf.append(BasicElementLabels.getJavaElementName(GetterSetterUtil.getSetterName(field, null) + '(' + fieldTypeName + ") : void"  )); //$NON-NLS-1$
107                         buf.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
108                         buf.append(Messages.format(JavaTextMessages.GetterSetterCompletionProposal_setter_label, fieldNameLabel), StyledString.QUALIFIER_STYLER);
109                 }
110                 return buf;
111         }
112
113         /* (non-Javadoc)
114          * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
115          */
116         @Override
117         protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
118
119                 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject());
120                 return settings.generated_994620741155192875(document, this);
121         }
122
123         /*
124          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
125          */
126         public boolean isAutoInsertable() {
127                 return false;
128         }
129 }
130