2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
9 import net.sourceforge.phpdt.internal.corext.template.ContextType;
10 import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
11 import net.sourceforge.phpdt.internal.corext.template.Template;
12 import net.sourceforge.phpdt.internal.corext.template.Templates;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
15 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.Region;
20 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
22 public class TemplateEngine {
24 /** The context type. */
25 private ContextType fContextType;
26 /** The result proposals. */
27 private ArrayList fProposals= new ArrayList();
30 * Creates the template engine for a particular context type.
31 * See <code>TemplateContext</code> for supported context types.
33 public TemplateEngine(ContextType contextType) {
34 // Assert.isNotNull(contextType);
35 fContextType= contextType;
39 * Empties the collector.
41 * @param viewer the text viewer
42 * @param unit the compilation unit (may be <code>null</code>)
49 * Returns the array of matching templates.
51 public IPHPCompletionProposal[] getResults() {
52 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
56 * Inspects the context of the compilation unit around <code>completionPosition</code>
57 * and feeds the collector with proposals.
58 * @param viewer the text viewer
59 * @param completionPosition the context position in the document of the text viewer
60 * @param compilationUnit the compilation unit (may be <code>null</code>)
62 public void complete(ITextViewer viewer, int completionPosition)
63 //,ICompilationUnit compilationUnit)
64 //hrows JavaModelException
66 IDocument document= viewer.getDocument();
69 // if (LinkedPositionManager.hasActiveManager(document))
72 if (!(fContextType instanceof CompilationUnitContextType))
75 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
76 DocumentTemplateContext context= (DocumentTemplateContext) fContextType.createContext();
77 int start= context.getStart();
78 int end= context.getEnd();
79 IRegion region= new Region(start, end - start);
81 Template[] templates= Templates.getInstance().getTemplates();
82 for (int i= 0; i != templates.length; i++)
83 if (context.canEvaluate(templates[i]))
84 fProposals.add(new TemplateProposal(templates[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_OBJS_TEMPLATE)));