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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.java;
13 import java.util.Collection;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.viewers.StyledString;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
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;
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;
38 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
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;
44 public class GetterSetterCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
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) {
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));
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));
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)) {
85 public final IField fField;
86 public final boolean fIsGetter;
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);
94 setProposalInfo(new ProposalInfo(field));
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());
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);
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);
114 * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
117 protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
119 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject());
120 return settings.generated_994620741155192875(document, this);
124 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
126 public boolean isAutoInsertable() {