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