KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > template > java > LocalVarResolver


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.template.java;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jface.text.templates.TemplateContext;
16 import org.eclipse.jface.text.templates.TemplateVariable;
17 import org.eclipse.jface.text.templates.TemplateVariableResolver;
18
19 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable;
20
21 /**
22  * Resolves template variables to a local variable that is assignment-compatible with the variable
23  * instance' class parameter.
24  *
25  * @since 3.3
26  */

27 public class LocalVarResolver extends TemplateVariableResolver {
28     
29     private final String JavaDoc fDefaultType;
30     private String JavaDoc fType;
31
32     /**
33      * Default ctor for instantiation by the extension point.
34      */

35     public LocalVarResolver() {
36         this("java.lang.Object"); //$NON-NLS-1$
37
}
38     
39     LocalVarResolver(String JavaDoc defaultType) {
40         fDefaultType= defaultType;
41     }
42
43     /*
44      * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
45      */

46     public void resolve(TemplateVariable variable, TemplateContext context) {
47         List JavaDoc params= variable.getVariableType().getParams();
48         if (params.size() == 0)
49             fType= fDefaultType;
50         else
51             fType= (String JavaDoc) params.get(0);
52         
53         if (variable instanceof JavaVariable) {
54             JavaContext jc= (JavaContext) context;
55             JavaVariable jv= (JavaVariable) variable;
56             jv.setParamType(fType);
57             Variable[] variables= jc.getLocalVariables(fType);
58             if (variables.length > 0) {
59                 jv.setChoices(variables);
60                 jc.markAsUsed(jv.getDefaultValue());
61             } else {
62                 super.resolve(variable, context);
63                 return;
64             }
65             if (variables.length > 1)
66                 variable.setUnambiguous(false);
67             else
68                 variable.setUnambiguous(isUnambiguous(context));
69         } else
70             super.resolve(variable, context);
71     }
72     
73     /*
74      * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolveAll(org.eclipse.jface.text.templates.TemplateContext)
75      */

76     protected String JavaDoc[] resolveAll(TemplateContext context) {
77         JavaContext jc= (JavaContext) context;
78         Variable[] iterables= jc.getLocalVariables(fType);
79         String JavaDoc[] names= new String JavaDoc[iterables.length];
80         for (int i= 0; i < iterables.length; i++)
81             names[i]= iterables[i].getName();
82         if (names.length > 0)
83             jc.markAsUsed(names[0]);
84         return names;
85     }
86 }
87
Popular Tags