KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > contentassist > JavaSourcePackageFragmentRootCompletionProcessor


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.refactoring.contentassist;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.core.resources.ResourcesPlugin;
20
21 import org.eclipse.swt.graphics.Image;
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
40 import org.eclipse.jdt.ui.JavaElementLabelProvider;
41 import org.eclipse.jdt.ui.PreferenceConstants;
42 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
46
47 /**
48  * Simple completion processor that completes package fragment roots that are source
49  * folders.
50  */

51 public class JavaSourcePackageFragmentRootCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
52
53     private char[] fProposalAutoActivationSet;
54     private IJavaModel fRoot;
55     private CompletionProposalComparator fComparator;
56     private JavaElementLabelProvider fLabelProvider;
57
58     public JavaSourcePackageFragmentRootCompletionProcessor() {
59         fRoot= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
60         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
61         String JavaDoc triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
62         fProposalAutoActivationSet= triggers.toCharArray();
63         fComparator= new CompletionProposalComparator();
64         fLabelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS);
65     }
66
67     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
68         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
69
return null;
70     }
71
72     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
73         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
74
return null;
75     }
76
77     public char[] getCompletionProposalAutoActivationCharacters() {
78         return fProposalAutoActivationSet;
79     }
80
81     public char[] getContextInformationAutoActivationCharacters() {
82         return null;
83     }
84
85     public String JavaDoc getErrorMessage() {
86         return null;
87     }
88
89     public IContextInformationValidator getContextInformationValidator() {
90         return null;
91     }
92
93     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
94         if (fRoot == null) {
95             return null;
96         }
97
98         String JavaDoc input= contentAssistSubject.getDocument().get();
99         String JavaDoc prefix= input.substring(0, documentOffset);
100
101         ICompletionProposal[] proposals= createSourcePackageFragmentRootProposals(prefix, input.length());
102         Arrays.sort(proposals, fComparator);
103         return proposals;
104     }
105
106     /**
107      * @param prefix prefixString with thatthe sourcepackagefragmentroots must begin
108      * @param replacementLength length of the text to replace
109      * @return array with sourcepackagefragmentroots
110      */

111     private ICompletionProposal[] createSourcePackageFragmentRootProposals(String JavaDoc prefix, int replacementLength) {
112         List JavaDoc proposals= new ArrayList JavaDoc();
113         try {
114             IJavaProject[] projects= fRoot.getJavaProjects();
115             for (int i= 0; i < projects.length; i++) {
116                 IJavaProject project= projects[i];
117                 IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
118                 for (int j= 0; j < roots.length; j++) {
119                     IPackageFragmentRoot root= roots[j];
120                     if (root.exists() && (root.getKind() == IPackageFragmentRoot.K_SOURCE)) {
121                         String JavaDoc name= root.getPath().toString();
122                         if (name.length() > 1) {
123                             name= name.substring(1);
124                         }
125                         if (name.startsWith(prefix)) {
126                             Image image= fLabelProvider.getImage(root);
127                             JavaCompletionProposal proposal= new JavaCompletionProposal(name, 0, replacementLength, image,
128                                 name, 0);
129                             proposals.add(proposal);
130                         }
131                     }
132                 }
133             }
134         } catch (JavaModelException e) {
135             // nothing to do
136
}
137
138         return (ICompletionProposal[])proposals.toArray(new ICompletionProposal[proposals.size()]);
139     }
140
141     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
142         return null;
143     }
144
145 }
146
Popular Tags