1 11 package org.eclipse.jdt.internal.ui.refactoring.contentassist; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.core.resources.ResourcesPlugin; 20 21 import org.eclipse.swt.graphics.Image; 22 23 import org.eclipse.jface.contentassist.IContentAssistSubjectControl; 24 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor; 25 import org.eclipse.jface.preference.IPreferenceStore; 26 27 import org.eclipse.jface.text.ITextViewer; 28 import org.eclipse.jface.text.contentassist.ICompletionProposal; 29 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 30 import org.eclipse.jface.text.contentassist.IContextInformation; 31 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 32 33 import org.eclipse.jdt.core.IJavaModel; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.IPackageFragmentRoot; 36 import org.eclipse.jdt.core.JavaCore; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 40 import org.eclipse.jdt.ui.JavaElementLabelProvider; 41 import org.eclipse.jdt.ui.PreferenceConstants; 42 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 43 44 import org.eclipse.jdt.internal.ui.JavaPlugin; 45 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; 46 47 51 public class JavaSourcePackageFragmentRootCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { 52 53 private char[] fProposalAutoActivationSet; 54 private IJavaModel fRoot; 55 private CompletionProposalComparator fComparator; 56 private JavaElementLabelProvider fLabelProvider; 57 58 public JavaSourcePackageFragmentRootCompletionProcessor() { 59 fRoot= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 60 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 61 String triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA); 62 fProposalAutoActivationSet= triggers.toCharArray(); 63 fComparator= new CompletionProposalComparator(); 64 fLabelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS); 65 } 66 67 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 68 Assert.isTrue(false, "ITextViewer not supported"); return null; 70 } 71 72 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 73 Assert.isTrue(false, "ITextViewer not supported"); return null; 75 } 76 77 public char[] getCompletionProposalAutoActivationCharacters() { 78 return fProposalAutoActivationSet; 79 } 80 81 public char[] getContextInformationAutoActivationCharacters() { 82 return null; 83 } 84 85 public String getErrorMessage() { 86 return null; 87 } 88 89 public IContextInformationValidator getContextInformationValidator() { 90 return null; 91 } 92 93 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) { 94 if (fRoot == null) { 95 return null; 96 } 97 98 String input= contentAssistSubject.getDocument().get(); 99 String prefix= input.substring(0, documentOffset); 100 101 ICompletionProposal[] proposals= createSourcePackageFragmentRootProposals(prefix, input.length()); 102 Arrays.sort(proposals, fComparator); 103 return proposals; 104 } 105 106 111 private ICompletionProposal[] createSourcePackageFragmentRootProposals(String prefix, int replacementLength) { 112 List proposals= new ArrayList (); 113 try { 114 IJavaProject[] projects= fRoot.getJavaProjects(); 115 for (int i= 0; i < projects.length; i++) { 116 IJavaProject project= projects[i]; 117 IPackageFragmentRoot[] roots= project.getPackageFragmentRoots(); 118 for (int j= 0; j < roots.length; j++) { 119 IPackageFragmentRoot root= roots[j]; 120 if (root.exists() && (root.getKind() == IPackageFragmentRoot.K_SOURCE)) { 121 String name= root.getPath().toString(); 122 if (name.length() > 1) { 123 name= name.substring(1); 124 } 125 if (name.startsWith(prefix)) { 126 Image image= fLabelProvider.getImage(root); 127 JavaCompletionProposal proposal= new JavaCompletionProposal(name, 0, replacementLength, image, 128 name, 0); 129 proposals.add(proposal); 130 } 131 } 132 } 133 } 134 } catch (JavaModelException e) { 135 } 137 138 return (ICompletionProposal[])proposals.toArray(new ICompletionProposal[proposals.size()]); 139 } 140 141 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubject, int documentOffset) { 142 return null; 143 } 144 145 } 146 | Popular Tags |