5b432248808f5310694a205cf5e51de9e93e3f6d
[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.BadLocationException;
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.swt.graphics.Point;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
22
23 public class BuiltInEngine {
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 BuiltInEngine(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, Object[] identifiers)
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     Point selection= viewer.getSelectedRange();
76     // remember selected text
77     String selectedText= null;
78     if (selection.y != 0) {
79       try {
80         selectedText= document.get(selection.x, selection.y);
81       } catch (BadLocationException e) {}
82     }
83     
84     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
85
86                 PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
87                 int start= context.getStart();
88                 int end= context.getEnd();
89                 IRegion region= new Region(start, end - start);
90
91 //              Template[] templates= Templates.getInstance().getTemplates();
92     String identifier = null;
93                 for (int i= 0; i != identifiers.length; i++) {
94       identifier = (String) identifiers[i];
95                         if (context.canEvaluate(identifier)) {
96                                 fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN))); 
97       }
98     }
99         }
100
101 }
102