KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.graphics.Image;
19
20 import org.eclipse.jface.contentassist.IContentAssistSubjectControl;
21 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.ILabelProvider;
24
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.contentassist.ICompletionProposal;
27 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
28 import org.eclipse.jface.text.contentassist.IContextInformation;
29 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
30
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IPackageFragment;
33 import org.eclipse.jdt.core.IPackageFragmentRoot;
34 import org.eclipse.jdt.core.JavaModelException;
35
36
37 import org.eclipse.jdt.ui.JavaElementLabelProvider;
38 import org.eclipse.jdt.ui.PreferenceConstants;
39 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
43
44 public class JavaPackageCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
45     
46     private IPackageFragmentRoot fPackageFragmentRoot;
47     private CompletionProposalComparator fComparator;
48     private ILabelProvider fLabelProvider;
49
50     private char[] fProposalAutoActivationSet;
51
52     /**
53      * Creates a <code>JavaPackageCompletionProcessor</code>.
54      * The completion context must be set via {@link #setPackageFragmentRoot(IPackageFragmentRoot)}.
55      */

56     public JavaPackageCompletionProcessor() {
57         this(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS));
58     }
59     
60     /**
61      * Creates a <code>JavaPackageCompletionProcessor</code>.
62      * The Processor uses the given <code>ILabelProvider</code> to show text and icons for the
63      * possible completions.
64      * @param labelProvider Used for the popups.
65      */

66     public JavaPackageCompletionProcessor(ILabelProvider labelProvider) {
67         fComparator= new CompletionProposalComparator();
68         fLabelProvider= labelProvider;
69
70         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
71         String JavaDoc triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
72         fProposalAutoActivationSet = triggers.toCharArray();
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
77      */

78     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
79         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
80
return null;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
85      */

86     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
87         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
88
return null;
89     }
90
91     /*
92      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
93      */

94     public char[] getCompletionProposalAutoActivationCharacters() {
95         return fProposalAutoActivationSet;
96     }
97
98     /*
99      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
100      */

101     public char[] getContextInformationAutoActivationCharacters() {
102         return null;
103     }
104
105     /*
106      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
107      */

108     public String JavaDoc getErrorMessage() {
109         return null;
110     }
111
112     /*
113      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
114      */

115     public IContextInformationValidator getContextInformationValidator() {
116         return null; //no context
117
}
118
119     /*
120      * @see ISubjectControlContentAssistProcessor#computeContextInformation(IContentAssistSubjectControl, int)
121      */

122     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl,
123             int documentOffset) {
124         return null;
125     }
126
127     /*
128      * @see ISubjectControlContentAssistProcessor#computeCompletionProposals(IContentAssistSubjectControl, int)
129      */

130     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
131         if (fPackageFragmentRoot == null)
132             return null;
133         String JavaDoc input= contentAssistSubjectControl.getDocument().get();
134         ICompletionProposal[] proposals= createPackagesProposals(documentOffset, input);
135         Arrays.sort(proposals, fComparator);
136         return proposals;
137     }
138     
139     public void setPackageFragmentRoot(IPackageFragmentRoot packageFragmentRoot) {
140         fPackageFragmentRoot= packageFragmentRoot;
141     }
142
143     private ICompletionProposal[] createPackagesProposals(int documentOffset, String JavaDoc input) {
144         ArrayList JavaDoc proposals= new ArrayList JavaDoc();
145         String JavaDoc prefix= input.substring(0, documentOffset);
146         try {
147             IJavaElement[] packageFragments= fPackageFragmentRoot.getChildren();
148             for (int i= 0; i < packageFragments.length; i++) {
149                 IPackageFragment pack= (IPackageFragment) packageFragments[i];
150                 String JavaDoc packName= pack.getElementName();
151                 if (packName.length() == 0 || ! packName.startsWith(prefix))
152                     continue;
153                 Image image= fLabelProvider.getImage(pack);
154                 JavaCompletionProposal proposal= new JavaCompletionProposal(packName, 0, input.length(), image, fLabelProvider.getText(pack), 0);
155                 proposals.add(proposal);
156             }
157         } catch (JavaModelException e) {
158             //fPackageFragmentRoot is not a proper root -> no proposals
159
}
160         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
161     }
162 }
163
Popular Tags