1 11 package org.eclipse.jdt.internal.debug.ui.snippeteditor; 12 13 14 import java.util.Arrays ; 15 16 import org.eclipse.jdt.core.JavaModelException; 17 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 18 import org.eclipse.jdt.internal.ui.JavaPlugin; 19 import org.eclipse.jdt.internal.ui.text.java.JavaParameterListValidator; 20 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine; 21 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal; 22 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector; 23 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 25 import org.eclipse.jface.dialogs.ErrorDialog; 26 import org.eclipse.jface.text.ITextViewer; 27 import org.eclipse.jface.text.contentassist.ICompletionProposal; 28 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 29 import org.eclipse.jface.text.contentassist.IContextInformation; 30 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 31 import org.eclipse.jface.text.templates.TemplateContextType; 32 import org.eclipse.swt.widgets.Shell; 33 34 37 public class JavaSnippetCompletionProcessor implements IContentAssistProcessor { 38 39 private CompletionProposalCollector fCollector; 40 private JavaSnippetEditor fEditor; 41 private IContextInformationValidator fValidator; 42 private TemplateEngine fTemplateEngine; 43 private CompletionProposalComparator fComparator; 44 private String fErrorMessage; 45 46 private char[] fProposalAutoActivationSet; 47 48 public JavaSnippetCompletionProcessor(JavaSnippetEditor editor) { 49 fEditor= editor; 50 TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("java"); if (contextType != null) { 52 fTemplateEngine= new TemplateEngine(contextType); 53 } 54 55 fComparator= new CompletionProposalComparator(); 56 } 57 58 61 public String getErrorMessage() { 62 return fErrorMessage; 63 } 64 65 protected void setErrorMessage(String message) { 66 if (message != null && message.length() == 0) { 67 message = null; 68 } 69 fErrorMessage = message; 70 } 71 72 75 public IContextInformationValidator getContextInformationValidator() { 76 if (fValidator == null) { 77 fValidator= new JavaParameterListValidator(); 78 } 79 return fValidator; 80 } 81 82 85 public char[] getContextInformationAutoActivationCharacters() { 86 return null; 87 } 88 89 92 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { 93 return null; 94 } 95 96 99 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int position) { 100 try { 101 setErrorMessage(null); 102 try { 103 fCollector = new CompletionProposalCollector(fEditor.getJavaProject()); 104 fEditor.codeComplete(fCollector); 105 } catch (JavaModelException x) { 106 Shell shell= viewer.getTextWidget().getShell(); 107 ErrorDialog.openError(shell, SnippetMessages.getString("CompletionProcessor.errorTitle"), SnippetMessages.getString("CompletionProcessor.errorMessage"), x.getStatus()); JDIDebugUIPlugin.log(x); 109 } 110 111 IJavaCompletionProposal[] results= fCollector.getJavaCompletionProposals(); 112 113 if (fTemplateEngine != null) { 114 fTemplateEngine.reset(); 115 fTemplateEngine.complete(viewer, position, null); 116 117 TemplateProposal[] templateResults= fTemplateEngine.getResults(); 118 119 IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length]; 121 System.arraycopy(templateResults, 0, total, 0, templateResults.length); 122 System.arraycopy(results, 0, total, templateResults.length, results.length); 123 results= total; 124 } 125 return order(results); 126 } finally { 127 setErrorMessage(fCollector.getErrorMessage()); 128 fCollector = null; 129 } 130 } 131 132 135 private ICompletionProposal[] order(IJavaCompletionProposal[] proposals) { 136 Arrays.sort(proposals, fComparator); 137 return proposals; 138 } 139 140 143 public char[] getCompletionProposalAutoActivationCharacters() { 144 return fProposalAutoActivationSet; 145 } 146 147 153 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) { 154 fProposalAutoActivationSet= activationSet; 155 } 156 157 162 public void orderProposalsAlphabetically(boolean order) { 163 fComparator.setOrderAlphabetically(order); 164 } 165 } 166 | Popular Tags |