refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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.core.ICompilationUnit;
10 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
11 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
12 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
13 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
14 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
17 import net.sourceforge.phpeclipse.ui.WebUI;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.Region;
24 import org.eclipse.swt.graphics.Point;
25
26 public class BuiltInEngine {
27
28         /** The context type. */
29         private JavaContextType fContextType;
30
31         /** The result proposals. */
32         private ArrayList fProposals = new ArrayList();
33
34         /**
35          * Creates the template engine for a particular context type. See
36          * <code>TemplateContext</code> for supported context types.
37          */
38         public BuiltInEngine(JavaContextType contextType) {
39                 // Assert.isNotNull(contextType);
40                 fContextType = contextType;
41         }
42
43         /**
44          * Empties the collector.
45          * 
46          * @param viewer
47          *            the text viewer
48          * @param unit
49          *            the compilation unit (may be <code>null</code>)
50          */
51         public void reset() {
52                 fProposals.clear();
53         }
54
55         /**
56          * Returns the array of matching templates.
57          */
58         public IPHPCompletionProposal[] getResults() {
59                 return (IPHPCompletionProposal[]) fProposals
60                                 .toArray(new IPHPCompletionProposal[fProposals.size()]);
61         }
62
63         /**
64          * Inspects the context of the compilation unit around
65          * <code>completionPosition</code> and feeds the collector with proposals.
66          * 
67          * @param viewer
68          *            the text viewer
69          * @param completionPosition
70          *            the context position in the document of the text viewer
71          * @param compilationUnit
72          *            the compilation unit (may be <code>null</code>)
73          */
74         public void complete(ITextViewer viewer, int completionPosition,
75                         ArrayList identifiers, ICompilationUnit compilationUnit)
76         // hrows JavaModelException
77         {
78                 IDocument document = viewer.getDocument();
79
80                 // prohibit recursion
81                 // if (LinkedPositionManager.hasActiveManager(document))
82                 // return;
83
84                 if (!(fContextType instanceof CompilationUnitContextType))
85                         return;
86                 Point selection = viewer.getSelectedRange();
87                 // remember selected text
88                 String selectedText = null;
89                 if (selection.y != 0) {
90                         try {
91                                 selectedText = document.get(selection.x, selection.y);
92                         } catch (BadLocationException e) {
93                         }
94                 }
95
96                 // ((CompilationUnitContextType)
97                 // fContextType).setContextParameters(document, completionPosition,
98                 // selection.y); //mpilationUnit);
99                 // JavaContext context = (JavaContext) fContextType.createContext();
100                 JavaContext context = (JavaContext) fContextType.createContext(
101                                 document, completionPosition, selection.y, compilationUnit);
102                 context.setVariable("selection", selectedText); //$NON-NLS-1$
103                 int start = context.getStart();
104                 int end = context.getEnd();
105                 IRegion region = new Region(start, end - start);
106
107                 // Template[] templates= Templates.getInstance().getTemplates();
108                 String identifier = null;
109                 int maxProposals = WebUI.MAX_PROPOSALS;
110                 PHPElement element = null;
111                 for (int i = 0; i != identifiers.size(); i++) {
112                         element = (PHPElement) identifiers.get(i);
113                         if (element instanceof PHPFunction) {
114                                 identifier = ((PHPFunction) element).getName();
115                                 if (context.canEvaluate(identifier)) {
116                                         if (maxProposals-- < 0) {
117                                                 return;
118                                         }
119                                         fProposals.add(new BuiltInProposal(identifier,
120                                                         (PHPFunction) element, context, region, viewer));
121                                 }
122                         }
123                 }
124         }
125
126 }