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