1 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.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IRegion; 22 import org.eclipse.jface.text.TextUtilities; 23 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4; 24 25 import org.eclipse.jdt.core.Flags; 26 import org.eclipse.jdt.core.IField; 27 import org.eclipse.jdt.core.IMethod; 28 import org.eclipse.jdt.core.IType; 29 import org.eclipse.jdt.core.JavaModelException; 30 import org.eclipse.jdt.core.Signature; 31 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 32 import org.eclipse.jdt.core.formatter.CodeFormatter; 33 34 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 35 import org.eclipse.jdt.internal.corext.codemanipulation.GetterSetterUtil; 36 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 37 import org.eclipse.jdt.internal.corext.util.JdtFlags; 38 import org.eclipse.jdt.internal.corext.util.Messages; 39 import org.eclipse.jdt.internal.corext.util.Strings; 40 41 import org.eclipse.jdt.internal.ui.JavaPluginImages; 42 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 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 suggestedMethods, Collection 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 (getterName.startsWith(prefix) && !hasMethod(methods, getterName) && suggestedMethods.add(getterName)) { 58 result.add(new GetterSetterCompletionProposal(curr, offset, length, true, relevance)); 59 } 60 61 String setterName= GetterSetterUtil.getSetterName(curr, null); 62 if (setterName.startsWith(prefix) && !hasMethod(methods, setterName) && suggestedMethods.add(setterName)) { 63 result.add(new GetterSetterCompletionProposal(curr, offset, length, false, relevance)); 64 } 65 } 66 } 67 } 68 69 private static boolean hasMethod(IMethod[] methods, String name) { 70 for (int i= 0; i < methods.length; i++) { 71 if (methods[i].getElementName().equals(name)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 private final IField fField; 79 private final boolean fIsGetter; 80 81 public GetterSetterCompletionProposal(IField field, int start, int length, boolean isGetter, int relevance) throws JavaModelException { 82 super("", field.getCompilationUnit(), start, length, JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC), getDisplayName(field, isGetter), relevance); Assert.isNotNull(field); 84 85 fField= field; 86 fIsGetter= isGetter; 87 setProposalInfo(new ProposalInfo(field)); 88 } 89 90 private static String getDisplayName(IField field, boolean isGetter) throws JavaModelException { 91 StringBuffer buf= new StringBuffer (); 92 if (isGetter) { 93 buf.append(GetterSetterUtil.getGetterName(field, null)); 94 buf.append("() "); buf.append(Signature.toString(field.getTypeSignature())); 96 buf.append(" - "); buf.append(Messages.format(JavaTextMessages.GetterSetterCompletionProposal_getter_label, field.getElementName())); 98 } else { 99 buf.append(GetterSetterUtil.getSetterName(field, null)); 100 buf.append('(').append(Signature.toString(field.getTypeSignature())).append(')'); 101 buf.append(" "); buf.append(Signature.toString(Signature.SIG_VOID)); 103 buf.append(" - "); buf.append(Messages.format(JavaTextMessages.GetterSetterCompletionProposal_setter_label, field.getElementName())); 105 } 106 return buf.toString(); 107 } 108 109 112 protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { 113 114 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fField.getJavaProject()); 115 boolean addComments= settings.createComments; 116 int flags= Flags.AccPublic | (fField.getFlags() & Flags.AccStatic); 117 118 String stub; 119 if (fIsGetter) { 120 String getterName= GetterSetterUtil.getGetterName(fField, null); 121 stub= GetterSetterUtil.getGetterStub(fField, getterName, addComments, flags); 122 } else { 123 String setterName= GetterSetterUtil.getSetterName(fField, null); 124 stub= GetterSetterUtil.getSetterStub(fField, setterName, addComments, flags); 125 } 126 127 String lineDelim= TextUtilities.getDefaultLineDelimiter(document); 129 130 IRegion region= document.getLineInformationOfOffset(getReplacementOffset()); 131 int lineStart= region.getOffset(); 132 int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth); 133 134 String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, null, lineDelim, fField.getJavaProject()); 135 136 if (replacement.endsWith(lineDelim)) { 137 replacement= replacement.substring(0, replacement.length() - lineDelim.length()); 138 } 139 140 setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement)); 141 return true; 142 } 143 144 147 public boolean isAutoInsertable() { 148 return false; 149 } 150 } 151 152 | Popular Tags |