KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.refactoring.contentassist;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.swt.graphics.Image;
20
21 import org.eclipse.jface.contentassist.IContentAssistSubjectControl;
22 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.contentassist.ICompletionProposal;
25 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
26 import org.eclipse.jface.text.contentassist.IContextInformation;
27 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
28
29 import org.eclipse.jface.resource.ImageDescriptor;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.JavaPluginImages;
33 import org.eclipse.jdt.internal.ui.JavaUIMessages;
34 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
35 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
36
37 public class VariableNamesProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
38
39     private String JavaDoc fErrorMessage;
40     
41     private String JavaDoc[] fTempNameProposals;
42     
43     private ImageDescriptorRegistry fImageRegistry;
44     private ImageDescriptor fProposalImageDescriptor;
45
46     public VariableNamesProcessor(String JavaDoc[] tempNameProposals) {
47         fTempNameProposals= (String JavaDoc[]) tempNameProposals.clone();
48         Arrays.sort(fTempNameProposals);
49         fImageRegistry= JavaPlugin.getImageDescriptorRegistry();
50         fProposalImageDescriptor= JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;
51
52     }
53     
54     public void setProposalImageDescriptor(ImageDescriptor proposalImageDescriptor) {
55         fProposalImageDescriptor= proposalImageDescriptor;
56     }
57
58     /*
59      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
60      */

61     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
62         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
63
return null;
64     }
65
66     /*
67      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
68      */

69     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
70         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
71
return null;
72     }
73
74     /*
75      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
76      */

77     public char[] getCompletionProposalAutoActivationCharacters() {
78         return null;
79     }
80
81     /*
82      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
83      */

84     public char[] getContextInformationAutoActivationCharacters() {
85         return null; //no context
86
}
87
88     /*
89      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
90      */

91     public String JavaDoc getErrorMessage() {
92         return fErrorMessage;
93     }
94
95     /*
96      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
97      */

98     public IContextInformationValidator getContextInformationValidator() {
99         return null; //no context
100
}
101
102     /*
103      * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeContextInformation(org.eclipse.jface.contentassist.IContentAssistSubject, int)
104      */

105     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
106         return null; //no context
107
}
108
109     /*
110      * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeCompletionProposals(org.eclipse.jface.contentassist.IContentAssistSubject, int)
111      */

112     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
113         if (fTempNameProposals.length == 0)
114             return null;
115         String JavaDoc input= contentAssistSubject.getDocument().get();
116         
117         ArrayList JavaDoc proposals= new ArrayList JavaDoc();
118         String JavaDoc prefix= input.substring(0, documentOffset);
119         Image image= fImageRegistry.get(fProposalImageDescriptor);
120         for (int i= 0; i < fTempNameProposals.length; i++) {
121             String JavaDoc tempName= fTempNameProposals[i];
122             if (tempName.length() == 0 || ! tempName.startsWith(prefix))
123                 continue;
124             JavaCompletionProposal proposal= new JavaCompletionProposal(tempName, 0, input.length(), image, tempName, 0);
125             proposals.add(proposal);
126         }
127         fErrorMessage= proposals.size() > 0 ? null : JavaUIMessages.JavaEditor_codeassist_noCompletions;
128         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
129     }
130
131 }
132
Popular Tags