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;
 
  24  * A context type for java code.
 
  26 public class JavaContextType extends CompilationUnitContextType {
 
  28         public static final String NAME = "php"; //$NON-NLS-1$
 
  30         // protected static class Array extends TemplateVariableResolver {
 
  33         // TemplateMessages.getString("JavaContextType.variable.description.array"));
 
  34         // //$NON-NLS-1$ //$NON-NLS-2$
 
  36         // protected String[] resolveAll(TemplateContext context) {
 
  37         // return ((JavaContext) context).guessArrays();
 
  41         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
 
  42         // org.eclipse.jface.text.templates.TemplateContext)
 
  44         // public void resolve(TemplateVariable variable, TemplateContext context) {
 
  45         // if (variable instanceof MultiVariable) {
 
  46         // JavaContext jc= (JavaContext) context;
 
  47         // MultiVariable mv= (MultiVariable) variable;
 
  48         // String[] bindings= resolveAll(context);
 
  49         // if (bindings.length > 0) {
 
  50         // mv.setValues(bindings);
 
  51         // MultiVariableGuess guess= jc.getMultiVariableGuess();
 
  52         // if (guess == null) {
 
  53         // guess= new MultiVariableGuess(mv);
 
  54         // jc.setMultiVariableGuess(guess);
 
  57         // if (bindings.length > 1)
 
  58         // variable.setUnambiguous(false);
 
  60         // variable.setUnambiguous(isUnambiguous(context));
 
  62         // super.resolve(variable, context);
 
  66         // protected static class ArrayType extends TemplateVariableResolver {
 
  67         // public ArrayType() {
 
  68         // super("array_type",
 
  69         // TemplateMessages.getString("JavaContextType.variable.description.array.type"));
 
  70         // //$NON-NLS-1$ //$NON-NLS-2$
 
  72         // protected String[] resolveAll(TemplateContext context) {
 
  74         // String[] arrayTypes= ((JavaContext) context).guessArrayTypes();
 
  75         // if (arrayTypes != null)
 
  78         // return super.resolveAll(context);
 
  83         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
 
  84         // org.eclipse.jface.text.templates.TemplateContext)
 
  86         // public void resolve(TemplateVariable variable, TemplateContext context) {
 
  87         // if (variable instanceof MultiVariable) {
 
  88         // MultiVariable mv= (MultiVariable) variable;
 
  89         // String[] arrays= ((JavaContext) context).guessArrays();
 
  90         // String[][] types= ((JavaContext) context).guessGroupedArrayTypes();
 
  92         // for (int i= 0; i < arrays.length; i++) {
 
  93         // mv.setValues(arrays[i], types[i]);
 
  96         // if (arrays.length > 1 || types.length == 1 && types[0].length > 1)
 
  97         // variable.setUnambiguous(false);
 
  99         // variable.setUnambiguous(isUnambiguous(context));
 
 102         // super.resolve(variable, context);
 
 106         // protected static class ArrayElement extends TemplateVariableResolver {
 
 107         // public ArrayElement() {
 
 108         // super("array_element",
 
 109         // TemplateMessages.getString("JavaContextType.variable.description.array.element"));
 
 110         // //$NON-NLS-1$ //$NON-NLS-2$
 
 112         // protected String[] resolveAll(TemplateContext context) {
 
 113         // return ((JavaContext) context).guessArrayElements();
 
 118         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
 
 119         // org.eclipse.jface.text.templates.TemplateContext)
 
 121         // public void resolve(TemplateVariable variable, TemplateContext context) {
 
 122         // if (variable instanceof MultiVariable) {
 
 123         // MultiVariable mv= (MultiVariable) variable;
 
 124         // String[] arrays= ((JavaContext) context).guessArrays();
 
 125         // String[][] elems= ((JavaContext) context).guessGroupedArrayElements();
 
 127         // for (int i= 0; i < arrays.length; i++) {
 
 128         // mv.setValues(arrays[i], elems[i]);
 
 131         // if (arrays.length > 1 || elems.length == 1 && elems[0].length > 1)
 
 132         // variable.setUnambiguous(false);
 
 134         // variable.setUnambiguous(isUnambiguous(context));
 
 137         // super.resolve(variable, context);
 
 141         // protected static class Index extends TemplateVariableResolver {
 
 144         // TemplateMessages.getString("JavaContextType.variable.description.index"));
 
 145         // //$NON-NLS-1$ //$NON-NLS-2$
 
 147         // protected String resolve(TemplateContext context) {
 
 148         // return ((JavaContext) context).getIndex();
 
 152         // protected static class Collection extends TemplateVariableResolver {
 
 153         // public Collection() {
 
 154         // super("collection",
 
 155         // TemplateMessages.getString("JavaContextType.variable.description.collection"));
 
 156         // //$NON-NLS-1$ //$NON-NLS-2$
 
 159         // protected String[] resolveAll(TemplateContext context) {
 
 160         // String[] collections= ((JavaContext) context).guessCollections();
 
 161         // if (collections.length > 0)
 
 162         // return collections;
 
 164         // return super.resolveAll(context);
 
 168         // protected static class Iterator extends TemplateVariableResolver {
 
 170         // public Iterator() {
 
 172         // TemplateMessages.getString("JavaContextType.variable.description.iterator"));
 
 173         // //$NON-NLS-1$ //$NON-NLS-2$
 
 175         // protected String resolve(TemplateContext context) {
 
 176         // JavaContext javaContext= (JavaContext) context;
 
 178         // if (!context.isReadOnly())
 
 179         // javaContext.addIteratorImport();
 
 181         // return javaContext.getIterator();
 
 185         protected static class Todo extends TemplateVariableResolver {
 
 189                                         "todo", JavaTemplateMessages.getString("JavaContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
 
 192                 protected String resolve(TemplateContext context) {
 
 193                         JavaContext javaContext = (JavaContext) context;
 
 194                         ICompilationUnit compilationUnit = javaContext.getCompilationUnit();
 
 195                         if (compilationUnit == null)
 
 196                                 return "XXX"; //$NON-NLS-1$
 
 198                         IJavaProject javaProject = compilationUnit.getJavaProject();
 
 199                         String todoTaskTag = StubUtility.getTodoTaskTag(javaProject);
 
 200                         if (todoTaskTag == null)
 
 201                                 return "XXX"; //$NON-NLS-1$
 
 208          * protected static class Arguments extends SimpleVariableResolver { public
 
 209          * Arguments() { super("arguments",
 
 210          * TemplateMessages.getString("JavaContextType.variable.description.arguments"),
 
 215          * Creates a java context type.
 
 217         public JavaContextType() {
 
 221                 addResolver(new GlobalTemplateVariables.Cursor());
 
 222                 addResolver(new GlobalTemplateVariables.WordSelection());
 
 223                 addResolver(new GlobalTemplateVariables.LineSelection());
 
 224                 addResolver(new GlobalTemplateVariables.Dollar());
 
 225                 addResolver(new GlobalTemplateVariables.Date());
 
 226                 addResolver(new GlobalTemplateVariables.Year());
 
 227                 addResolver(new GlobalTemplateVariables.Time());
 
 228                 addResolver(new GlobalTemplateVariables.User());
 
 231                 addResolver(new File());
 
 232                 addResolver(new PrimaryTypeName());
 
 233                 addResolver(new ReturnType());
 
 234                 addResolver(new Method());
 
 235                 addResolver(new Type());
 
 236                 addResolver(new Package());
 
 237                 addResolver(new Project());
 
 238                 addResolver(new Arguments());
 
 241                 // addResolver(new Array());
 
 242                 // addResolver(new ArrayType());
 
 243                 // addResolver(new ArrayElement());
 
 244                 // addResolver(new Index());
 
 245                 // addResolver(new Iterator());
 
 246                 // addResolver(new Collection());
 
 247                 addResolver(new Todo());
 
 253          * @see net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContextType#createContext(org.eclipse.jface.text.IDocument,
 
 254          *      int, int, net.sourceforge.phpdt.core.ICompilationUnit)
 
 256         public CompilationUnitContext createContext(IDocument document, int offset,
 
 257                         int length, ICompilationUnit compilationUnit) {
 
 258                 return new JavaContext(this, document, offset, length, compilationUnit);