added PHPDoc contexts
[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.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 IdentifierEngine {
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 IdentifierEngine(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                 
76     Point selection= viewer.getSelectedRange();
77     // remember selected text
78     String selectedText= null;
79     if (selection.y != 0) {
80       try {
81         selectedText= document.get(selection.x, selection.y);
82       } catch (BadLocationException e) {}
83     }
84     
85     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
86
87                 PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
88                 int start= context.getStart();
89                 int end= context.getEnd();
90                 IRegion region= new Region(start, end - start);
91
92 //              Template[] templates= Templates.getInstance().getTemplates();
93     String identifier = null;
94                 for (int i= 0; i != identifiers.length; i++) {
95       identifier = (String) identifiers[i];
96                         if (context.canEvaluate(identifier)) {
97                                 fProposals.add(new IdentifierProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_FUN), PHPUiImages.get(PHPUiImages.IMG_VAR))); 
98       }
99     }
100         }
101
102 }
103