2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.IRegion;
11 import org.eclipse.jface.text.ITextViewer;
12 import org.eclipse.jface.text.Region;
14 //import org.eclipse.jdt.core.ICompilationUnit;
15 //import org.eclipse.jdt.core.JavaModelException;
17 //import org.eclipse.jdt.internal.corext.Assert;
18 import net.sourceforge.phpdt.internal.corext.template.ContextType;
19 import net.sourceforge.phpdt.internal.corext.template.Template;
20 import net.sourceforge.phpdt.internal.corext.template.Templates;
21 import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContext;
22 import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContextType;
23 //import org.eclipse.jdt.internal.ui.JavaPluginImages;
24 import net.sourceforge.phpdt.internal.ui.text.java.IJavaCompletionProposal;
25 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
27 public class TemplateEngine {
29 /** The context type. */
30 private ContextType fContextType;
31 /** The result proposals. */
32 private ArrayList fProposals= new ArrayList();
35 * Creates the template engine for a particular context type.
36 * See <code>TemplateContext</code> for supported context types.
38 public TemplateEngine(ContextType contextType) {
39 // Assert.isNotNull(contextType);
40 fContextType= contextType;
44 * Empties the collector.
46 * @param viewer the text viewer
47 * @param unit the compilation unit (may be <code>null</code>)
54 * Returns the array of matching templates.
56 public IJavaCompletionProposal[] getResults() {
57 return (IJavaCompletionProposal[]) fProposals.toArray(new IJavaCompletionProposal[fProposals.size()]);
61 * Inspects the context of the compilation unit around <code>completionPosition</code>
62 * and feeds the collector with proposals.
63 * @param viewer the text viewer
64 * @param completionPosition the context position in the document of the text viewer
65 * @param compilationUnit the compilation unit (may be <code>null</code>)
67 public void complete(ITextViewer viewer, int completionPosition)
68 //,ICompilationUnit compilationUnit)
69 //hrows JavaModelException
71 IDocument document= viewer.getDocument();
74 // if (LinkedPositionManager.hasActiveManager(document))
77 if (!(fContextType instanceof CompilationUnitContextType))
80 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
81 CompilationUnitContext context= (CompilationUnitContext) fContextType.createContext();
82 int start= context.getStart();
83 int end= context.getEnd();
84 IRegion region= new Region(start, end - start);
86 Template[] templates= Templates.getInstance().getTemplates();
87 for (int i= 0; i != templates.length; i++)
88 if (context.canEvaluate(templates[i]))
89 fProposals.add(new TemplateProposal(templates[i], context, region, viewer)); //luginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE)));