001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.contentassist;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014:
015: import org.eclipse.core.runtime.Assert;
016:
017: import org.eclipse.swt.graphics.Image;
018:
019: import org.eclipse.jface.contentassist.IContentAssistSubjectControl;
020: import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.jface.text.ITextViewer;
023: import org.eclipse.jface.text.contentassist.ICompletionProposal;
024: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
025: import org.eclipse.jface.text.contentassist.IContextInformation;
026: import org.eclipse.jface.text.contentassist.IContextInformationValidator;
027:
028: import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring;
029:
030: import org.eclipse.jdt.internal.ui.JavaPlugin;
031: import org.eclipse.jdt.internal.ui.JavaUIMessages;
032: import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
033: import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
034: import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
035:
036: public class FieldNameProcessor implements IContentAssistProcessor,
037: ISubjectControlContentAssistProcessor {
038:
039: private String[] fFieldNameProposals;
040: private String fErrorMessage;
041: private ImageDescriptorRegistry fImageRegistry = JavaPlugin
042: .getImageDescriptorRegistry();
043: private PromoteTempToFieldRefactoring fRefactoring;
044:
045: public FieldNameProcessor(String[] guessedFieldNames,
046: PromoteTempToFieldRefactoring refactoring) {
047: fRefactoring = refactoring;
048: fFieldNameProposals = refactoring.guessFieldNames();
049: Arrays.sort(fFieldNameProposals);
050: }
051:
052: /*
053: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
054: */
055: public ICompletionProposal[] computeCompletionProposals(
056: ITextViewer viewer, int documentOffset) {
057: Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
058: return null;
059: }
060:
061: /*
062: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
063: */
064: public IContextInformation[] computeContextInformation(
065: ITextViewer viewer, int documentOffset) {
066: Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
067: return null;
068: }
069:
070: /*
071: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
072: */
073: public char[] getCompletionProposalAutoActivationCharacters() {
074: return null;
075: }
076:
077: /*
078: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
079: */
080: public char[] getContextInformationAutoActivationCharacters() {
081: return null; //no context
082: }
083:
084: /*
085: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
086: */
087: public String getErrorMessage() {
088: return fErrorMessage;
089: }
090:
091: /*
092: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
093: */
094: public IContextInformationValidator getContextInformationValidator() {
095: return null; //no context
096: }
097:
098: /*
099: * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeContextInformation(org.eclipse.jface.contentassist.IContentAssistSubject, int)
100: */
101: public IContextInformation[] computeContextInformation(
102: IContentAssistSubjectControl contentAssistSubject,
103: int documentOffset) {
104: return null; //no context
105: }
106:
107: /*
108: * @see org.eclipse.jface.contentassist.IContentAssistProcessorExtension#computeCompletionProposals(org.eclipse.jface.contentassist.IContentAssistSubject, int)
109: */
110: public ICompletionProposal[] computeCompletionProposals(
111: IContentAssistSubjectControl contentAssistSubject,
112: int documentOffset) {
113: if (fFieldNameProposals.length == 0)
114: return null;
115: String input = contentAssistSubject.getDocument().get();
116:
117: ArrayList proposals = new ArrayList();
118: String prefix = input.substring(0, documentOffset);
119: ImageDescriptor imageDescriptor = JavaElementImageProvider
120: .getFieldImageDescriptor(false, fRefactoring
121: .getVisibility());
122: Image image = fImageRegistry.get(imageDescriptor);
123: for (int i = 0; i < fFieldNameProposals.length; i++) {
124: String tempName = fFieldNameProposals[i];
125: if (tempName.length() == 0 || !tempName.startsWith(prefix))
126: continue;
127: JavaCompletionProposal proposal = new JavaCompletionProposal(
128: tempName, 0, input.length(), image, tempName, 0);
129: proposals.add(proposal);
130: }
131: fErrorMessage = proposals.size() > 0 ? null
132: : JavaUIMessages.JavaEditor_codeassist_noCompletions;
133: return (ICompletionProposal[]) proposals
134: .toArray(new ICompletionProposal[proposals.size()]);
135: }
136:
137: }
|