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