1 11 package org.eclipse.jdt.internal.corext.template.java; 12 13 import java.util.Iterator ; 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 fLineDelimiter; 36 private IJavaProject fProject; 37 38 public CodeTemplateContext(String contextTypeName, IJavaProject project, String 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 51 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { 52 Iterator 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"); } 59 } 60 61 if (!canEvaluate(template)) 62 return null; 63 64 String 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 changeLineDelimiter(String code, String 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 buf= new StringBuffer (); 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 line= code.substring(region.getOffset(), region.getOffset() + region.getLength()); 88 buf.append(line); 89 } 90 return buf.toString(); 91 } catch (BadLocationException e) { 92 return code; 94 } 95 } 96 97 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 |