KickJava   Java API By Example, From Geeks To Geeks.

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


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.resource.ImageDescriptor;
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.PreferenceConstants;
38 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
43 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
44
45 /**
46  * TODO: this class is not used anywhere yet.
47  * (ContentAssist should be added to all source folder dialog fields.)
48  */

49 public class JavaPackageFragmentRootCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
50     private static final ImageDescriptorRegistry IMAGE_DESC_REGISTRY= JavaPlugin.getImageDescriptorRegistry();
51     
52     private IPackageFragmentRoot fPackageFragmentRoot;
53     private CompletionProposalComparator fComparator;
54
55     private char[] fProposalAutoActivationSet;
56
57     /**
58      * Creates a <code>JavaPackageCompletionProcessor</code> to complete existing packages
59      * in the context of <code>packageFragmentRoot</code>.
60      *
61      * @param packageFragmentRoot the context for package completion
62      */

63     public JavaPackageFragmentRootCompletionProcessor(IPackageFragmentRoot packageFragmentRoot) {
64         fPackageFragmentRoot= packageFragmentRoot;
65         fComparator= new CompletionProposalComparator();
66
67         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
68         String JavaDoc triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
69         fProposalAutoActivationSet = triggers.toCharArray();
70     }
71
72     /*(non-Javadoc)
73      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
74      */

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

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

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

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

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

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

119     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject,
120             int documentOffset) {
121         return null;
122     }
123
124     /*
125      * @see ISubjectControlContentAssistProcessor#computeCompletionProposals(IContentAssistSubjectControl, int)
126      */

127     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
128         String JavaDoc input= contentAssistSubjectControl.getDocument().get();
129         ICompletionProposal[] proposals= createPackagesProposals(documentOffset, input);
130         Arrays.sort(proposals, fComparator);
131         return proposals;
132     }
133
134     private ICompletionProposal[] createPackagesProposals(int documentOffset, String JavaDoc input) {
135         ArrayList JavaDoc proposals= new ArrayList JavaDoc();
136         String JavaDoc prefix= input.substring(0, documentOffset);
137         try {
138             IJavaElement[] packageFragments= fPackageFragmentRoot.getChildren();
139             for (int i= 0; i < packageFragments.length; i++) {
140                 IPackageFragment pack= (IPackageFragment) packageFragments[i];
141                 String JavaDoc packName= pack.getElementName();
142                 if (packName.length() == 0 || ! packName.startsWith(prefix))
143                     continue;
144                 Image image= getImage(JavaPluginImages.DESC_OBJS_PACKAGE);
145                 JavaCompletionProposal proposal= new JavaCompletionProposal(packName, 0, input.length(), image, packName, 0);
146                 proposals.add(proposal);
147             }
148         } catch (JavaModelException e) {
149             JavaPlugin.log(e);
150         }
151         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
152     }
153
154     private static Image getImage(ImageDescriptor descriptor) {
155         return (descriptor == null) ? null : JavaPackageFragmentRootCompletionProcessor.IMAGE_DESC_REGISTRY.get(descriptor);
156     }
157 }
158
Popular Tags