1 /*******************************************************************************
2 * Copyright (c) 2000, orporation 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 * Sebastian Davids: sdavids@gmx.de - see bug 25376
11 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.corext.template.php;
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
20 import org.eclipse.jface.text.templates.TemplateContext;
21 import org.eclipse.jface.text.templates.TemplateVariableResolver;
22 //import net.sourceforge.phpdt.internal.ui.text.template.contentassist.*;
23 //import net.sourceforge.phpdt.internal.ui.text.template.contentassist.MultiVariableGuess;
26 * A context type for java code.
28 public class JavaContextType extends CompilationUnitContextType {
30 public static final String NAME= "php"; //$NON-NLS-1$
32 // protected static class Array extends TemplateVariableResolver {
34 // super("array", TemplateMessages.getString("JavaContextType.variable.description.array")); //$NON-NLS-1$ //$NON-NLS-2$
36 // protected String[] resolveAll(TemplateContext context) {
37 // return ((JavaContext) context).guessArrays();
40 // * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
42 // public void resolve(TemplateVariable variable, TemplateContext context) {
43 // if (variable instanceof MultiVariable) {
44 // JavaContext jc= (JavaContext) context;
45 // MultiVariable mv= (MultiVariable) variable;
46 // String[] bindings= resolveAll(context);
47 // if (bindings.length > 0) {
48 // mv.setValues(bindings);
49 // MultiVariableGuess guess= jc.getMultiVariableGuess();
50 // if (guess == null) {
51 // guess= new MultiVariableGuess(mv);
52 // jc.setMultiVariableGuess(guess);
55 // if (bindings.length > 1)
56 // variable.setUnambiguous(false);
58 // variable.setUnambiguous(isUnambiguous(context));
60 // super.resolve(variable, context);
64 // protected static class ArrayType extends TemplateVariableResolver {
65 // public ArrayType() {
66 // super("array_type", TemplateMessages.getString("JavaContextType.variable.description.array.type")); //$NON-NLS-1$ //$NON-NLS-2$
68 // protected String[] resolveAll(TemplateContext context) {
70 // String[] arrayTypes= ((JavaContext) context).guessArrayTypes();
71 // if (arrayTypes != null)
74 // return super.resolveAll(context);
78 // * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
80 // public void resolve(TemplateVariable variable, TemplateContext context) {
81 // if (variable instanceof MultiVariable) {
82 // MultiVariable mv= (MultiVariable) variable;
83 // String[] arrays= ((JavaContext) context).guessArrays();
84 // String[][] types= ((JavaContext) context).guessGroupedArrayTypes();
86 // for (int i= 0; i < arrays.length; i++) {
87 // mv.setValues(arrays[i], types[i]);
90 // if (arrays.length > 1 || types.length == 1 && types[0].length > 1)
91 // variable.setUnambiguous(false);
93 // variable.setUnambiguous(isUnambiguous(context));
96 // super.resolve(variable, context);
100 // protected static class ArrayElement extends TemplateVariableResolver {
101 // public ArrayElement() {
102 // super("array_element", TemplateMessages.getString("JavaContextType.variable.description.array.element")); //$NON-NLS-1$ //$NON-NLS-2$
104 // protected String[] resolveAll(TemplateContext context) {
105 // return ((JavaContext) context).guessArrayElements();
109 // * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
111 // public void resolve(TemplateVariable variable, TemplateContext context) {
112 // if (variable instanceof MultiVariable) {
113 // MultiVariable mv= (MultiVariable) variable;
114 // String[] arrays= ((JavaContext) context).guessArrays();
115 // String[][] elems= ((JavaContext) context).guessGroupedArrayElements();
117 // for (int i= 0; i < arrays.length; i++) {
118 // mv.setValues(arrays[i], elems[i]);
121 // if (arrays.length > 1 || elems.length == 1 && elems[0].length > 1)
122 // variable.setUnambiguous(false);
124 // variable.setUnambiguous(isUnambiguous(context));
127 // super.resolve(variable, context);
131 // protected static class Index extends TemplateVariableResolver {
133 // super("index", TemplateMessages.getString("JavaContextType.variable.description.index")); //$NON-NLS-1$ //$NON-NLS-2$
135 // protected String resolve(TemplateContext context) {
136 // return ((JavaContext) context).getIndex();
140 // protected static class Collection extends TemplateVariableResolver {
141 // public Collection() {
142 // super("collection", TemplateMessages.getString("JavaContextType.variable.description.collection")); //$NON-NLS-1$ //$NON-NLS-2$
145 // protected String[] resolveAll(TemplateContext context) {
146 // String[] collections= ((JavaContext) context).guessCollections();
147 // if (collections.length > 0)
148 // return collections;
150 // return super.resolveAll(context);
154 // protected static class Iterator extends TemplateVariableResolver {
156 // public Iterator() {
157 // super("iterator", TemplateMessages.getString("JavaContextType.variable.description.iterator")); //$NON-NLS-1$ //$NON-NLS-2$
159 // protected String resolve(TemplateContext context) {
160 // JavaContext javaContext= (JavaContext) context;
162 // if (!context.isReadOnly())
163 // javaContext.addIteratorImport();
165 // return javaContext.getIterator();
169 protected static class Todo extends TemplateVariableResolver {
172 super("todo", JavaTemplateMessages.getString("JavaContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
174 protected String resolve(TemplateContext context) {
175 JavaContext javaContext= (JavaContext) context;
176 ICompilationUnit compilationUnit= javaContext.getCompilationUnit();
177 if (compilationUnit == null)
178 return "XXX"; //$NON-NLS-1$
180 IJavaProject javaProject= compilationUnit.getJavaProject();
181 String todoTaskTag= StubUtility.getTodoTaskTag(javaProject);
182 if (todoTaskTag == null)
183 return "XXX"; //$NON-NLS-1$
189 protected static class Arguments extends SimpleVariableResolver {
191 super("arguments", TemplateMessages.getString("JavaContextType.variable.description.arguments"), "");
198 * Creates a java context type.
200 public JavaContextType() {
204 addResolver(new GlobalTemplateVariables.Cursor());
205 addResolver(new GlobalTemplateVariables.WordSelection());
206 addResolver(new GlobalTemplateVariables.LineSelection());
207 addResolver(new GlobalTemplateVariables.Dollar());
208 addResolver(new GlobalTemplateVariables.Date());
209 addResolver(new GlobalTemplateVariables.Year());
210 addResolver(new GlobalTemplateVariables.Time());
211 addResolver(new GlobalTemplateVariables.User());
214 addResolver(new File());
215 addResolver(new PrimaryTypeName());
216 addResolver(new ReturnType());
217 addResolver(new Method());
218 addResolver(new Type());
219 addResolver(new Package());
220 addResolver(new Project());
221 addResolver(new Arguments());
224 // addResolver(new Array());
225 // addResolver(new ArrayType());
226 // addResolver(new ArrayElement());
227 // addResolver(new Index());
228 // addResolver(new Iterator());
229 // addResolver(new Collection());
230 addResolver(new Todo());
235 * @see net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContextType#createContext(org.eclipse.jface.text.IDocument, int, int, net.sourceforge.phpdt.core.ICompilationUnit)
237 public CompilationUnitContext createContext(IDocument document, int offset, int length, ICompilationUnit compilationUnit) {
238 return new JavaContext(this, document, offset, length, compilationUnit);