2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template.php;
7 import org.eclipse.jface.text.IDocument;
9 import org.eclipse.jdt.core.ICompilationUnit;
10 import org.eclipse.jdt.core.IJavaElement;
11 import org.eclipse.jdt.core.IMethod;
12 import org.eclipse.jdt.core.JavaModelException;
13 import org.eclipse.jdt.core.Signature;
15 import net.sourceforge.phpdt.internal.corext.template.ContextType;
16 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
17 import net.sourceforge.phpdt.internal.corext.template.TemplateVariable;
20 * Compilation unit context type.
22 public abstract class CompilationUnitContextType extends ContextType {
25 protected IDocument fDocument;
27 /** the completion position within the document string */
28 protected int fPosition;
30 /** the associated compilation unit, may be <code>null</code> */
31 //protected ICompilationUnit fCompilationUnit;
33 protected static class ReturnType extends TemplateVariable {
35 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.return.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
38 public String evaluate(TemplateContext context) {
39 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
44 return Signature.toString(((IMethod) element).getReturnType());
45 } catch (JavaModelException e) {
50 public boolean isResolved(TemplateContext context) {
51 return evaluate(context) != null;
55 protected static class File extends TemplateVariable {
57 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.file"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
59 public String evaluate(TemplateContext context) {
60 ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
62 return (unit == null) ? null : unit.getElementName();
65 public boolean isResolved(TemplateContext context) {
66 return evaluate(context) != null;
70 protected static class EnclosingJavaElement extends TemplateVariable {
71 protected final int fElementType;
73 public EnclosingJavaElement(String name, String description, int elementType) {
74 super(name, description);
75 fElementType= elementType;
77 public String evaluate(TemplateContext context) {
78 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
79 return (element == null) ? null : element.getElementName();
81 public boolean isResolved(TemplateContext context) {
82 return evaluate(context) != null;
86 protected static class Method extends EnclosingJavaElement {
88 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
92 protected static class Type extends EnclosingJavaElement {
94 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
98 protected static class Package extends EnclosingJavaElement {
100 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.package"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
104 protected static class Project extends EnclosingJavaElement {
106 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.project"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
111 protected static class Arguments extends TemplateVariable {
113 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method.arguments"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
115 public String evaluate(TemplateContext context) {
116 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
120 IMethod method= (IMethod) element;
123 String[] arguments= method.getParameterNames();
124 StringBuffer buffer= new StringBuffer();
126 for (int i= 0; i < arguments.length; i++) {
128 buffer.append(", "); //$NON-NLS-1$
129 buffer.append(arguments[i]);
132 return buffer.toString();
134 } catch (JavaModelException e) {
142 * @see ContextType#ContextType(String)
144 public CompilationUnitContextType(String name) {
149 * Sets context parameters. Needs to be called before createContext().
151 public void setContextParameters(IDocument document, int position) {//, ICompilationUnit compilationUnit) {
154 // fCompilationUnit= compilationUnit;