2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template.php;
7 import net.sourceforge.phpdt.internal.corext.template.ContextType;
9 import org.eclipse.jface.text.IDocument;
12 * Compilation unit context type.
14 public abstract class CompilationUnitContextType extends ContextType {
17 protected IDocument fDocument;
19 /** the completion position within the document string */
20 protected int fPosition;
22 /** the associated compilation unit, may be <code>null</code> */
23 //protected ICompilationUnit fCompilationUnit;
25 protected static class ReturnType extends TemplateVariable {
27 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.return.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
30 public String evaluate(TemplateContext context) {
31 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
36 return Signature.toString(((IMethod) element).getReturnType());
37 } catch (JavaModelException e) {
42 public boolean isResolved(TemplateContext context) {
43 return evaluate(context) != null;
47 protected static class File extends TemplateVariable {
49 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.file"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
51 public String evaluate(TemplateContext context) {
52 ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
54 return (unit == null) ? null : unit.getElementName();
57 public boolean isResolved(TemplateContext context) {
58 return evaluate(context) != null;
62 protected static class EnclosingJavaElement extends TemplateVariable {
63 protected final int fElementType;
65 public EnclosingJavaElement(String name, String description, int elementType) {
66 super(name, description);
67 fElementType= elementType;
69 public String evaluate(TemplateContext context) {
70 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
71 return (element == null) ? null : element.getElementName();
73 public boolean isResolved(TemplateContext context) {
74 return evaluate(context) != null;
78 protected static class Method extends EnclosingJavaElement {
80 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
84 protected static class Type extends EnclosingJavaElement {
86 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
90 protected static class Package extends EnclosingJavaElement {
92 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.package"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
96 protected static class Project extends EnclosingJavaElement {
98 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.project"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
103 protected static class Arguments extends TemplateVariable {
105 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method.arguments"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
107 public String evaluate(TemplateContext context) {
108 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
112 IMethod method= (IMethod) element;
115 String[] arguments= method.getParameterNames();
116 StringBuffer buffer= new StringBuffer();
118 for (int i= 0; i < arguments.length; i++) {
120 buffer.append(", "); //$NON-NLS-1$
121 buffer.append(arguments[i]);
124 return buffer.toString();
126 } catch (JavaModelException e) {
134 * @see ContextType#ContextType(String)
136 public CompilationUnitContextType(String name) {
141 * Sets context parameters. Needs to be called before createContext().
143 public void setContextParameters(IDocument document, int position) {//, ICompilationUnit compilationUnit) {
146 // fCompilationUnit= compilationUnit;