intial source from http://www.sf.net/projects/wdte
[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.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;
33 /**
34  * A completion processor for XML templates.
35  */
36 public class BasicCompletionProcessor extends TemplateCompletionProcessor {
37   private static final String DEFAULT_IMAGE = "icons/template.gif"; //$NON-NLS-1$
38
39   /**
40    * We watch for angular brackets since those are often part of XML templates.
41    */
42   protected String extractPrefix(ITextViewer viewer, int offset) {
43     IDocument document = viewer.getDocument();
44     int i = offset;
45     if (i > document.getLength())
46       return ""; //$NON-NLS-1$
47
48     try {
49       while (i > 0) {
50         char ch = document.getChar(i - 1);
51         if (ch != '<' && ch != '&' && ch != '{' && !Character.isJavaIdentifierPart(ch))
52           break;
53         i--;
54       }
55
56       return document.get(i, offset - i);
57     } catch (BadLocationException e) {
58       return ""; //$NON-NLS-1$
59     }
60   }
61
62   /**
63    * Cut out angular brackets for relevance sorting, since the template name does not contain the brackets.
64    */
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))
69       return 90;
70     return 0;
71   }
72
73   /**
74    * Simply return all templates.
75    */
76   protected Template[] getTemplates(String contextTypeId) {
77     return WebUI.getDefault().getTemplateStore().getTemplates();
78   }
79
80   /**
81    * Return the XML context type that is supported by this plugin.
82    */
83   protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
84     return WebUI.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE);
85   }
86
87   /**
88    * Always return the default image.
89    */
90   protected Image getImage(Template template) {
91     ImageRegistry registry = WebUI.getDefault().getImageRegistry();
92     Image image = registry.get(DEFAULT_IMAGE);
93     if (image == null) {
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);
97     }
98     return image;
99   }
100
101   /*
102    * (non-Javadoc)
103    * 
104    * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
105    *      int)
106    */
107   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
108     ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
109
110     // adjust offset to end of normalized selection
111     if (selection.getOffset() == offset)
112       offset = selection.getOffset() + selection.getLength();
113
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);
118     if (context == null)
119       return new ICompletionProposal[0];
120
121     context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
122
123     Template[] templates = getTemplates(context.getContextType().getId());
124
125     List matches = new ArrayList();
126     for (int i = 0; i < templates.length; i++) {
127       Template template = templates[i];
128       try {
129         context.getContextType().validate(template.getPattern());
130       } catch (TemplateException e) {
131         continue;
132       }
133
134       if (template.getName().startsWith(prefix)) { //&& template.matches(prefix, context.getContextType().getId()))
135         matches.add(createProposal(template, context, region, getRelevance(template, prefix)));
136       }
137     }
138
139     return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
140
141   }
142 }