]>
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.refactoring.contentassist; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Arrays; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.swt.graphics.Image; | |
18 | ||
19 | import org.eclipse.core.runtime.Assert; | |
20 | ||
21 | import org.eclipse.core.resources.ResourcesPlugin; | |
22 | ||
23 | import org.eclipse.jface.contentassist.IContentAssistSubjectControl; | |
24 | import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor; | |
25 | import org.eclipse.jface.preference.IPreferenceStore; | |
26 | ||
27 | import org.eclipse.jface.text.ITextViewer; | |
28 | import org.eclipse.jface.text.contentassist.ICompletionProposal; | |
29 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; | |
30 | import org.eclipse.jface.text.contentassist.IContextInformation; | |
31 | import org.eclipse.jface.text.contentassist.IContextInformationValidator; | |
32 | ||
33 | import org.eclipse.jdt.core.IJavaModel; | |
34 | import org.eclipse.jdt.core.IJavaProject; | |
35 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
36 | import org.eclipse.jdt.core.JavaCore; | |
37 | import org.eclipse.jdt.core.JavaModelException; | |
38 | ||
39 | import org.eclipse.jdt.ui.JavaElementLabelProvider; | |
40 | import org.eclipse.jdt.ui.PreferenceConstants; | |
41 | import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; | |
42 | ||
43 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
44 | import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; | |
45 | ||
46 | /** | |
47 | * Simple completion processor that completes package fragment roots that are source | |
48 | * folders. | |
49 | */ | |
50 | public class JavaSourcePackageFragmentRootCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { | |
51 | ||
52 | private char[] fProposalAutoActivationSet; | |
53 | private IJavaModel fRoot; | |
54 | private CompletionProposalComparator fComparator; | |
55 | private JavaElementLabelProvider fLabelProvider; | |
56 | ||
57 | public JavaSourcePackageFragmentRootCompletionProcessor() { | |
58 | fRoot= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); | |
59 | IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); | |
60 | String triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA); | |
61 | fProposalAutoActivationSet= triggers.toCharArray(); | |
62 | fComparator= new CompletionProposalComparator(); | |
63 | fLabelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS); | |
64 | } | |
65 | ||
66 | public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { | |
67 | Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$ | |
68 | return null; | |
69 | } | |
70 | ||
71 | public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { | |
72 | Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$ | |
73 | return null; | |
74 | } | |
75 | ||
76 | public char[] getCompletionProposalAutoActivationCharacters() { | |
77 | return fProposalAutoActivationSet; | |
78 | } | |
79 | ||
80 | public char[] getContextInformationAutoActivationCharacters() { | |
81 | return null; | |
82 | } | |
83 | ||
84 | public String getErrorMessage() { | |
85 | return null; | |
86 | } | |
87 | ||
88 | public IContextInformationValidator getContextInformationValidator() { | |
89 | return null; | |
90 | } | |
91 | ||
92 | public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) { | |
93 | if (fRoot == null) { | |
94 | return null; | |
95 | } | |
96 | ||
97 | String input= contentAssistSubject.getDocument().get(); | |
98 | String prefix= input.substring(0, documentOffset); | |
99 | ||
100 | ICompletionProposal[] proposals= createSourcePackageFragmentRootProposals(prefix, input.length()); | |
101 | Arrays.sort(proposals, fComparator); | |
102 | return proposals; | |
103 | } | |
104 | ||
105 | /** | |
106 | * @param prefix prefixString with thatthe sourcepackagefragmentroots must begin | |
107 | * @param replacementLength length of the text to replace | |
108 | * @return array with sourcepackagefragmentroots | |
109 | */ | |
110 | private ICompletionProposal[] createSourcePackageFragmentRootProposals(String prefix, int replacementLength) { | |
111 | List<JavaCompletionProposal> proposals= new ArrayList<JavaCompletionProposal>(); | |
112 | try { | |
113 | IJavaProject[] projects= fRoot.getJavaProjects(); | |
114 | for (int i= 0; i < projects.length; i++) { | |
115 | IJavaProject project= projects[i]; | |
116 | IPackageFragmentRoot[] roots= project.getPackageFragmentRoots(); | |
117 | for (int j= 0; j < roots.length; j++) { | |
118 | IPackageFragmentRoot root= roots[j]; | |
119 | if (root.exists() && (root.getKind() == IPackageFragmentRoot.K_SOURCE)) { | |
120 | String name= root.getPath().toString(); | |
121 | if (name.length() > 1) { | |
122 | name= name.substring(1); | |
123 | } | |
124 | if (name.startsWith(prefix)) { | |
125 | Image image= fLabelProvider.getImage(root); | |
126 | JavaCompletionProposal proposal= new JavaCompletionProposal(name, 0, replacementLength, image, | |
127 | name, 0); | |
128 | proposals.add(proposal); | |
129 | } | |
130 | } | |
131 | } | |
132 | } | |
133 | } catch (JavaModelException e) { | |
134 | // nothing to do | |
135 | } | |
136 | ||
137 | return proposals.toArray(new ICompletionProposal[proposals.size()]); | |
138 | } | |
139 | ||
140 | public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, int documentOffset) { | |
141 | return null; | |
142 | } | |
143 | ||
144 | } |