replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / templates / template / BasicCompletionProcessor.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.ui.templates.template;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpeclipse.ui.WebUI;
17
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;
34 /**
35  * A completion processor for XML templates.
36  */
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() {
42     super();
43 //    fComparator = new PHPCompletionProposalComparator();
44   }
45   /**
46    * We watch for angular brackets since those are often part of XML templates.
47    */
48   protected String extractPrefix(ITextViewer viewer, int offset) {
49     IDocument document = viewer.getDocument();
50     int i = offset;
51     if (i > document.getLength())
52       return ""; //$NON-NLS-1$
53
54     try {
55       while (i > 0) {
56         char ch = document.getChar(i - 1);
57         if (ch != '<' && ch != '&' && ch != '{' && !Character.isJavaIdentifierPart(ch))
58           break;
59         i--;
60       }
61
62       return document.get(i, offset - i);
63     } catch (BadLocationException e) {
64       return ""; //$NON-NLS-1$
65     }
66   }
67
68   /**
69    * Cut out angular brackets for relevance sorting, since the template name does not contain the brackets.
70    */
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))
75       return 90;
76     return 0;
77   }
78
79   /**
80    * Simply return all templates.
81    */
82   protected Template[] getTemplates(String contextTypeId) {
83     return WebUI.getDefault().getTemplateStore().getTemplates();
84   }
85
86   /**
87    * Return the XML context type that is supported by this plugin.
88    */
89   protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
90     return WebUI.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE);
91   }
92
93   /**
94    * Always return the default image.
95    */
96   protected Image getImage(Template template) {
97     ImageRegistry registry = WebUI.getDefault().getImageRegistry();
98     Image image = registry.get(DEFAULT_IMAGE);
99     if (image == null) {
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);
103     }
104     return image;
105   }
106
107   /*
108    * (non-Javadoc)
109    *
110    * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
111    *      int)
112    */
113   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
114     ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
115
116     // adjust offset to end of normalized selection
117     if (selection.getOffset() == offset)
118       offset = selection.getOffset() + selection.getLength();
119
120     String prefix = extractPrefix(viewer, offset);
121     prefix = prefix.toLowerCase();
122     IRegion region = new Region(offset - prefix.length(), prefix.length());
123     TemplateContext context = createContext(viewer, region);
124     if (context == null)
125       return new ICompletionProposal[0];
126
127     context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
128
129     Template[] templates = getTemplates(context.getContextType().getId());
130
131     List matches = new ArrayList();
132     for (int i = 0; i < templates.length; i++) {
133       Template template = templates[i];
134       try {
135         context.getContextType().validate(template.getPattern());
136       } catch (TemplateException e) {
137         continue;
138       }
139
140       if (template.getName().startsWith(prefix)) { //&& template.matches(prefix, context.getContextType().getId()))
141         matches.add(createProposal(template, context, region, getRelevance(template, prefix)));
142       }
143     }
144
145     return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
146
147   }
148   /**
149    * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
150    */
151   public char[] getCompletionProposalAutoActivationCharacters() {
152     return fProposalAutoActivationSet;
153   }
154
155   /**
156    * Sets this processor's set of characters triggering the activation of the
157    * completion proposal computation.
158    *
159    * @param activationSet the activation set
160    */
161   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
162     fProposalAutoActivationSet= activationSet;
163   }
164
165   /**
166    * Order the given proposals.
167    */
168 //  private ICompletionProposal[] order(ICompletionProposal[] proposals) {
169 //    Arrays.sort(proposals, fComparator);
170 //    return proposals;
171 //  }
172
173   /**
174    * Tells this processor to order the proposals alphabetically.
175    *
176    * @param order <code>true</code> if proposals should be ordered.
177    */
178 //  public void orderProposalsAlphabetically(boolean order) {
179 //    fComparator.setOrderAlphabetically(order);
180 //  }
181 }