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.viewers.ILabelProvider; 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.JavaElementLabelProvider; 38 import org.eclipse.jdt.ui.PreferenceConstants; 39 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 40 41 import org.eclipse.jdt.internal.ui.JavaPlugin; 42 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; 43 44 public class JavaPackageCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { 45 46 private IPackageFragmentRoot fPackageFragmentRoot; 47 private CompletionProposalComparator fComparator; 48 private ILabelProvider fLabelProvider; 49 50 private char[] fProposalAutoActivationSet; 51 52 56 public JavaPackageCompletionProcessor() { 57 this(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS)); 58 } 59 60 66 public JavaPackageCompletionProcessor(ILabelProvider labelProvider) { 67 fComparator= new CompletionProposalComparator(); 68 fLabelProvider= labelProvider; 69 70 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 71 String triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA); 72 fProposalAutoActivationSet = triggers.toCharArray(); 73 } 74 75 78 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 79 Assert.isTrue(false, "ITextViewer not supported"); return null; 81 } 82 83 86 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 87 Assert.isTrue(false, "ITextViewer not supported"); return null; 89 } 90 91 94 public char[] getCompletionProposalAutoActivationCharacters() { 95 return fProposalAutoActivationSet; 96 } 97 98 101 public char[] getContextInformationAutoActivationCharacters() { 102 return null; 103 } 104 105 108 public String getErrorMessage() { 109 return null; 110 } 111 112 115 public IContextInformationValidator getContextInformationValidator() { 116 return null; } 118 119 122 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, 123 int documentOffset) { 124 return null; 125 } 126 127 130 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 131 if (fPackageFragmentRoot == null) 132 return null; 133 String input= contentAssistSubjectControl.getDocument().get(); 134 ICompletionProposal[] proposals= createPackagesProposals(documentOffset, input); 135 Arrays.sort(proposals, fComparator); 136 return proposals; 137 } 138 139 public void setPackageFragmentRoot(IPackageFragmentRoot packageFragmentRoot) { 140 fPackageFragmentRoot= packageFragmentRoot; 141 } 142 143 private ICompletionProposal[] createPackagesProposals(int documentOffset, String input) { 144 ArrayList proposals= new ArrayList (); 145 String prefix= input.substring(0, documentOffset); 146 try { 147 IJavaElement[] packageFragments= fPackageFragmentRoot.getChildren(); 148 for (int i= 0; i < packageFragments.length; i++) { 149 IPackageFragment pack= (IPackageFragment) packageFragments[i]; 150 String packName= pack.getElementName(); 151 if (packName.length() == 0 || ! packName.startsWith(prefix)) 152 continue; 153 Image image= fLabelProvider.getImage(pack); 154 JavaCompletionProposal proposal= new JavaCompletionProposal(packName, 0, input.length(), image, fLabelProvider.getText(pack), 0); 155 proposals.add(proposal); 156 } 157 } catch (JavaModelException e) { 158 } 160 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); 161 } 162 } 163 | Popular Tags |