]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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 | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.text.java; | |
12 | ||
13 | import org.eclipse.swt.graphics.Image; | |
14 | ||
15 | import org.eclipse.core.runtime.CoreException; | |
16 | import org.eclipse.core.runtime.NullProgressMonitor; | |
17 | ||
18 | import org.eclipse.text.edits.TextEdit; | |
19 | ||
20 | import org.eclipse.jface.preference.IPreferenceStore; | |
21 | import org.eclipse.jface.viewers.StyledString; | |
22 | ||
23 | import org.eclipse.jface.text.BadLocationException; | |
24 | import org.eclipse.jface.text.IDocument; | |
25 | ||
26 | import org.eclipse.jdt.core.ICompilationUnit; | |
27 | import org.eclipse.jdt.core.IType; | |
28 | import org.eclipse.jdt.core.Signature; | |
29 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
30 | ||
31 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; | |
32 | ||
33 | import org.eclipse.jdt.ui.PreferenceConstants; | |
34 | import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; | |
35 | ||
36 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
37 | import org.eclipse.jdt.internal.ui.refactoring.contentassist.CUPositionCompletionProcessor.CUPositionCompletionRequestor; | |
38 | ||
39 | ||
40 | /** | |
41 | * If passed compilation unit is not null, the replacement string will be seen as a qualified type name. | |
42 | */ | |
43 | public class JavaTypeCompletionProposal extends JavaCompletionProposal { | |
44 | ||
45 | protected final ICompilationUnit fCompilationUnit; | |
46 | ||
47 | /** The unqualified type name. */ | |
48 | private final String fUnqualifiedTypeName; | |
49 | /** The fully qualified type name. */ | |
50 | private final String fFullyQualifiedTypeName; | |
51 | ||
52 | public JavaTypeCompletionProposal(String replacementString, ICompilationUnit cu, int replacementOffset, int replacementLength, Image image, StyledString displayString, int relevance) { | |
53 | this(replacementString, cu, replacementOffset, replacementLength, image, displayString, relevance, null); | |
54 | } | |
55 | ||
56 | public JavaTypeCompletionProposal(String replacementString, ICompilationUnit cu, int replacementOffset, int replacementLength, Image image, StyledString displayString, int relevance, String fullyQualifiedTypeName) { | |
57 | this(replacementString, cu, replacementOffset, replacementLength, image, displayString, relevance, fullyQualifiedTypeName, null); | |
58 | } | |
59 | ||
60 | public JavaTypeCompletionProposal(String replacementString, ICompilationUnit cu, int replacementOffset, int replacementLength, Image image, StyledString displayString, int relevance, String fullyQualifiedTypeName, JavaContentAssistInvocationContext invocationContext) { | |
61 | super(replacementString, replacementOffset, replacementLength, image, displayString, relevance, false, invocationContext); | |
62 | fCompilationUnit= cu; | |
63 | fFullyQualifiedTypeName= fullyQualifiedTypeName; | |
64 | fUnqualifiedTypeName= fullyQualifiedTypeName != null ? Signature.getSimpleName(fullyQualifiedTypeName) : null; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Updates the replacement string. | |
69 | * | |
70 | * @param document the document | |
71 | * @param trigger the trigger | |
72 | * @param offset the offset | |
73 | * @param impRewrite the import rewrite | |
74 | * @return <code>true</code> if the cursor position should be updated, <code>false</code> otherwise | |
75 | * @throws BadLocationException if accessing the document fails | |
76 | * @throws CoreException if something else fails | |
77 | */ | |
78 | protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { | |
79 | // avoid adding imports when inside imports container | |
80 | if (impRewrite != null && fFullyQualifiedTypeName != null) { | |
81 | String replacementString= getReplacementString(); | |
82 | String qualifiedType= fFullyQualifiedTypeName; | |
83 | if (qualifiedType.indexOf('.') != -1 && replacementString.startsWith(qualifiedType) && !replacementString.endsWith(String.valueOf(';'))) { | |
84 | IType[] types= impRewrite.getCompilationUnit().getTypes(); | |
85 | if (types.length > 0 && types[0].getSourceRange().getOffset() <= offset) { | |
86 | // ignore positions above type. | |
87 | setReplacementString(impRewrite.addImport(getReplacementString())); | |
88 | return true; | |
89 | } | |
90 | } | |
91 | } | |
92 | return false; | |
93 | } | |
94 | ||
95 | ||
96 | /* (non-Javadoc) | |
97 | * @see ICompletionProposalExtension#apply(IDocument, char, int) | |
98 | */ | |
99 | @Override | |
100 | public void apply(IDocument document, char trigger, int offset) { | |
101 | try { | |
102 | ImportRewrite impRewrite= null; | |
103 | ||
104 | if (fCompilationUnit != null && allowAddingImports()) { | |
105 | impRewrite= StubUtility.createImportRewrite(fCompilationUnit, true); | |
106 | } | |
107 | ||
108 | boolean updateCursorPosition= updateReplacementString(document, trigger, offset, impRewrite); | |
109 | ||
110 | if (updateCursorPosition) | |
111 | setCursorPosition(getReplacementString().length()); | |
112 | ||
113 | super.apply(document, trigger, offset); | |
114 | ||
115 | if (impRewrite != null) { | |
116 | int oldLen= document.getLength(); | |
117 | impRewrite.rewriteImports(new NullProgressMonitor()).apply(document, TextEdit.UPDATE_REGIONS); | |
118 | setReplacementOffset(getReplacementOffset() + 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().getPreferenceStore(); | |
129 | return preferenceStore.getBoolean(PreferenceConstants.CODEASSIST_ADDIMPORT); | |
130 | } | |
131 | ||
132 | /* | |
133 | * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String) | |
134 | */ | |
135 | @Override | |
136 | protected boolean isValidPrefix(String prefix) { | |
137 | return super.isValidPrefix(prefix) || isPrefix(prefix, fUnqualifiedTypeName) || isPrefix(prefix, fFullyQualifiedTypeName); | |
138 | } | |
139 | ||
140 | /* | |
141 | * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#getCompletionText() | |
142 | */ | |
143 | @Override | |
144 | public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { | |
145 | return fUnqualifiedTypeName; | |
146 | } | |
147 | ||
148 | public void generated_2013497102554989698(CUPositionCompletionRequestor cupositioncompletionrequestor) { | |
149 | setTriggerCharacters(CUPositionCompletionRequestor.TRIGGER_CHARACTERS); | |
150 | cupositioncompletionrequestor.fProposals.add(this); | |
151 | } | |
152 | ||
153 | } |