KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
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 JavaDoc prefix, int offset, int length, int relevance, Set JavaDoc suggestedMethods, Collection JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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); //$NON-NLS-1$
83
Assert.isNotNull(field);
84
85         fField= field;
86         fIsGetter= isGetter;
87         setProposalInfo(new ProposalInfo(field));
88     }
89
90     private static String JavaDoc getDisplayName(IField field, boolean isGetter) throws JavaModelException {
91         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
92         if (isGetter) {
93             buf.append(GetterSetterUtil.getGetterName(field, null));
94             buf.append("() "); //$NON-NLS-1$
95
buf.append(Signature.toString(field.getTypeSignature()));
96             buf.append(" - "); //$NON-NLS-1$
97
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(" "); //$NON-NLS-1$
102
buf.append(Signature.toString(Signature.SIG_VOID));
103             buf.append(" - "); //$NON-NLS-1$
104
buf.append(Messages.format(JavaTextMessages.GetterSetterCompletionProposal_setter_label, field.getElementName()));
105         }
106         return buf.toString();
107     }
108
109     /* (non-Javadoc)
110      * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
111      */

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 JavaDoc stub;
119         if (fIsGetter) {
120             String JavaDoc getterName= GetterSetterUtil.getGetterName(fField, null);
121             stub= GetterSetterUtil.getGetterStub(fField, getterName, addComments, flags);
122         } else {
123             String JavaDoc setterName= GetterSetterUtil.getSetterName(fField, null);
124             stub= GetterSetterUtil.getSetterStub(fField, setterName, addComments, flags);
125         }
126
127         // use the code formatter
128
String JavaDoc 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 JavaDoc 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     /*
145      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
146      */

147     public boolean isAutoInsertable() {
148         return false;
149     }
150 }
151
152
Popular Tags