2dcd584560788f7276865d7f324b8362e28868be
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInEngine.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.php.PHPUnitContext;
11 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
12 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
13 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.text.ITextViewer;
17 import org.eclipse.jface.text.Region;
18 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
19
20 public class BuiltInEngine {
21
22         /** The context type. */
23         private ContextType fContextType;
24         /** The result proposals. */
25         private ArrayList fProposals= new ArrayList();
26
27         /**
28          * Creates the template engine for a particular context type.
29          * See <code>TemplateContext</code> for supported context types.
30          */
31         public BuiltInEngine(ContextType contextType) {
32         //      Assert.isNotNull(contextType);
33                 fContextType= contextType;
34         }
35
36         /**
37          * Empties the collector.
38          * 
39          * @param viewer the text viewer  
40          * @param unit   the compilation unit (may be <code>null</code>)
41          */
42         public void reset() {
43                 fProposals.clear();
44         }
45
46         /**
47          * Returns the array of matching templates.
48          */
49         public IPHPCompletionProposal[] getResults() {
50                 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
51         }
52
53         /**
54          * Inspects the context of the compilation unit around <code>completionPosition</code>
55          * and feeds the collector with proposals.
56          * @param viewer the text viewer
57          * @param completionPosition the context position in the document of the text viewer
58          * @param compilationUnit the compilation unit (may be <code>null</code>)
59          */
60         public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
61   //,ICompilationUnit compilationUnit)
62         //hrows JavaModelException
63         {
64             IDocument document= viewer.getDocument();
65             
66                 // prohibit recursion
67 //              if (LinkedPositionManager.hasActiveManager(document))
68 //                      return;
69
70                 if (!(fContextType instanceof CompilationUnitContextType))
71                         return;
72                 
73                 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition);//mpilationUnit);
74                 PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
75                 int start= context.getStart();
76                 int end= context.getEnd();
77                 IRegion region= new Region(start, end - start);
78
79 //              Template[] templates= Templates.getInstance().getTemplates();
80     String identifier = null;
81                 for (int i= 0; i != identifiers.length; i++) {
82       identifier = (String) identifiers[i];
83                         if (context.canEvaluate(identifier)) {
84                                 fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN))); 
85       }
86     }
87         }
88
89 }
90