blob: 13867a3304ff1a444f12c60c35fcd076dfba10ad [file] [log] [blame]
package org.eclipse.jdt.internal.debug.ui.snippeteditor;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import java.util.Arrays;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.template.ContextType;
import org.eclipse.jdt.internal.corext.template.ContextTypeRegistry;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComparator;
import org.eclipse.jdt.internal.ui.text.java.JavaParameterListValidator;
import org.eclipse.jdt.internal.ui.text.java.ResultCollector;
import org.eclipse.jdt.internal.ui.text.template.TemplateEngine;
/**
* Java snippet completion processor.
*/
public class JavaSnippetCompletionProcessor implements IContentAssistProcessor {
private ResultCollector fCollector;
private JavaSnippetEditor fEditor;
private IContextInformationValidator fValidator;
private TemplateEngine fTemplateEngine;
private JavaCompletionProposalComparator fComparator;
private char[] fProposalAutoActivationSet;
public JavaSnippetCompletionProcessor(JavaSnippetEditor editor) {
fCollector= new ResultCollector();
fEditor= editor;
ContextType contextType= ContextTypeRegistry.getInstance().getContextType("java"); //$NON-NLS-1$
if (contextType != null) {
fTemplateEngine= new TemplateEngine(contextType);
}
fComparator= new JavaCompletionProposalComparator();
}
/**
* @see IContentAssistProcessor#getErrorMessage()
*/
public String getErrorMessage() {
return fCollector.getErrorMessage();
}
/**
* @see IContentAssistProcessor#getContextInformationValidator()
*/
public IContextInformationValidator getContextInformationValidator() {
if (fValidator == null) {
fValidator= new JavaParameterListValidator();
}
return fValidator;
}
/**
* @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
*/
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
/**
* @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
*/
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
return null;
}
/**
* @see IContentAssistProcessor#computeProposals(ITextViewer, int)
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int position) {
try {
fCollector.reset(position, fEditor.findJavaProject(), null);
fEditor.codeComplete(fCollector);
} catch (JavaModelException x) {
Shell shell= viewer.getTextWidget().getShell();
ErrorDialog.openError(shell, SnippetMessages.getString("CompletionProcessor.errorTitle"), SnippetMessages.getString("CompletionProcessor.errorMessage"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
JDIDebugUIPlugin.log(x);
}
IJavaCompletionProposal[] results= fCollector.getResults();
if (fTemplateEngine != null) {
try {
fTemplateEngine.reset();
fTemplateEngine.complete(viewer, position, null);
} catch (JavaModelException x) {
JDIDebugUIPlugin.log(x);
Shell shell= viewer.getTextWidget().getShell();
ErrorDialog.openError(shell, SnippetMessages.getString("CompletionProcessor.errorTitle"), SnippetMessages.getString("CompletionProcessor.errorMessage"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
}
IJavaCompletionProposal[] templateResults= fTemplateEngine.getResults();
// concatenate arrays
IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length];
System.arraycopy(templateResults, 0, total, 0, templateResults.length);
System.arraycopy(results, 0, total, templateResults.length, results.length);
results= total;
}
return order(results);
}
/**
* Order the given proposals.
*/
private ICompletionProposal[] order(IJavaCompletionProposal[] proposals) {
Arrays.sort(proposals, fComparator);
return proposals;
}
/**
* @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
*/
public char[] getCompletionProposalAutoActivationCharacters() {
return fProposalAutoActivationSet;
}
/**
* Sets this processor's set of characters triggering the activation of the
* completion proposal computation.
*
* @param activationSet the activation set
*/
public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
fProposalAutoActivationSet= activationSet;
}
/**
* Tells this processor to order the proposals alphabetically.
*
* @param order <code>true</code> if proposals should be ordered.
*/
public void orderProposalsAlphabetically(boolean order) {
fComparator.setOrderAlphabetically(order);
}
}