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 fOffset;
22 /** the completion length */
23 protected int fLength;
25 /** the associated compilation unit, may be <code>null</code> */
26 //protected ICompilationUnit fCompilationUnit;
28 protected static class ReturnType extends TemplateVariable {
30 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.return.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
33 public String evaluate(TemplateContext context) {
34 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
39 return Signature.toString(((IMethod) element).getReturnType());
40 } catch (JavaModelException e) {
45 public boolean isResolved(TemplateContext context) {
46 return evaluate(context) != null;
50 protected static class File extends TemplateVariable {
52 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.file"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
54 public String evaluate(TemplateContext context) {
55 ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
57 return (unit == null) ? null : unit.getElementName();
60 public boolean isResolved(TemplateContext context) {
61 return evaluate(context) != null;
65 protected static class EnclosingJavaElement extends TemplateVariable {
66 protected final int fElementType;
68 public EnclosingJavaElement(String name, String description, int elementType) {
69 super(name, description);
70 fElementType= elementType;
72 public String evaluate(TemplateContext context) {
73 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
74 return (element == null) ? null : element.getElementName();
76 public boolean isResolved(TemplateContext context) {
77 return evaluate(context) != null;
81 protected static class Method extends EnclosingJavaElement {
83 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
87 protected static class Type extends EnclosingJavaElement {
89 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
93 protected static class Package extends EnclosingJavaElement {
95 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.package"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
99 protected static class Project extends EnclosingJavaElement {
101 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.project"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
106 protected static class Arguments extends TemplateVariable {
108 super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method.arguments"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
110 public String evaluate(TemplateContext context) {
111 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
115 IMethod method= (IMethod) element;
118 String[] arguments= method.getParameterNames();
119 StringBuffer buffer= new StringBuffer();
121 for (int i= 0; i < arguments.length; i++) {
123 buffer.append(", "); //$NON-NLS-1$
124 buffer.append(arguments[i]);
127 return buffer.toString();
129 } catch (JavaModelException e) {
137 * @see ContextType#ContextType(String)
139 public CompilationUnitContextType(String name) {
144 * Sets context parameters. Needs to be called before createContext().
146 public void setContextParameters(IDocument document, int position, int length) {//, ICompilationUnit compilationUnit) {
150 // fCompilationUnit= compilationUnit;