d19bf3f6d4da2427f7888609c3022d4494ecd7cb
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
12
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.templates.TemplateContextType;
15 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
16 import org.eclipse.jface.text.templates.TemplateContext;
17 import org.eclipse.jface.text.templates.TemplateException;
18 import org.eclipse.jface.text.templates.TemplateVariable;
19 import org.eclipse.jface.text.templates.TemplateVariableResolver;
20
21 import net.sourceforge.phpdt.core.ICompilationUnit;
22 import net.sourceforge.phpdt.core.IJavaElement;
23 import net.sourceforge.phpdt.core.IMethod;
24 import net.sourceforge.phpdt.core.JavaModelException;
25 import net.sourceforge.phpdt.core.Signature;
26
27
28 /**
29  * Compilation unit context type.
30  */
31 public abstract class CompilationUnitContextType extends TemplateContextType {
32         
33         protected static class ReturnType extends TemplateVariableResolver {
34                 public ReturnType() {
35                         super("return_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
36                 }
37                 protected String resolve(TemplateContext context) {
38                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
39                         if (element == null)
40                                 return null;
41
42                         try {
43                                 return Signature.toString(((IMethod) element).getReturnType());
44                         } catch (JavaModelException e) {
45                                 return null;
46                         }
47                 }
48         }
49
50         protected static class File extends TemplateVariableResolver {
51                 public File() {
52                         super("file", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
53                 }
54                 protected String resolve(TemplateContext context) {
55                         ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
56                         
57                         return (unit == null) ? null : unit.getElementName();
58                 }
59                 
60                 /*
61                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
62                  */
63                 protected boolean isUnambiguous(TemplateContext context) {
64                         return resolve(context) != null;
65                 }
66         }
67         
68         protected static class PrimaryTypeName extends TemplateVariableResolver {
69                 public PrimaryTypeName() {
70                         super("primary_type_name", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.primary.type.name")); //$NON-NLS-1$ //$NON-NLS-2$
71                         
72                 }
73                 protected String resolve(TemplateContext context) {
74                         ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
75                         if (unit == null) 
76                                 return null;
77                         String elementName= unit.getElementName();
78                         return elementName.substring(0, elementName.lastIndexOf('.'));
79                 }
80                 
81                 /*
82                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
83                  */
84                 protected boolean isUnambiguous(TemplateContext context) {
85                         return resolve(context) != null;
86                 }
87         }
88
89         protected static class EnclosingJavaElement extends TemplateVariableResolver {
90                 protected final int fElementType;
91                 
92                 public EnclosingJavaElement(String name, String description, int elementType) {
93                         super(name, description);
94                         fElementType= elementType;
95                 }
96                 protected String resolve(TemplateContext context) {
97                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
98                         return (element == null) ? null : element.getElementName();                     
99                 }
100                 
101                 /*
102                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
103                  */
104                 protected boolean isUnambiguous(TemplateContext context) {
105                         return resolve(context) != null;
106                 }
107         }
108         
109         protected static class Method extends EnclosingJavaElement {
110                 public Method() {
111                         super("enclosing_method", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
112                 }
113         }
114
115         protected static class Type extends EnclosingJavaElement {
116                 public Type() {
117                         super("enclosing_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
118                 }
119         }
120 /*
121         protected static class SuperClass extends EnclosingJavaElement {
122                 public Type() {
123                         super("super_class", TemplateMessages.getString("JavaContextType.variable.description.type"), IJavaElement.TYPE);
124                 }
125         }
126 */
127         protected static class Package extends EnclosingJavaElement {
128                 public Package() {
129                         super("enclosing_package", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
130                 }
131         }       
132
133         protected static class Project extends EnclosingJavaElement {
134                 public Project() {
135                         super("enclosing_project", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
136                 }
137         }       
138 /*
139         protected static class Project2 extends TemplateVariableResolver {
140                 public Project2() {
141                         super("project", TemplateMessages.getString("JavaContextType.variable.description.project"));
142                 }
143                 public String evaluate(TemplateContext context) {
144                         ICompilationUnit unit= ((JavaContext) context).getUnit();
145                         return (unit == null) ? null : unit.getJavaProject().getElementName();
146                 }
147         }       
148 */
149         protected static class Arguments extends TemplateVariableResolver {
150                 public Arguments() {
151                         super("enclosing_method_arguments", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
152                 }
153                 protected String resolve(TemplateContext context) {
154                         IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
155                         if (element == null)
156                                 return null;
157                                 
158                         IMethod method= (IMethod) element;
159                         
160                         try {
161                                 String[] arguments= method.getParameterNames();
162                                 StringBuffer buffer= new StringBuffer();
163                                 
164                                 for (int i= 0; i < arguments.length; i++) {
165                                         if (i > 0)
166                                                 buffer.append(", "); //$NON-NLS-1$
167                                         buffer.append(arguments[i]);                            
168                                 }
169                                 
170                                 return buffer.toString();
171
172                         } catch (JavaModelException e) {
173                                 return null;
174                         }
175                 }
176         }
177
178 /*      
179         protected static class Line extends TemplateVariableResolver {
180                 public Line() {
181                         super("line", TemplateMessages.getString("CompilationUnitContextType.variable.description.line"));
182                 }
183                 public String evaluate(TemplateContext context) {
184                         return ((JavaTemplateContext) context).guessLineNumber();
185                 }
186         }
187 */      
188
189         /*
190          * @see ContextType#ContextType(String)
191          */
192         public CompilationUnitContextType(String name) {
193                 super(name);    
194         }
195
196         public abstract CompilationUnitContext createContext(IDocument document, int completionPosition, int length, ICompilationUnit compilationUnit);
197
198
199         /* (non-Javadoc)
200          * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
201          */
202         protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
203                 // check for multiple cursor variables          
204                 for (int i= 0; i < variables.length; i++) {
205                         TemplateVariable var= variables[i];
206                         if (var.getType().equals(GlobalTemplateVariables.Cursor.NAME)) {
207                                 if (var.getOffsets().length > 1) {
208                                         throw new TemplateException(JavaTemplateMessages.getString("ContextType.error.multiple.cursor.variables")); //$NON-NLS-1$
209                                 }
210                         }
211                 }
212         }
213
214 }