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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
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;
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;
29 * Compilation unit context type.
31 public abstract class CompilationUnitContextType extends TemplateContextType {
33 protected static class ReturnType extends TemplateVariableResolver {
35 super("return_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
37 protected String resolve(TemplateContext context) {
38 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
43 return Signature.toString(((IMethod) element).getReturnType());
44 } catch (JavaModelException e) {
50 protected static class File extends TemplateVariableResolver {
52 super("file", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
54 protected String resolve(TemplateContext context) {
55 ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
57 return (unit == null) ? null : unit.getElementName();
61 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
63 protected boolean isUnambiguous(TemplateContext context) {
64 return resolve(context) != null;
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$
73 protected String resolve(TemplateContext context) {
74 ICompilationUnit unit= ((CompilationUnitContext) context).getCompilationUnit();
77 String elementName= unit.getElementName();
78 return elementName.substring(0, elementName.lastIndexOf('.'));
82 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
84 protected boolean isUnambiguous(TemplateContext context) {
85 return resolve(context) != null;
89 protected static class EnclosingJavaElement extends TemplateVariableResolver {
90 protected final int fElementType;
92 public EnclosingJavaElement(String name, String description, int elementType) {
93 super(name, description);
94 fElementType= elementType;
96 protected String resolve(TemplateContext context) {
97 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(fElementType);
98 return (element == null) ? null : element.getElementName();
102 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
104 protected boolean isUnambiguous(TemplateContext context) {
105 return resolve(context) != null;
109 protected static class Method extends EnclosingJavaElement {
111 super("enclosing_method", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
115 protected static class Type extends EnclosingJavaElement {
117 super("enclosing_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
121 protected static class SuperClass extends EnclosingJavaElement {
123 super("super_class", TemplateMessages.getString("JavaContextType.variable.description.type"), IJavaElement.TYPE);
127 protected static class Package extends EnclosingJavaElement {
129 super("enclosing_package", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
133 protected static class Project extends EnclosingJavaElement {
135 super("enclosing_project", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
139 protected static class Project2 extends TemplateVariableResolver {
141 super("project", TemplateMessages.getString("JavaContextType.variable.description.project"));
143 public String evaluate(TemplateContext context) {
144 ICompilationUnit unit= ((JavaContext) context).getUnit();
145 return (unit == null) ? null : unit.getJavaProject().getElementName();
149 protected static class Arguments extends TemplateVariableResolver {
151 super("enclosing_method_arguments", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
153 protected String resolve(TemplateContext context) {
154 IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
158 IMethod method= (IMethod) element;
161 String[] arguments= method.getParameterNames();
162 StringBuffer buffer= new StringBuffer();
164 for (int i= 0; i < arguments.length; i++) {
166 buffer.append(", "); //$NON-NLS-1$
167 buffer.append(arguments[i]);
170 return buffer.toString();
172 } catch (JavaModelException e) {
179 protected static class Line extends TemplateVariableResolver {
181 super("line", TemplateMessages.getString("CompilationUnitContextType.variable.description.line"));
183 public String evaluate(TemplateContext context) {
184 return ((JavaTemplateContext) context).guessLineNumber();
190 * @see ContextType#ContextType(String)
192 public CompilationUnitContextType(String name) {
196 public abstract CompilationUnitContext createContext(IDocument document, int completionPosition, int length, ICompilationUnit compilationUnit);
200 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
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$