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