KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids: sdavids@gmx.de - see bug 25376
11  *******************************************************************************/

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

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

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

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

82     protected String JavaDoc[] resolveAll(TemplateContext context) {
83         JavaContext jc= (JavaContext) context;
84         Variable[] localVariables= jc.getLocalVariables(fType);
85         Variable[] fields= jc.getFields(fType);
86         Variable[] variables= new Variable[localVariables.length + fields.length];
87         System.arraycopy(fields, 0, variables, 0, fields.length);
88         System.arraycopy(localVariables, 0, variables, fields.length, localVariables.length);
89         
90         String JavaDoc[] names= new String JavaDoc[variables.length];
91         for (int i= 0; i < variables.length; i++)
92             names[i]= variables[i].getName();
93         if (names.length > 0)
94             jc.markAsUsed(names[0]);
95         return names;
96     }
97 }
98
Popular Tags