2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
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;
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;
23 public class BuiltInEngine {
25 /** The context type. */
26 private ContextType fContextType;
27 /** The result proposals. */
28 private ArrayList fProposals= new ArrayList();
31 * Creates the template engine for a particular context type.
32 * See <code>TemplateContext</code> for supported context types.
34 public BuiltInEngine(ContextType contextType) {
35 // Assert.isNotNull(contextType);
36 fContextType= contextType;
40 * Empties the collector.
42 * @param viewer the text viewer
43 * @param unit the compilation unit (may be <code>null</code>)
50 * Returns the array of matching templates.
52 public IPHPCompletionProposal[] getResults() {
53 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
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>)
63 public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
64 //,ICompilationUnit compilationUnit)
65 //hrows JavaModelException
67 IDocument document= viewer.getDocument();
70 // if (LinkedPositionManager.hasActiveManager(document))
73 if (!(fContextType instanceof CompilationUnitContextType))
75 Point selection= viewer.getSelectedRange();
76 // remember selected text
77 String selectedText= null;
78 if (selection.y != 0) {
80 selectedText= document.get(selection.x, selection.y);
81 } catch (BadLocationException e) {}
84 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
86 PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
87 int start= context.getStart();
88 int end= context.getEnd();
89 IRegion region= new Region(start, end - start);
91 // Template[] templates= Templates.getInstance().getTemplates();
92 String identifier = null;
93 for (int i= 0; i != identifiers.length; i++) {
94 identifier = (String) identifiers[i];
95 if (context.canEvaluate(identifier)) {
96 fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN)));