001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.contentassist;
011:
012: import java.util.Arrays;
013: import java.util.List;
014:
015: import org.eclipse.jdt.core.CompletionProposal;
016: import org.eclipse.jdt.core.ICompilationUnit;
017: import org.eclipse.jdt.core.IJavaElement;
018: import org.eclipse.jdt.core.IPackageFragment;
019: import org.eclipse.jdt.core.IType;
020: import org.eclipse.jdt.core.Signature;
021:
022: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
023: import org.eclipse.jdt.internal.corext.util.TypeFilter;
024:
025: import org.eclipse.jdt.internal.ui.JavaPluginImages;
026: import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
027:
028: public class JavaTypeCompletionProcessor extends
029: CUPositionCompletionProcessor {
030:
031: public static final String DUMMY_CLASS_NAME = "$$__$$"; //$NON-NLS-1$
032:
033: /**
034: * The CU name to be used if no parent ICompilationUnit is available.
035: * The main type of this class will be filtered out from the proposals list.
036: */
037: public static final String DUMMY_CU_NAME = DUMMY_CLASS_NAME
038: + JavaModelUtil.DEFAULT_CU_SUFFIX;
039:
040: /**
041: * Creates a <code>JavaTypeCompletionProcessor</code>.
042: * The completion context must be set via {@link #setPackageFragment(IPackageFragment)}.
043: *
044: * @param enableBaseTypes complete java base types iff <code>true</code>
045: * @param enableVoid complete <code>void</code> base type iff <code>true</code>
046: */
047: public JavaTypeCompletionProcessor(boolean enableBaseTypes,
048: boolean enableVoid) {
049: this (enableBaseTypes, enableVoid, false);
050: }
051:
052: /**
053: * Creates a <code>JavaTypeCompletionProcessor</code>.
054: * The completion context must be set via {@link #setPackageFragment(IPackageFragment)}.
055: *
056: * @param enableBaseTypes complete java base types iff <code>true</code>
057: * @param enableVoid complete <code>void</code> base type iff <code>true</code>
058: * @param fullyQualify always complete to fully qualifies type iff <code>true</code>
059: */
060: public JavaTypeCompletionProcessor(boolean enableBaseTypes,
061: boolean enableVoid, boolean fullyQualify) {
062: super (new TypeCompletionRequestor(enableBaseTypes, enableVoid,
063: fullyQualify));
064: }
065:
066: public char[] getCompletionProposalAutoActivationCharacters() {
067: // disable auto activation in dialog fields, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=89476
068: return null;
069: }
070:
071: /**
072: * @param packageFragment the new completion context
073: */
074: public void setPackageFragment(IPackageFragment packageFragment) {
075: //TODO: Some callers have a better completion context and should include imports
076: // and nested classes of their declaring CU in WC's source.
077: if (packageFragment == null) {
078: setCompletionContext(null, null, null);
079: } else {
080: String before = "public class " + DUMMY_CLASS_NAME + " { "; //$NON-NLS-1$//$NON-NLS-2$
081: String after = " }"; //$NON-NLS-1$
082: setCompletionContext(packageFragment
083: .getCompilationUnit(DUMMY_CU_NAME), before, after);
084: }
085: }
086:
087: public void setExtendsCompletionContext(IJavaElement javaElement) {
088: if (javaElement instanceof IPackageFragment) {
089: IPackageFragment packageFragment = (IPackageFragment) javaElement;
090: ICompilationUnit cu = packageFragment
091: .getCompilationUnit(DUMMY_CU_NAME);
092: setCompletionContext(
093: cu,
094: "public class " + DUMMY_CLASS_NAME + " extends ", " {}"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
095: } else if (javaElement instanceof IType) {
096: // pattern: public class OuterType { public class Type extends /*caret*/ {} }
097: IType type = (IType) javaElement;
098: String before = "public class " + type.getElementName() + " extends "; //$NON-NLS-1$ //$NON-NLS-2$
099: String after = " {}"; //$NON-NLS-1$
100: IJavaElement parent = type.getParent();
101: while (parent instanceof IType) {
102: type = (IType) parent;
103: before += "public class " + type.getElementName() + " {"; //$NON-NLS-1$ //$NON-NLS-2$
104: after += "}"; //$NON-NLS-1$
105: parent = type.getParent();
106: }
107: ICompilationUnit cu = type.getCompilationUnit();
108: setCompletionContext(cu, before, after);
109: } else {
110: setCompletionContext(null, null, null);
111: }
112: }
113:
114: // public void setImplementsCompletionContext(IPackageFragment packageFragment) {
115: // ICompilationUnit cu= packageFragment.getCompilationUnit(DUMMY_CU_NAME);
116: // setCompletionContext(cu, "public class " + DUMMY_CLASS_NAME + " implements ", " {}"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
117: // }
118:
119: protected static class TypeCompletionRequestor extends
120: CUPositionCompletionRequestor {
121: private static final String VOID = "void"; //$NON-NLS-1$
122: private static final List BASE_TYPES = Arrays
123: .asList(new String[] {
124: "boolean", "byte", "char", "double", "float", "int", "long", "short" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
125:
126: private boolean fEnableBaseTypes;
127: private boolean fEnableVoid;
128: private final boolean fFullyQualify;
129:
130: public TypeCompletionRequestor(boolean enableBaseTypes,
131: boolean enableVoid, boolean fullyQualify) {
132: fFullyQualify = fullyQualify;
133: fEnableBaseTypes = enableBaseTypes;
134: fEnableVoid = enableVoid;
135: setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION,
136: true);
137: setIgnored(CompletionProposal.FIELD_REF, true);
138: setIgnored(CompletionProposal.LABEL_REF, true);
139: setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
140: setIgnored(CompletionProposal.METHOD_DECLARATION, true);
141: setIgnored(CompletionProposal.METHOD_REF, true);
142: setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
143: setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION,
144: true);
145: setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
146: }
147:
148: public void accept(CompletionProposal proposal) {
149: switch (proposal.getKind()) {
150: case CompletionProposal.PACKAGE_REF:
151: char[] packageName = proposal.getDeclarationSignature();
152: if (TypeFilter.isFiltered(packageName))
153: return;
154: addAdjustedCompletion(new String(packageName),
155: new String(proposal.getCompletion()), proposal
156: .getReplaceStart(), proposal
157: .getReplaceEnd(), proposal
158: .getRelevance(),
159: JavaPluginImages.DESC_OBJS_PACKAGE);
160: return;
161:
162: case CompletionProposal.TYPE_REF:
163: char[] fullName = Signature.toCharArray(proposal
164: .getSignature());
165: if (TypeFilter.isFiltered(fullName))
166: return;
167: StringBuffer buf = new StringBuffer();
168: buf.append(Signature.getSimpleName(fullName));
169: if (buf.length() == 0)
170: return; // this is the dummy class, whose $ have been converted to dots
171: char[] typeQualifier = Signature.getQualifier(fullName);
172: if (typeQualifier.length > 0) {
173: buf.append(" - "); //$NON-NLS-1$
174: buf.append(typeQualifier);
175: }
176: String name = buf.toString();
177:
178: addAdjustedTypeCompletion(name, new String(proposal
179: .getCompletion()), proposal.getReplaceStart(),
180: proposal.getReplaceEnd(), proposal
181: .getRelevance(),
182: JavaElementImageProvider
183: .getTypeImageDescriptor(false, false,
184: proposal.getFlags(), false),
185: fFullyQualify ? new String(fullName) : null);
186: return;
187:
188: case CompletionProposal.KEYWORD:
189: if (!fEnableBaseTypes)
190: return;
191: String keyword = new String(proposal.getName());
192: if ((fEnableVoid && VOID.equals(keyword))
193: || (fEnableBaseTypes && BASE_TYPES
194: .contains(keyword)))
195: addAdjustedCompletion(keyword, new String(proposal
196: .getCompletion()), proposal
197: .getReplaceStart(), proposal
198: .getReplaceEnd(), proposal.getRelevance(),
199: null);
200: return;
201:
202: default:
203: return;
204: }
205:
206: }
207: }
208: }
|