KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > TemplateCompletionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.templates;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.graphics.Image;
19
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.contentassist.IContextInformation;
29 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
30
31
32 /**
33  * A completion processor that computes template proposals. Subclasses need to
34  * provide implementations for {@link #getTemplates(String)},
35  * {@link #getContextType(ITextViewer, IRegion)} and {@link #getImage(Template)}.
36  *
37  * @since 3.0
38  */

39 public abstract class TemplateCompletionProcessor implements IContentAssistProcessor {
40
41     private static final class ProposalComparator implements Comparator JavaDoc {
42         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
43             return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance();
44         }
45     }
46
47     private static final Comparator JavaDoc fgProposalComparator= new ProposalComparator();
48
49     /*
50      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
51      * int)
52      */

53     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
54
55         ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();
56
57         // adjust offset to end of normalized selection
58
if (selection.getOffset() == offset)
59             offset= selection.getOffset() + selection.getLength();
60
61         String JavaDoc prefix= extractPrefix(viewer, offset);
62         Region region= new Region(offset - prefix.length(), prefix.length());
63         TemplateContext context= createContext(viewer, region);
64         if (context == null)
65             return new ICompletionProposal[0];
66
67         context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
68

69         Template[] templates= getTemplates(context.getContextType().getId());
70
71         List JavaDoc matches= new ArrayList JavaDoc();
72         for (int i= 0; i < templates.length; i++) {
73             Template template= templates[i];
74             try {
75                 context.getContextType().validate(template.getPattern());
76             } catch (TemplateException e) {
77                 continue;
78             }
79             if (template.matches(prefix, context.getContextType().getId()))
80                 matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
81         }
82
83         Collections.sort(matches, fgProposalComparator);
84
85         return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
86     }
87
88     /**
89      * Creates a new proposal.
90      * <p>
91      * Forwards to {@link #createProposal(Template, TemplateContext, IRegion, int)}.
92      * Do neither call nor override.
93      * </p>
94      *
95      * @param template the template to be applied by the proposal
96      * @param context the context for the proposal
97      * @param region the region the proposal applies to
98      * @param relevance the relevance of the proposal
99      * @return a new <code>ICompletionProposal</code> for
100      * <code>template</code>
101      * @deprecated use the version specifying <code>IRegion</code> as third parameter
102      * @since 3.1
103      */

104     protected ICompletionProposal createProposal(Template template, TemplateContext context, Region region, int relevance) {
105         return createProposal(template, context, (IRegion) region, relevance);
106     }
107
108     /**
109      * Creates a new proposal.
110      * <p>
111      * The default implementation returns an instance of
112      * {@link TemplateProposal}. Subclasses may replace this method to provide
113      * their own implementations.
114      * </p>
115      *
116      * @param template the template to be applied by the proposal
117      * @param context the context for the proposal
118      * @param region the region the proposal applies to
119      * @param relevance the relevance of the proposal
120      * @return a new <code>ICompletionProposal</code> for
121      * <code>template</code>
122      */

123     protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
124         return new TemplateProposal(template, context, region, getImage(template), relevance);
125     }
126
127     /**
128      * Returns the templates valid for the context type specified by <code>contextTypeId</code>.
129      *
130      * @param contextTypeId the context type id
131      * @return the templates valid for this context type id
132      */

133     protected abstract Template[] getTemplates(String JavaDoc contextTypeId);
134
135     /**
136      * Creates a concrete template context for the given region in the document. This involves finding out which
137      * context type is valid at the given location, and then creating a context of this type. The default implementation
138      * returns a <code>DocumentTemplateContext</code> for the context type at the given location.
139      *
140      * @param viewer the viewer for which the context is created
141      * @param region the region into <code>document</code> for which the context is created
142      * @return a template context that can handle template insertion at the given location, or <code>null</code>
143      */

144     protected TemplateContext createContext(ITextViewer viewer, IRegion region) {
145         TemplateContextType contextType= getContextType(viewer, region);
146         if (contextType != null) {
147             IDocument document= viewer.getDocument();
148             return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength());
149         }
150         return null;
151     }
152
153     /**
154      * Returns the context type that can handle template insertion at the given region
155      * in the viewer's document.
156      *
157      * @param viewer the text viewer
158      * @param region the region into the document displayed by viewer
159      * @return the context type that can handle template expansion for the given location, or <code>null</code> if none exists
160      */

161     protected abstract TemplateContextType getContextType(ITextViewer viewer, IRegion region);
162
163     /**
164      * Returns the relevance of a template given a prefix. The default
165      * implementation returns a number greater than zero if the template name
166      * starts with the prefix, and zero otherwise.
167      *
168      * @param template the template to compute the relevance for
169      * @param prefix the prefix after which content assist was requested
170      * @return the relevance of <code>template</code>
171      * @see #extractPrefix(ITextViewer, int)
172      */

173     protected int getRelevance(Template template, String JavaDoc prefix) {
174         if (template.getName().startsWith(prefix))
175             return 90;
176         return 0;
177     }
178
179     /**
180      * Heuristically extracts the prefix used for determining template relevance
181      * from the viewer's document. The default implementation returns the String from
182      * offset backwards that forms a java identifier.
183      *
184      * @param viewer the viewer
185      * @param offset offset into document
186      * @return the prefix to consider
187      * @see #getRelevance(Template, String)
188      */

189     protected String JavaDoc extractPrefix(ITextViewer viewer, int offset) {
190         int i= offset;
191         IDocument document= viewer.getDocument();
192         if (i > document.getLength())
193             return ""; //$NON-NLS-1$
194

195         try {
196             while (i > 0) {
197                 char ch= document.getChar(i - 1);
198                 if (!Character.isJavaIdentifierPart(ch))
199                     break;
200                 i--;
201             }
202
203             return document.get(i, offset - i);
204         } catch (BadLocationException e) {
205             return ""; //$NON-NLS-1$
206
}
207     }
208
209     /**
210      * Returns the image to be used for the proposal for <code>template</code>.
211      *
212      * @param template the template for which an image should be returned
213      * @return the image for <code>template</code>
214      */

215     protected abstract Image getImage(Template template);
216
217     /*
218      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
219      */

220     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
221         return null;
222     }
223
224     /*
225      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
226      */

227     public char[] getCompletionProposalAutoActivationCharacters() {
228         return null;
229     }
230
231     /*
232      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
233      */

234     public char[] getContextInformationAutoActivationCharacters() {
235         return null;
236     }
237
238     /*
239      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
240      */

241     public String JavaDoc getErrorMessage() {
242         return null;
243     }
244
245     /*
246      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
247      */

248     public IContextInformationValidator getContextInformationValidator() {
249         return null;
250     }
251 }
252
Popular Tags