d6216391d27fa39ef1e946664ca3bbd17e4d24c4
[phpeclipse.git] /
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template.php;
6
7 import net.sourceforge.phpdt.internal.corext.template.ContextType;
8
9 import org.eclipse.jface.text.IDocument;
10
11 /**
12  * Compilation unit context type.
13  */
14 public abstract class CompilationUnitContextType extends ContextType {
15         
16         /** the document */
17         protected IDocument fDocument;
18
19         /** the completion position within the document string */
20         protected int fPosition;
21
22         /** the associated compilation unit, may be <code>null</code> */
23         //protected ICompilationUnit fCompilationUnit;
24 /*
25         protected static class ReturnType extends TemplateVariable {
26                 public ReturnType() {
27                         super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.return.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
28                 }
29     
30                 public String evaluate(TemplateContext context) {
31                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
32                         if (element == null)
33                                 return null;
34
35                         try {
36                                 return Signature.toString(((IMethod) element).getReturnType());
37                         } catch (JavaModelException e) {
38                                 return null;
39                         }
40                 }
41     
42                 public boolean isResolved(TemplateContext context) {
43                         return evaluate(context) != null;
44                 }               
45         }
46
47         protected static class File extends TemplateVariable {
48                 public File() {
49                         super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.file"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
50                 }
51                 public String evaluate(TemplateContext context) {
52                         ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
53                         
54                         return (unit == null) ? null : unit.getElementName();
55       return null;
56                 }
57                 public boolean isResolved(TemplateContext context) {
58                         return evaluate(context) != null;
59                 }               
60         }
61
62         protected static class EnclosingJavaElement extends TemplateVariable {
63                 protected final int fElementType;
64                 
65                 public EnclosingJavaElement(String name, String description, int elementType) {
66                         super(name, description);
67                         fElementType= elementType;
68                 }
69                 public String evaluate(TemplateContext context) {
70                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
71                         return (element == null) ? null : element.getElementName();                     
72                 }
73                 public boolean isResolved(TemplateContext context) {
74                         return evaluate(context) != null;
75                 }
76         }
77         
78         protected static class Method extends EnclosingJavaElement {
79                 public Method() {
80                         super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
81                 }
82         }
83
84         protected static class Type extends EnclosingJavaElement {
85                 public Type() {
86                         super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.type"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
87                 }
88         }
89
90         protected static class Package extends EnclosingJavaElement {
91                 public Package() {
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$
93                 }
94         }       
95
96         protected static class Project extends EnclosingJavaElement {
97                 public Project() {
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$
99                 }
100         }       
101
102
103         protected static class Arguments extends TemplateVariable {
104                 public Arguments() {
105                         super(JavaTemplateMessages.getString("CompilationUnitContextType.variable.name.enclosing.method.arguments"), JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
106                 }
107                 public String evaluate(TemplateContext context) {
108                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
109                         if (element == null)
110                                 return null;
111                                 
112                         IMethod method= (IMethod) element;
113                         
114                         try {
115                                 String[] arguments= method.getParameterNames();
116                                 StringBuffer buffer= new StringBuffer();
117                                 
118                                 for (int i= 0; i < arguments.length; i++) {
119                                         if (i > 0)
120                                                 buffer.append(", "); //$NON-NLS-1$
121                                         buffer.append(arguments[i]);                            
122                                 }
123                                 
124                                 return buffer.toString();
125
126                         } catch (JavaModelException e) {
127                                 return null;
128                         }
129                 }
130         }
131 */
132
133         /*
134          * @see ContextType#ContextType(String)
135          */
136         public CompilationUnitContextType(String name) {
137                 super(name);    
138         }
139
140         /**
141          * Sets context parameters. Needs to be called before createContext().
142          */
143         public void setContextParameters(IDocument document, int position) {//, ICompilationUnit compilationUnit) {
144                 fDocument= document;
145                 fPosition= position;
146 //              fCompilationUnit= compilationUnit;
147         }
148
149 }