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