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.text.java.IPHPCompletionProposal;
 
  13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  14 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
 
  15 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
 
  17 import org.eclipse.jface.text.BadLocationException;
 
  18 import org.eclipse.jface.text.IDocument;
 
  19 import org.eclipse.jface.text.IRegion;
 
  20 import org.eclipse.jface.text.ITextViewer;
 
  21 import org.eclipse.jface.text.Region;
 
  22 import org.eclipse.swt.graphics.Point;
 
  23 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
 
  25 public class BuiltInEngine {
 
  27   /** The context type. */
 
  28   private ContextType fContextType;
 
  29   /** The result proposals. */
 
  30   private ArrayList fProposals = new ArrayList();
 
  33    * Creates the template engine for a particular context type.
 
  34    * See <code>TemplateContext</code> for supported context types.
 
  36   public BuiltInEngine(ContextType contextType) {
 
  37     //  Assert.isNotNull(contextType);
 
  38     fContextType = contextType;
 
  42    * Empties the collector.
 
  44    * @param viewer the text viewer  
 
  45    * @param unit   the compilation unit (may be <code>null</code>)
 
  52    * Returns the array of matching templates.
 
  54   public IPHPCompletionProposal[] getResults() {
 
  55     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
 
  59    * Inspects the context of the compilation unit around <code>completionPosition</code>
 
  60    * and feeds the collector with proposals.
 
  61    * @param viewer the text viewer
 
  62    * @param completionPosition the context position in the document of the text viewer
 
  63    * @param compilationUnit the compilation unit (may be <code>null</code>)
 
  65   public void complete(ITextViewer viewer, int completionPosition, ArrayList identifiers)
 
  66   //,ICompilationUnit compilationUnit)
 
  67   //hrows JavaModelException
 
  69     IDocument document = viewer.getDocument();
 
  72     //          if (LinkedPositionManager.hasActiveManager(document))
 
  75     if (!(fContextType instanceof CompilationUnitContextType))
 
  77     Point selection = viewer.getSelectedRange();
 
  78     // remember selected text
 
  79     String selectedText = null;
 
  80     if (selection.y != 0) {
 
  82         selectedText = document.get(selection.x, selection.y);
 
  83       } catch (BadLocationException e) {
 
  87     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
 
  89     PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
 
  90     int start = context.getStart();
 
  91     int end = context.getEnd();
 
  92     IRegion region = new Region(start, end - start);
 
  94     //          Template[] templates= Templates.getInstance().getTemplates();
 
  95     String identifier = null;
 
  96     int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
 
  97     PHPElement element = null;
 
  98     for (int i = 0; i != identifiers.size(); i++) {
 
  99       element = (PHPElement) identifiers.get(i);
 
 100       if (element instanceof PHPFunction) {
 
 101         identifier = ((PHPFunction) element).getName();
 
 102         if (context.canEvaluate(identifier)) {
 
 103           if (maxProposals-- < 0) {
 
 106           fProposals.add(new BuiltInProposal(identifier, (PHPFunction) element, context, region, viewer));