KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > TemplateCompletionProposalComputer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.jdt.internal.ui.text.java;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21
22
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.TextUtilities;
25 import org.eclipse.jface.text.templates.TemplateContextType;
26
27 import org.eclipse.jdt.core.ICompilationUnit;
28
29 import org.eclipse.jdt.internal.corext.template.java.JavaContextType;
30 import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType;
31
32 import org.eclipse.jdt.ui.text.IJavaPartitions;
33 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
35 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
36 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine;
40 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal;
41
42 /**
43  *
44  * @since 3.2
45  */

46 public final class TemplateCompletionProposalComputer implements IJavaCompletionProposalComputer {
47     
48     private final TemplateEngine fJavaTemplateEngine;
49     private final TemplateEngine fJavadocTemplateEngine;
50
51     public TemplateCompletionProposalComputer() {
52         TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(JavaContextType.NAME);
53         if (contextType == null) {
54             contextType= new JavaContextType();
55             JavaPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType);
56         }
57         if (contextType != null)
58             fJavaTemplateEngine= new TemplateEngine(contextType);
59         else
60             fJavaTemplateEngine= null;
61         contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("javadoc"); //$NON-NLS-1$
62
if (contextType == null) {
63             contextType= new JavaDocContextType();
64             JavaPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType);
65         }
66         if (contextType != null)
67             fJavadocTemplateEngine= new TemplateEngine(contextType);
68         else
69             fJavadocTemplateEngine= null;
70     }
71     
72     /*
73      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
74      */

75     public List JavaDoc computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
76         TemplateEngine engine;
77         try {
78             String JavaDoc partition= TextUtilities.getContentType(context.getDocument(), IJavaPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true);
79             if (partition.equals(IJavaPartitions.JAVA_DOC))
80                 engine= fJavadocTemplateEngine;
81             else
82                 engine= fJavaTemplateEngine;
83         } catch (BadLocationException x) {
84             return Collections.EMPTY_LIST;
85         }
86         
87         if (engine != null) {
88             if (!(context instanceof JavaContentAssistInvocationContext))
89                 return Collections.EMPTY_LIST;
90
91             JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
92             ICompilationUnit unit= javaContext.getCompilationUnit();
93             if (unit == null)
94                 return Collections.EMPTY_LIST;
95             
96             engine.reset();
97             engine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit);
98
99             TemplateProposal[] templateProposals= engine.getResults();
100             List JavaDoc result= new ArrayList JavaDoc(Arrays.asList(templateProposals));
101
102             IJavaCompletionProposal[] keyWordResults= javaContext.getKeywordProposals();
103             if (keyWordResults.length > 0) {
104                 List JavaDoc removals= new ArrayList JavaDoc();
105                 
106                 // update relevance of template proposals that match with a keyword
107
// give those templates slightly more relevance than the keyword to
108
// sort them first
109
// remove keyword templates that don't have an equivalent
110
// keyword proposal
111
if (keyWordResults.length > 0) {
112                     outer: for (int k= 0; k < templateProposals.length; k++) {
113                         TemplateProposal curr= templateProposals[k];
114                         String JavaDoc name= curr.getTemplate().getName();
115                         for (int i= 0; i < keyWordResults.length; i++) {
116                             String JavaDoc keyword= keyWordResults[i].getDisplayString();
117                             if (name.startsWith(keyword)) {
118                                 curr.setRelevance(keyWordResults[i].getRelevance() + 1);
119                                 continue outer;
120                             }
121                         }
122                         if (isKeyword(name))
123                             removals.add(curr);
124                     }
125                 }
126                 
127                 result.removeAll(removals);
128             }
129             return result;
130         }
131         
132         return Collections.EMPTY_LIST;
133     }
134
135     /*
136      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
137      */

138     public List JavaDoc computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
139         return Collections.EMPTY_LIST;
140     }
141
142     private static final Set JavaDoc KEYWORDS;
143     static {
144         Set JavaDoc keywords= new HashSet JavaDoc(42);
145         keywords.add("abstract"); //$NON-NLS-1$
146
keywords.add("assert"); //$NON-NLS-1$
147
keywords.add("break"); //$NON-NLS-1$
148
keywords.add("case"); //$NON-NLS-1$
149
keywords.add("catch"); //$NON-NLS-1$
150
keywords.add("class"); //$NON-NLS-1$
151
keywords.add("continue"); //$NON-NLS-1$
152
keywords.add("default"); //$NON-NLS-1$
153
keywords.add("do"); //$NON-NLS-1$
154
keywords.add("else"); //$NON-NLS-1$
155
keywords.add("elseif"); //$NON-NLS-1$
156
keywords.add("extends"); //$NON-NLS-1$
157
keywords.add("final"); //$NON-NLS-1$
158
keywords.add("finally"); //$NON-NLS-1$
159
keywords.add("for"); //$NON-NLS-1$
160
keywords.add("if"); //$NON-NLS-1$
161
keywords.add("implements"); //$NON-NLS-1$
162
keywords.add("import"); //$NON-NLS-1$
163
keywords.add("instanceof"); //$NON-NLS-1$
164
keywords.add("interface"); //$NON-NLS-1$
165
keywords.add("native"); //$NON-NLS-1$
166
keywords.add("new"); //$NON-NLS-1$
167
keywords.add("package"); //$NON-NLS-1$
168
keywords.add("private"); //$NON-NLS-1$
169
keywords.add("protected"); //$NON-NLS-1$
170
keywords.add("public"); //$NON-NLS-1$
171
keywords.add("return"); //$NON-NLS-1$
172
keywords.add("static"); //$NON-NLS-1$
173
keywords.add("strictfp"); //$NON-NLS-1$
174
keywords.add("super"); //$NON-NLS-1$
175
keywords.add("switch"); //$NON-NLS-1$
176
keywords.add("synchronized"); //$NON-NLS-1$
177
keywords.add("this"); //$NON-NLS-1$
178
keywords.add("throw"); //$NON-NLS-1$
179
keywords.add("throws"); //$NON-NLS-1$
180
keywords.add("transient"); //$NON-NLS-1$
181
keywords.add("try"); //$NON-NLS-1$
182
keywords.add("volatile"); //$NON-NLS-1$
183
keywords.add("while"); //$NON-NLS-1$
184
keywords.add("true"); //$NON-NLS-1$
185
keywords.add("false"); //$NON-NLS-1$
186
keywords.add("null"); //$NON-NLS-1$
187
KEYWORDS= Collections.unmodifiableSet(keywords);
188     }
189     
190     private boolean isKeyword(String JavaDoc name) {
191         return KEYWORDS.contains(name);
192     }
193
194     /*
195      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
196      */

197     public String JavaDoc getErrorMessage() {
198         return null;
199     }
200
201     /*
202      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
203      */

204     public void sessionStarted() {
205     }
206
207     /*
208      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
209      */

210     public void sessionEnded() {
211         fJavadocTemplateEngine.reset();
212         fJavaTemplateEngine.reset();
213     }
214 }
215
Popular Tags