001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.text.java;
011:
012: import org.eclipse.text.edits.TextEdit;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.NullProgressMonitor;
016:
017: import org.eclipse.swt.graphics.Image;
018:
019: import org.eclipse.jface.preference.IPreferenceStore;
020:
021: import org.eclipse.jface.text.BadLocationException;
022: import org.eclipse.jface.text.IDocument;
023:
024: import org.eclipse.jdt.core.ICompilationUnit;
025: import org.eclipse.jdt.core.IType;
026: import org.eclipse.jdt.core.Signature;
027: import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
028:
029: import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
030:
031: import org.eclipse.jdt.ui.PreferenceConstants;
032:
033: import org.eclipse.jdt.internal.ui.JavaPlugin;
034:
035: /**
036: * If passed compilation unit is not null, the replacement string will be seen as a qualified type name.
037: */
038: public class JavaTypeCompletionProposal extends JavaCompletionProposal {
039:
040: protected final ICompilationUnit fCompilationUnit;
041:
042: /** The unqualified type name. */
043: private final String fUnqualifiedTypeName;
044: /** The fully qualified type name. */
045: private final String fFullyQualifiedTypeName;
046:
047: public JavaTypeCompletionProposal(String replacementString,
048: ICompilationUnit cu, int replacementOffset,
049: int replacementLength, Image image, String displayString,
050: int relevance) {
051: this (replacementString, cu, replacementOffset,
052: replacementLength, image, displayString, relevance,
053: null);
054: }
055:
056: public JavaTypeCompletionProposal(String replacementString,
057: ICompilationUnit cu, int replacementOffset,
058: int replacementLength, Image image, String displayString,
059: int relevance, String fullyQualifiedTypeName) {
060: super (replacementString, replacementOffset, replacementLength,
061: image, displayString, relevance);
062: fCompilationUnit = cu;
063: fFullyQualifiedTypeName = fullyQualifiedTypeName;
064: fUnqualifiedTypeName = fullyQualifiedTypeName != null ? Signature
065: .getSimpleName(fullyQualifiedTypeName)
066: : null;
067: }
068:
069: protected boolean updateReplacementString(IDocument document,
070: char trigger, int offset, ImportRewrite impRewrite)
071: throws CoreException, BadLocationException {
072: // avoid adding imports when inside imports container
073: if (impRewrite != null && fFullyQualifiedTypeName != null) {
074: String replacementString = getReplacementString();
075: String qualifiedType = fFullyQualifiedTypeName;
076: if (qualifiedType.indexOf('.') != -1
077: && replacementString.startsWith(qualifiedType)
078: && !replacementString.endsWith(String.valueOf(';'))) {
079: IType[] types = impRewrite.getCompilationUnit()
080: .getTypes();
081: if (types.length > 0
082: && types[0].getSourceRange().getOffset() <= offset) {
083: // ignore positions above type.
084: setReplacementString(impRewrite
085: .addImport(getReplacementString()));
086: return true;
087: }
088: }
089: }
090: return false;
091: }
092:
093: /* (non-Javadoc)
094: * @see ICompletionProposalExtension#apply(IDocument, char, int)
095: */
096: public void apply(IDocument document, char trigger, int offset) {
097: try {
098: ImportRewrite impRewrite = null;
099:
100: if (fCompilationUnit != null && allowAddingImports()) {
101: impRewrite = StubUtility.createImportRewrite(
102: fCompilationUnit, true);
103: }
104:
105: boolean importAdded = updateReplacementString(document,
106: trigger, offset, impRewrite);
107:
108: if (importAdded)
109: setCursorPosition(getReplacementString().length());
110:
111: super .apply(document, trigger, offset);
112:
113: if (importAdded && impRewrite != null) {
114: int oldLen = document.getLength();
115: impRewrite.rewriteImports(new NullProgressMonitor())
116: .apply(document, TextEdit.UPDATE_REGIONS);
117: setReplacementOffset(getReplacementOffset()
118: + document.getLength() - oldLen);
119: }
120: } catch (CoreException e) {
121: JavaPlugin.log(e);
122: } catch (BadLocationException e) {
123: JavaPlugin.log(e);
124: }
125: }
126:
127: private boolean allowAddingImports() {
128: IPreferenceStore preferenceStore = JavaPlugin.getDefault()
129: .getPreferenceStore();
130: return preferenceStore
131: .getBoolean(PreferenceConstants.CODEASSIST_ADDIMPORT);
132: }
133:
134: /*
135: * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String)
136: */
137: protected boolean isValidPrefix(String prefix) {
138: return super .isValidPrefix(prefix)
139: || isPrefix(prefix, fUnqualifiedTypeName)
140: || isPrefix(prefix, fFullyQualifiedTypeName);
141: }
142:
143: /*
144: * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#getCompletionText()
145: */
146: public CharSequence getPrefixCompletionText(IDocument document,
147: int completionOffset) {
148: return fUnqualifiedTypeName;
149: }
150:
151: }
|