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.preference.IPreferenceStore;
022: import org.eclipse.jface.viewers.ILabelProvider;
023:
024: import org.eclipse.jface.text.ITextViewer;
025: import org.eclipse.jface.text.contentassist.ICompletionProposal;
026: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
027: import org.eclipse.jface.text.contentassist.IContextInformation;
028: import org.eclipse.jface.text.contentassist.IContextInformationValidator;
029:
030: import org.eclipse.jdt.core.IJavaElement;
031: import org.eclipse.jdt.core.IPackageFragment;
032: import org.eclipse.jdt.core.IPackageFragmentRoot;
033: import org.eclipse.jdt.core.JavaModelException;
034:
035: import org.eclipse.jdt.ui.JavaElementLabelProvider;
036: import org.eclipse.jdt.ui.PreferenceConstants;
037: import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
038:
039: import org.eclipse.jdt.internal.ui.JavaPlugin;
040: import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
041:
042: public class JavaPackageCompletionProcessor implements
043: IContentAssistProcessor, ISubjectControlContentAssistProcessor {
044:
045: private IPackageFragmentRoot fPackageFragmentRoot;
046: private CompletionProposalComparator fComparator;
047: private ILabelProvider fLabelProvider;
048:
049: private char[] fProposalAutoActivationSet;
050:
051: /**
052: * Creates a <code>JavaPackageCompletionProcessor</code>.
053: * The completion context must be set via {@link #setPackageFragmentRoot(IPackageFragmentRoot)}.
054: */
055: public JavaPackageCompletionProcessor() {
056: this (new JavaElementLabelProvider(
057: JavaElementLabelProvider.SHOW_SMALL_ICONS));
058: }
059:
060: /**
061: * Creates a <code>JavaPackageCompletionProcessor</code>.
062: * The Processor uses the given <code>ILabelProvider</code> to show text and icons for the
063: * possible completions.
064: * @param labelProvider Used for the popups.
065: */
066: public JavaPackageCompletionProcessor(ILabelProvider labelProvider) {
067: fComparator = new CompletionProposalComparator();
068: fLabelProvider = labelProvider;
069:
070: IPreferenceStore preferenceStore = JavaPlugin.getDefault()
071: .getPreferenceStore();
072: String triggers = preferenceStore
073: .getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
074: fProposalAutoActivationSet = triggers.toCharArray();
075: }
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
079: */
080: public ICompletionProposal[] computeCompletionProposals(
081: ITextViewer viewer, int documentOffset) {
082: Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
083: return null;
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
088: */
089: public IContextInformation[] computeContextInformation(
090: ITextViewer viewer, int documentOffset) {
091: Assert.isTrue(false, "ITextViewer not supported"); //$NON-NLS-1$
092: return null;
093: }
094:
095: /*
096: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
097: */
098: public char[] getCompletionProposalAutoActivationCharacters() {
099: return fProposalAutoActivationSet;
100: }
101:
102: /*
103: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
104: */
105: public char[] getContextInformationAutoActivationCharacters() {
106: return null;
107: }
108:
109: /*
110: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
111: */
112: public String getErrorMessage() {
113: return null;
114: }
115:
116: /*
117: * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
118: */
119: public IContextInformationValidator getContextInformationValidator() {
120: return null; //no context
121: }
122:
123: /*
124: * @see ISubjectControlContentAssistProcessor#computeContextInformation(IContentAssistSubjectControl, int)
125: */
126: public IContextInformation[] computeContextInformation(
127: IContentAssistSubjectControl contentAssistSubjectControl,
128: int documentOffset) {
129: return null;
130: }
131:
132: /*
133: * @see ISubjectControlContentAssistProcessor#computeCompletionProposals(IContentAssistSubjectControl, int)
134: */
135: public ICompletionProposal[] computeCompletionProposals(
136: IContentAssistSubjectControl contentAssistSubjectControl,
137: int documentOffset) {
138: if (fPackageFragmentRoot == null)
139: return null;
140: String input = contentAssistSubjectControl.getDocument().get();
141: ICompletionProposal[] proposals = createPackagesProposals(
142: documentOffset, input);
143: Arrays.sort(proposals, fComparator);
144: return proposals;
145: }
146:
147: public void setPackageFragmentRoot(
148: IPackageFragmentRoot packageFragmentRoot) {
149: fPackageFragmentRoot = packageFragmentRoot;
150: }
151:
152: private ICompletionProposal[] createPackagesProposals(
153: int documentOffset, String input) {
154: ArrayList proposals = new ArrayList();
155: String prefix = input.substring(0, documentOffset);
156: try {
157: IJavaElement[] packageFragments = fPackageFragmentRoot
158: .getChildren();
159: for (int i = 0; i < packageFragments.length; i++) {
160: IPackageFragment pack = (IPackageFragment) packageFragments[i];
161: String packName = pack.getElementName();
162: if (packName.length() == 0
163: || !packName.startsWith(prefix))
164: continue;
165: Image image = fLabelProvider.getImage(pack);
166: JavaCompletionProposal proposal = new JavaCompletionProposal(
167: packName, 0, input.length(), image,
168: fLabelProvider.getText(pack), 0);
169: proposals.add(proposal);
170: }
171: } catch (JavaModelException e) {
172: //fPackageFragmentRoot is not a proper root -> no proposals
173: }
174: return (ICompletionProposal[]) proposals
175: .toArray(new ICompletionProposal[proposals.size()]);
176: }
177: }
|