1 /*******************************************************************************
 
   2  * Copyright (c) 2000, 2004 IBM Corporation and others.
 
   3  * All rights reserved. This program and the accompanying materials 
 
   4  * are made available under the terms of the Common Public License v1.0
 
   5  * which accompanies this distribution, and is available at
 
   6  * http://www.eclipse.org/legal/cpl-v10.html
 
   9  *     IBM Corporation - initial API and implementation
 
  10  *******************************************************************************/
 
  11 package net.sourceforge.phpeclipse.ui.templates.template;
 
  13 import java.util.ArrayList;
 
  14 import java.util.List;
 
  16 import net.sourceforge.phpeclipse.ui.WebUI;
 
  18 import org.eclipse.jface.resource.ImageDescriptor;
 
  19 import org.eclipse.jface.resource.ImageRegistry;
 
  20 import org.eclipse.jface.text.BadLocationException;
 
  21 import org.eclipse.jface.text.IDocument;
 
  22 import org.eclipse.jface.text.IRegion;
 
  23 import org.eclipse.jface.text.ITextSelection;
 
  24 import org.eclipse.jface.text.ITextViewer;
 
  25 import org.eclipse.jface.text.Region;
 
  26 import org.eclipse.jface.text.contentassist.ICompletionProposal;
 
  27 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 
  28 import org.eclipse.jface.text.templates.Template;
 
  29 import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
 
  30 import org.eclipse.jface.text.templates.TemplateContext;
 
  31 import org.eclipse.jface.text.templates.TemplateContextType;
 
  32 import org.eclipse.jface.text.templates.TemplateException;
 
  33 import org.eclipse.swt.graphics.Image;
 
  35  * A completion processor for XML templates.
 
  37 public class BasicCompletionProcessor extends TemplateCompletionProcessor {
 
  38   private static final String DEFAULT_IMAGE = "icons/template.gif"; //$NON-NLS-1$
 
  39   private char[] fProposalAutoActivationSet;
 
  40 //  private PHPCompletionProposalComparator fComparator;
 
  41   public BasicCompletionProcessor() {
 
  43 //    fComparator = new PHPCompletionProposalComparator();
 
  46    * We watch for angular brackets since those are often part of XML templates.
 
  48   protected String extractPrefix(ITextViewer viewer, int offset) {
 
  49     IDocument document = viewer.getDocument();
 
  51     if (i > document.getLength())
 
  52       return ""; //$NON-NLS-1$
 
  56         char ch = document.getChar(i - 1);
 
  57         if (ch != '<' && ch != '&' && ch != '{' && !Character.isJavaIdentifierPart(ch))
 
  62       return document.get(i, offset - i);
 
  63     } catch (BadLocationException e) {
 
  64       return ""; //$NON-NLS-1$
 
  69    * Cut out angular brackets for relevance sorting, since the template name does not contain the brackets.
 
  71   protected int getRelevance(Template template, String prefix) {
 
  72     //          if (prefix.startsWith("<")) //$NON-NLS-1$
 
  73     //                  prefix= prefix.substring(1);
 
  74     if (template.getName().startsWith(prefix))
 
  80    * Simply return all templates.
 
  82   protected Template[] getTemplates(String contextTypeId) {
 
  83     return WebUI.getDefault().getTemplateStore().getTemplates();
 
  87    * Return the XML context type that is supported by this plugin.
 
  89   protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
 
  90     return WebUI.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE);
 
  94    * Always return the default image.
 
  96   protected Image getImage(Template template) {
 
  97     ImageRegistry registry = WebUI.getDefault().getImageRegistry();
 
  98     Image image = registry.get(DEFAULT_IMAGE);
 
 100       ImageDescriptor desc = WebUI.imageDescriptorFromPlugin("org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
 
 101       registry.put(DEFAULT_IMAGE, desc);
 
 102       image = registry.get(DEFAULT_IMAGE);
 
 110    * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
 
 113   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
 
 114     ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
 
 116     // adjust offset to end of normalized selection
 
 117     if (selection.getOffset() == offset)
 
 118       offset = selection.getOffset() + selection.getLength();
 
 120     String prefix = extractPrefix(viewer, offset);
 
 121     prefix = prefix.toLowerCase();
 
 122     Region region = new Region(offset - prefix.length(), prefix.length());
 
 123     TemplateContext context = createContext(viewer, region);
 
 125       return new ICompletionProposal[0];
 
 127     context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
 
 129     Template[] templates = getTemplates(context.getContextType().getId());
 
 131     List matches = new ArrayList();
 
 132     for (int i = 0; i < templates.length; i++) {
 
 133       Template template = templates[i];
 
 135         context.getContextType().validate(template.getPattern());
 
 136       } catch (TemplateException e) {
 
 140       if (template.getName().startsWith(prefix)) { //&& template.matches(prefix, context.getContextType().getId()))
 
 141         matches.add(createProposal(template, context, region, getRelevance(template, prefix)));
 
 145     return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
 
 149    * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
 
 151   public char[] getCompletionProposalAutoActivationCharacters() {
 
 152     return fProposalAutoActivationSet;
 
 156    * Sets this processor's set of characters triggering the activation of the
 
 157    * completion proposal computation.
 
 159    * @param activationSet the activation set
 
 161   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
 
 162     fProposalAutoActivationSet= activationSet;
 
 166    * Order the given proposals.
 
 168 //  private ICompletionProposal[] order(ICompletionProposal[] proposals) {
 
 169 //    Arrays.sort(proposals, fComparator);
 
 174    * Tells this processor to order the proposals alphabetically.
 
 176    * @param order <code>true</code> if proposals should be ordered.
 
 178 //  public void orderProposalsAlphabetically(boolean order) {
 
 179 //    fComparator.setOrderAlphabetically(order);