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