1 11 package org.eclipse.jdt.internal.corext.template.java; 12 13 import org.eclipse.jface.text.IDocument; 14 import org.eclipse.jface.text.Position; 15 import org.eclipse.jface.text.templates.DocumentTemplateContext; 16 import org.eclipse.jface.text.templates.TemplateContextType; 17 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaProject; 21 import org.eclipse.jdt.core.JavaModelException; 22 23 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariableGuess; 24 25 26 29 public abstract class CompilationUnitContext extends DocumentTemplateContext { 30 31 32 private final ICompilationUnit fCompilationUnit; 33 34 protected boolean fForceEvaluation; 35 36 protected MultiVariableGuess fMultiVariableGuess; 37 38 protected final boolean fIsManaged; 39 40 49 protected CompilationUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) { 50 super(type, document, completionOffset, completionLength); 51 fCompilationUnit= compilationUnit; 52 fIsManaged= false; 53 } 54 55 64 protected CompilationUnitContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) { 65 super(type, document, completionPosition); 66 fCompilationUnit= compilationUnit; 67 fIsManaged= true; 68 } 69 70 76 public final ICompilationUnit getCompilationUnit() { 77 return fCompilationUnit; 78 } 79 80 87 public IJavaElement findEnclosingElement(int elementType) { 88 if (fCompilationUnit == null) 89 return null; 90 91 try { 92 IJavaElement element= fCompilationUnit.getElementAt(getStart()); 93 if (element == null) { 94 element= fCompilationUnit; 95 } 96 97 return element.getAncestor(elementType); 98 99 } catch (JavaModelException e) { 100 return null; 101 } 102 } 103 104 110 public void setForceEvaluation(boolean evaluate) { 111 fForceEvaluation= evaluate; 112 } 113 114 119 public MultiVariableGuess getMultiVariableGuess() { 120 return fMultiVariableGuess; 121 } 122 123 126 void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) { 127 fMultiVariableGuess= multiVariableGuess; 128 } 129 130 protected IJavaProject getJavaProject() { 131 ICompilationUnit compilationUnit= getCompilationUnit(); 132 IJavaProject project= compilationUnit == null ? null : compilationUnit.getJavaProject(); 133 return project; 134 } 135 } 136 | Popular Tags |