1 11 package org.eclipse.jdt.internal.ui.refactoring.contentassist; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 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 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 63 public JavaPackageFragmentRootCompletionProcessor(IPackageFragmentRoot packageFragmentRoot) { 64 fPackageFragmentRoot= packageFragmentRoot; 65 fComparator= new CompletionProposalComparator(); 66 67 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 68 String triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA); 69 fProposalAutoActivationSet = triggers.toCharArray(); 70 } 71 72 75 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 76 Assert.isTrue(false, "ITextViewer not supported"); return null; 78 } 79 80 83 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 84 Assert.isTrue(false, "ITextViewer not supported"); return null; 86 } 87 88 91 public char[] getCompletionProposalAutoActivationCharacters() { 92 return fProposalAutoActivationSet; 93 } 94 95 98 public char[] getContextInformationAutoActivationCharacters() { 99 return null; 100 } 101 102 105 public String getErrorMessage() { 106 return null; 107 } 108 109 112 public IContextInformationValidator getContextInformationValidator() { 113 return null; } 115 116 119 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, 120 int documentOffset) { 121 return null; 122 } 123 124 127 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 128 String 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 input) { 135 ArrayList proposals= new ArrayList (); 136 String 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 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 |