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