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