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.DocumentTemplateContext;
11 import net.sourceforge.phpdt.internal.corext.template.Template;
12 import net.sourceforge.phpdt.internal.corext.template.Templates;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
15 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
26 public class TemplateEngine {
28 /** The context type. */
29 private ContextType fContextType;
30 /** The result proposals. */
31 private ArrayList fProposals = new ArrayList();
34 * Creates the template engine for a particular context type.
35 * See <code>TemplateContext</code> for supported context types.
37 public TemplateEngine(ContextType contextType) {
38 // Assert.isNotNull(contextType);
39 fContextType = contextType;
43 * Empties the collector.
45 * @param viewer the text viewer
46 * @param unit the compilation unit (may be <code>null</code>)
53 * Returns the array of matching templates.
55 public IPHPCompletionProposal[] getResults() {
56 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
60 * Inspects the context of the compilation unit around <code>completionPosition</code>
61 * and feeds the collector with proposals.
62 * @param viewer the text viewer
63 * @param completionPosition the context position in the document of the text viewer
64 * @param compilationUnit the compilation unit (may be <code>null</code>)
66 public void complete(ITextViewer viewer, int completionPosition)
67 //,ICompilationUnit compilationUnit)
68 //hrows JavaModelException
70 IDocument document = viewer.getDocument();
72 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) {
85 ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
86 DocumentTemplateContext context = (DocumentTemplateContext) 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 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
94 for (int i = 0; i != templates.length; i++)
95 if (context.canEvaluate(templates[i])) {
96 if (maxProposals-- < 0) {
99 fProposals.add(new TemplateProposal(templates[i], context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_OBJS_TEMPLATE)));