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;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.Region;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
23 public class TemplateEngine {
25 /** The context type. */
26 private ContextType fContextType;
27 /** The result proposals. */
28 private ArrayList fProposals= new ArrayList();
31 * Creates the template engine for a particular context type.
32 * See <code>TemplateContext</code> for supported context types.
34 public TemplateEngine(ContextType contextType) {
35 // Assert.isNotNull(contextType);
36 fContextType= contextType;
40 * Empties the collector.
42 * @param viewer the text viewer
43 * @param unit the compilation unit (may be <code>null</code>)
50 * Returns the array of matching templates.
52 public IPHPCompletionProposal[] getResults() {
53 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
57 * Inspects the context of the compilation unit around <code>completionPosition</code>
58 * and feeds the collector with proposals.
59 * @param viewer the text viewer
60 * @param completionPosition the context position in the document of the text viewer
61 * @param compilationUnit the compilation unit (may be <code>null</code>)
63 public void complete(ITextViewer viewer, int completionPosition)
64 //,ICompilationUnit compilationUnit)
65 //hrows JavaModelException
67 IDocument document= viewer.getDocument();
70 // if (LinkedPositionManager.hasActiveManager(document))
73 if (!(fContextType instanceof CompilationUnitContextType))
76 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
77 DocumentTemplateContext context= (DocumentTemplateContext) fContextType.createContext();
78 int start= context.getStart();
79 int end= context.getEnd();
80 IRegion region= new Region(start, end - start);
82 Template[] templates= Templates.getInstance().getTemplates();
83 for (int i= 0; i != templates.length; i++)
84 if (context.canEvaluate(templates[i]))
85 fProposals.add(new TemplateProposal(templates[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_OBJS_TEMPLATE)));