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.templates.Template;
28 import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
29 import org.eclipse.jface.text.templates.TemplateContext;
30 import org.eclipse.jface.text.templates.TemplateContextType;
31 import org.eclipse.jface.text.templates.TemplateException;
32 import org.eclipse.swt.graphics.Image;
34 * A completion processor for XML templates.
36 public class BasicCompletionProcessor extends TemplateCompletionProcessor {
37 private static final String DEFAULT_IMAGE = "icons/template.gif"; //$NON-NLS-1$
40 * We watch for angular brackets since those are often part of XML templates.
42 protected String extractPrefix(ITextViewer viewer, int offset) {
43 IDocument document = viewer.getDocument();
45 if (i > document.getLength())
46 return ""; //$NON-NLS-1$
50 char ch = document.getChar(i - 1);
51 if (ch != '<' && ch != '&' && ch != '{' && !Character.isJavaIdentifierPart(ch))
56 return document.get(i, offset - i);
57 } catch (BadLocationException e) {
58 return ""; //$NON-NLS-1$
63 * Cut out angular brackets for relevance sorting, since the template name does not contain the brackets.
65 protected int getRelevance(Template template, String prefix) {
66 // if (prefix.startsWith("<")) //$NON-NLS-1$
67 // prefix= prefix.substring(1);
68 if (template.getName().startsWith(prefix))
74 * Simply return all templates.
76 protected Template[] getTemplates(String contextTypeId) {
77 return WebUI.getDefault().getTemplateStore().getTemplates();
81 * Return the XML context type that is supported by this plugin.
83 protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
84 return WebUI.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE);
88 * Always return the default image.
90 protected Image getImage(Template template) {
91 ImageRegistry registry = WebUI.getDefault().getImageRegistry();
92 Image image = registry.get(DEFAULT_IMAGE);
94 ImageDescriptor desc = WebUI.imageDescriptorFromPlugin("org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
95 registry.put(DEFAULT_IMAGE, desc);
96 image = registry.get(DEFAULT_IMAGE);
104 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
107 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
108 ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
110 // adjust offset to end of normalized selection
111 if (selection.getOffset() == offset)
112 offset = selection.getOffset() + selection.getLength();
114 String prefix = extractPrefix(viewer, offset);
115 prefix = prefix.toLowerCase();
116 Region region = new Region(offset - prefix.length(), prefix.length());
117 TemplateContext context = createContext(viewer, region);
119 return new ICompletionProposal[0];
121 context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
123 Template[] templates = getTemplates(context.getContextType().getId());
125 List matches = new ArrayList();
126 for (int i = 0; i < templates.length; i++) {
127 Template template = templates[i];
129 context.getContextType().validate(template.getPattern());
130 } catch (TemplateException e) {
134 if (template.getName().startsWith(prefix)) { //&& template.matches(prefix, context.getContextType().getId()))
135 matches.add(createProposal(template, context, region, getRelevance(template, prefix)));
139 return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);