KickJava   Java API By Example, From Geeks To Geeks.

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


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.resource.ImageDescriptor;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.contentassist.ICompletionProposal;
26 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
29
30 import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
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 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
37
38 public class FieldNameProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor {
39
40     private String JavaDoc[] fFieldNameProposals;
41     private String JavaDoc fErrorMessage;
42     private ImageDescriptorRegistry fImageRegistry= JavaPlugin.getImageDescriptorRegistry();
43     private PromoteTempToFieldRefactoring fRefactoring;
44
45     public FieldNameProcessor(String JavaDoc[] guessedFieldNames, PromoteTempToFieldRefactoring refactoring) {
46         fRefactoring= refactoring;
47         fFieldNameProposals= refactoring.guessFieldNames();
48         Arrays.sort(fFieldNameProposals);
49     }
50
51     /*
52      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
53      */

54     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
55         Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
56
return null;
57     }
58
59     /*
60      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
61      */

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

70     public char[] getCompletionProposalAutoActivationCharacters() {
71         return null;
72     }
73
74     /*
75      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
76      */

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

84     public String JavaDoc getErrorMessage() {
85         return fErrorMessage;
86     }
87
88     /*
89      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
90      */

91     public IContextInformationValidator getContextInformationValidator() {
92         return null; //no context
93
}
94
95     /*
96      * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeContextInformation(org.eclipse.jface.contentassist.IContentAssistSubject, int)
97      */

98     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
99         return null; //no context
100
}
101
102     /*
103      * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeCompletionProposals(org.eclipse.jface.contentassist.IContentAssistSubject, int)
104      */

105     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) {
106         if (fFieldNameProposals.length == 0)
107             return null;
108         String JavaDoc input= contentAssistSubject.getDocument().get();
109         
110         ArrayList JavaDoc proposals= new ArrayList JavaDoc();
111         String JavaDoc prefix= input.substring(0, documentOffset);
112         ImageDescriptor imageDescriptor= JavaElementImageProvider.getFieldImageDescriptor(false, fRefactoring.getVisibility());
113         Image image= fImageRegistry.get(imageDescriptor);
114         for (int i= 0; i < fFieldNameProposals.length; i++) {
115             String JavaDoc tempName= fFieldNameProposals[i];
116             if (tempName.length() == 0 || ! tempName.startsWith(prefix))
117                 continue;
118             JavaCompletionProposal proposal= new JavaCompletionProposal(tempName, 0, input.length(), image, tempName, 0);
119             proposals.add(proposal);
120         }
121         fErrorMessage= proposals.size() > 0 ? null : JavaUIMessages.JavaEditor_codeassist_noCompletions;
122         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
123     }
124
125 }
126
Popular Tags