e2e144e88e6374b5715e82796acbafb619936e71
[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
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;
22
23 public class TemplateEngine {
24
25         /** The context type. */
26         private ContextType fContextType;
27         /** The result proposals. */
28         private ArrayList fProposals= new ArrayList();
29
30         /**
31          * Creates the template engine for a particular context type.
32          * See <code>TemplateContext</code> for supported context types.
33          */
34         public TemplateEngine(ContextType contextType) {
35         //      Assert.isNotNull(contextType);
36                 fContextType= contextType;
37         }
38
39         /**
40          * Empties the collector.
41          * 
42          * @param viewer the text viewer  
43          * @param unit   the compilation unit (may be <code>null</code>)
44          */
45         public void reset() {
46                 fProposals.clear();
47         }
48
49         /**
50          * Returns the array of matching templates.
51          */
52         public IPHPCompletionProposal[] getResults() {
53                 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
54         }
55
56         /**
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>)
62          */
63         public void complete(ITextViewer viewer, int completionPosition)
64   //,ICompilationUnit compilationUnit)
65         //hrows JavaModelException
66         {
67             IDocument document= viewer.getDocument();
68             
69                 // prohibit recursion
70 //              if (LinkedPositionManager.hasActiveManager(document))
71 //                      return;
72
73                 if (!(fContextType instanceof CompilationUnitContextType))
74                         return;
75                 
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);
81
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))); 
86         }
87
88 }
89