/* * (c) Copyright IBM Corp. 2000, 2001. * All Rights Reserved. */ package net.sourceforge.phpdt.internal.ui.text.template; import java.util.ArrayList; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.Region; //import org.eclipse.jdt.core.ICompilationUnit; //import org.eclipse.jdt.core.JavaModelException; //import org.eclipse.jdt.internal.corext.Assert; import net.sourceforge.phpdt.internal.corext.template.ContextType; import net.sourceforge.phpdt.internal.corext.template.Template; import net.sourceforge.phpdt.internal.corext.template.Templates; import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContext; import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContextType; //import org.eclipse.jdt.internal.ui.JavaPluginImages; import net.sourceforge.phpdt.internal.ui.text.java.IJavaCompletionProposal; //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager; public class TemplateEngine { /** The context type. */ private ContextType fContextType; /** The result proposals. */ private ArrayList fProposals= new ArrayList(); /** * Creates the template engine for a particular context type. * See TemplateContext for supported context types. */ public TemplateEngine(ContextType contextType) { // Assert.isNotNull(contextType); fContextType= contextType; } /** * Empties the collector. * * @param viewer the text viewer * @param unit the compilation unit (may be null) */ public void reset() { fProposals.clear(); } /** * Returns the array of matching templates. */ public IJavaCompletionProposal[] getResults() { return (IJavaCompletionProposal[]) fProposals.toArray(new IJavaCompletionProposal[fProposals.size()]); } /** * Inspects the context of the compilation unit around completionPosition * and feeds the collector with proposals. * @param viewer the text viewer * @param completionPosition the context position in the document of the text viewer * @param compilationUnit the compilation unit (may be null) */ public void complete(ITextViewer viewer, int completionPosition) //,ICompilationUnit compilationUnit) //hrows JavaModelException { IDocument document= viewer.getDocument(); // prohibit recursion // if (LinkedPositionManager.hasActiveManager(document)) // return; if (!(fContextType instanceof CompilationUnitContextType)) return; ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit); CompilationUnitContext context= (CompilationUnitContext) fContextType.createContext(); int start= context.getStart(); int end= context.getEnd(); IRegion region= new Region(start, end - start); Template[] templates= Templates.getInstance().getTemplates(); for (int i= 0; i != templates.length; i++) if (context.canEvaluate(templates[i])) fProposals.add(new TemplateProposal(templates[i], context, region, viewer)); //luginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE))); } }