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 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;
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;
28 * Compilation unit context type.
30 public abstract class CompilationUnitContextType extends TemplateContextType {
32 protected static class ReturnType extends TemplateVariableResolver {
35 "return_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$
38 protected String resolve(TemplateContext context) {
39 IJavaElement element = ((CompilationUnitContext) context)
40 .findEnclosingElement(IJavaElement.METHOD);
45 return Signature.toString(((IMethod) element).getReturnType());
46 } catch (JavaModelException e) {
52 protected static class File extends TemplateVariableResolver {
55 "file", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$
58 protected String resolve(TemplateContext context) {
59 ICompilationUnit unit = ((CompilationUnitContext) context)
60 .getCompilationUnit();
62 return (unit == null) ? null : unit.getElementName();
66 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
68 protected boolean isUnambiguous(TemplateContext context) {
69 return resolve(context) != null;
73 protected static class PrimaryTypeName extends TemplateVariableResolver {
74 public PrimaryTypeName() {
76 "primary_type_name", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.primary.type.name")); //$NON-NLS-1$ //$NON-NLS-2$
80 protected String resolve(TemplateContext context) {
81 ICompilationUnit unit = ((CompilationUnitContext) context)
82 .getCompilationUnit();
85 String elementName = unit.getElementName();
86 return elementName.substring(0, elementName.lastIndexOf('.'));
90 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
92 protected boolean isUnambiguous(TemplateContext context) {
93 return resolve(context) != null;
97 protected static class EnclosingJavaElement extends
98 TemplateVariableResolver {
99 protected final int fElementType;
101 public EnclosingJavaElement(String name, String description,
103 super(name, description);
104 fElementType = elementType;
107 protected String resolve(TemplateContext context) {
108 IJavaElement element = ((CompilationUnitContext) context)
109 .findEnclosingElement(fElementType);
110 return (element == null) ? null : element.getElementName();
114 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#isUnambiguous(org.eclipse.jface.text.templates.TemplateContext)
116 protected boolean isUnambiguous(TemplateContext context) {
117 return resolve(context) != null;
121 protected static class Method extends EnclosingJavaElement {
124 "enclosing_method", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method"), IJavaElement.METHOD); //$NON-NLS-1$ //$NON-NLS-2$
128 protected static class Type extends EnclosingJavaElement {
131 "enclosing_type", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.type"), IJavaElement.TYPE); //$NON-NLS-1$ //$NON-NLS-2$
136 * protected static class SuperClass extends EnclosingJavaElement { public
137 * Type() { super("super_class",
138 * TemplateMessages.getString("JavaContextType.variable.description.type"),
139 * IJavaElement.TYPE); } }
141 protected static class Package extends EnclosingJavaElement {
144 "enclosing_package", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.package"), IJavaElement.PACKAGE_FRAGMENT); //$NON-NLS-1$ //$NON-NLS-2$
148 protected static class Project extends EnclosingJavaElement {
151 "enclosing_project", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.project"), IJavaElement.JAVA_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$
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(); } }
163 protected static class Arguments extends TemplateVariableResolver {
166 "enclosing_method_arguments", JavaTemplateMessages.getString("CompilationUnitContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$
169 protected String resolve(TemplateContext context) {
170 IJavaElement element = ((CompilationUnitContext) context)
171 .findEnclosingElement(IJavaElement.METHOD);
175 IMethod method = (IMethod) element;
178 String[] arguments = method.getParameterNames();
179 StringBuffer buffer = new StringBuffer();
181 for (int i = 0; i < arguments.length; i++) {
183 buffer.append(", "); //$NON-NLS-1$
184 buffer.append(arguments[i]);
187 return buffer.toString();
189 } catch (JavaModelException e) {
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(); } }
204 * @see ContextType#ContextType(String)
206 public CompilationUnitContextType(String name) {
210 public abstract CompilationUnitContext createContext(IDocument document,
211 int completionPosition, int length, ICompilationUnit compilationUnit);
216 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
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(
227 .getString("ContextType.error.multiple.cursor.variables")); //$NON-NLS-1$