b946e5ff8c4370475c32d04164d19ba5521d9f6e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / TemplateEngine.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
7 import java.util.ArrayList;
8
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;
21
22 public class TemplateEngine {
23
24         /** The context type. */
25         private ContextType fContextType;
26         /** The result proposals. */
27         private ArrayList fProposals= new ArrayList();
28
29         /**
30          * Creates the template engine for a particular context type.
31          * See <code>TemplateContext</code> for supported context types.
32          */
33         public TemplateEngine(ContextType contextType) {
34         //      Assert.isNotNull(contextType);
35                 fContextType= contextType;
36         }
37
38         /**
39          * Empties the collector.
40          * 
41          * @param viewer the text viewer  
42          * @param unit   the compilation unit (may be <code>null</code>)
43          */
44         public void reset() {
45                 fProposals.clear();
46         }
47
48         /**
49          * Returns the array of matching templates.
50          */
51         public IPHPCompletionProposal[] getResults() {
52                 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
53         }
54
55         /**
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>)
61          */
62         public void complete(ITextViewer viewer, int completionPosition)
63   //,ICompilationUnit compilationUnit)
64         //hrows JavaModelException
65         {
66             IDocument document= viewer.getDocument();
67             
68                 // prohibit recursion
69 //              if (LinkedPositionManager.hasActiveManager(document))
70 //                      return;
71
72                 if (!(fContextType instanceof CompilationUnitContextType))
73                         return;
74                 
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);
80
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))); 
85         }
86
87 }
88