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;
 
  36  * A completion processor for XML templates.
 
  38 public class BasicCompletionProcessor extends TemplateCompletionProcessor {
 
  39         private static final String DEFAULT_IMAGE = "icons/template.gif"; //$NON-NLS-1$
 
  41         private char[] fProposalAutoActivationSet;
 
  43         // private PHPCompletionProposalComparator fComparator;
 
  44         public BasicCompletionProcessor() {
 
  46                 // fComparator = new PHPCompletionProposalComparator();
 
  50          * We watch for angular brackets since those are often part of XML
 
  53         protected String extractPrefix(ITextViewer viewer, int offset) {
 
  54                 IDocument document = viewer.getDocument();
 
  56                 if (i > document.getLength())
 
  57                         return ""; //$NON-NLS-1$
 
  61                                 char ch = document.getChar(i - 1);
 
  62                                 if (ch != '<' && ch != '&' && ch != '{'
 
  63                                                 && !Character.isJavaIdentifierPart(ch))
 
  68                         return document.get(i, offset - i);
 
  69                 } catch (BadLocationException e) {
 
  70                         return ""; //$NON-NLS-1$
 
  75          * Cut out angular brackets for relevance sorting, since the template name
 
  76          * does not contain the brackets.
 
  78         protected int getRelevance(Template template, String prefix) {
 
  79                 // if (prefix.startsWith("<")) //$NON-NLS-1$
 
  80                 // prefix= prefix.substring(1);
 
  81                 if (template.getName().startsWith(prefix))
 
  87          * Simply return all templates.
 
  89         protected Template[] getTemplates(String contextTypeId) {
 
  90                 return WebUI.getDefault().getTemplateStore().getTemplates();
 
  94          * Return the XML context type that is supported by this plugin.
 
  96         protected TemplateContextType getContextType(ITextViewer viewer,
 
  98                 return WebUI.getDefault().getContextTypeRegistry().getContextType(
 
  99                                 XMLContextType.XML_CONTEXT_TYPE);
 
 103          * Always return the default image.
 
 105         protected Image getImage(Template template) {
 
 106                 ImageRegistry registry = WebUI.getDefault().getImageRegistry();
 
 107                 Image image = registry.get(DEFAULT_IMAGE);
 
 109                         ImageDescriptor desc = WebUI.imageDescriptorFromPlugin(
 
 110                                         "org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
 
 111                         registry.put(DEFAULT_IMAGE, desc);
 
 112                         image = registry.get(DEFAULT_IMAGE);
 
 120          * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
 
 123         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
 
 125                 ITextSelection selection = (ITextSelection) viewer
 
 126                                 .getSelectionProvider().getSelection();
 
 128                 // adjust offset to end of normalized selection
 
 129                 if (selection.getOffset() == offset)
 
 130                         offset = selection.getOffset() + selection.getLength();
 
 132                 String prefix = extractPrefix(viewer, offset);
 
 133                 prefix = prefix.toLowerCase();
 
 134                 IRegion region = new Region(offset - prefix.length(), prefix.length());
 
 135                 TemplateContext context = createContext(viewer, region);
 
 137                         return new ICompletionProposal[0];
 
 139                 context.setVariable("selection", selection.getText()); // name of the
 
 146                 Template[] templates = getTemplates(context.getContextType().getId());
 
 148                 List matches = new ArrayList();
 
 149                 for (int i = 0; i < templates.length; i++) {
 
 150                         Template template = templates[i];
 
 152                                 context.getContextType().validate(template.getPattern());
 
 153                         } catch (TemplateException e) {
 
 157                         if (template.getName().startsWith(prefix)) { // &&
 
 158                                                                                                                         // template.matches(prefix,
 
 159                                                                                                                         // context.getContextType().getId()))
 
 160                                 matches.add(createProposal(template, context, region,
 
 161                                                 getRelevance(template, prefix)));
 
 165                 return (ICompletionProposal[]) matches
 
 166                                 .toArray(new ICompletionProposal[matches.size()]);
 
 171          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
 
 173         public char[] getCompletionProposalAutoActivationCharacters() {
 
 174                 return fProposalAutoActivationSet;
 
 178          * Sets this processor's set of characters triggering the activation of the
 
 179          * completion proposal computation.
 
 181          * @param activationSet
 
 184         public void setCompletionProposalAutoActivationCharacters(
 
 185                         char[] activationSet) {
 
 186                 fProposalAutoActivationSet = activationSet;
 
 190          * Order the given proposals.
 
 192         // private ICompletionProposal[] order(ICompletionProposal[] proposals) {
 
 193         // Arrays.sort(proposals, fComparator);
 
 197          * Tells this processor to order the proposals alphabetically.
 
 200          *            <code>true</code> if proposals should be ordered.
 
 202         // public void orderProposalsAlphabetically(boolean order) {
 
 203         // fComparator.setOrderAlphabetically(order);