KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > template > java > CodeTemplateContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.corext.template.java;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.DefaultLineTracker;
19 import org.eclipse.jface.text.ILineTracker;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.templates.Template;
22 import org.eclipse.jface.text.templates.TemplateBuffer;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.jface.text.templates.TemplateException;
25 import org.eclipse.jface.text.templates.TemplateTranslator;
26 import org.eclipse.jface.text.templates.TemplateVariableResolver;
27
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.IJavaProject;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32
33 public class CodeTemplateContext extends TemplateContext {
34     
35     private String JavaDoc fLineDelimiter;
36     private IJavaProject fProject;
37
38     public CodeTemplateContext(String JavaDoc contextTypeName, IJavaProject project, String JavaDoc lineDelim) {
39         super(JavaPlugin.getDefault().getCodeTemplateContextRegistry().getContextType(contextTypeName));
40         fLineDelimiter= lineDelim;
41         fProject= project;
42     }
43
44     public IJavaProject getJavaProject() {
45         return fProject;
46     }
47
48     /*
49      * @see org.eclipse.jdt.internal.corext.template.TemplateContext#evaluate(org.eclipse.jdt.internal.corext.template.Template)
50      */

51     public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
52         // test that all variables are defined
53
Iterator JavaDoc iterator= getContextType().resolvers();
54         while (iterator.hasNext()) {
55             TemplateVariableResolver var= (TemplateVariableResolver) iterator.next();
56             if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
57                 Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
58
}
59         }
60
61         if (!canEvaluate(template))
62             return null;
63             
64         String JavaDoc pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);
65         
66         TemplateTranslator translator= new TemplateTranslator();
67         TemplateBuffer buffer= translator.translate(pattern);
68         getContextType().resolve(buffer, this);
69         return buffer;
70     }
71     
72     private static String JavaDoc changeLineDelimiter(String JavaDoc code, String JavaDoc lineDelim) {
73         try {
74             ILineTracker tracker= new DefaultLineTracker();
75             tracker.set(code);
76             int nLines= tracker.getNumberOfLines();
77             if (nLines == 1) {
78                 return code;
79             }
80             
81             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
82             for (int i= 0; i < nLines; i++) {
83                 if (i != 0) {
84                     buf.append(lineDelim);
85                 }
86                 IRegion region = tracker.getLineInformation(i);
87                 String JavaDoc line= code.substring(region.getOffset(), region.getOffset() + region.getLength());
88                 buf.append(line);
89             }
90             return buf.toString();
91         } catch (BadLocationException e) {
92             // can not happen
93
return code;
94         }
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.internal.corext.template.TemplateContext#canEvaluate(org.eclipse.jdt.internal.corext.template.Template)
99      */

100     public boolean canEvaluate(Template template) {
101         return true;
102     }
103     
104     public void setCompilationUnitVariables(ICompilationUnit cu) {
105         setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
106         setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent().getElementName());
107         setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject().getElementName());
108     }
109
110 }
111
Popular Tags