KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core.Signature;
20
21 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable;
22
23 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable;
24
25 /**
26  * Resolves to the lower bound of a type argument of another template variable.
27  *
28  * @since 3.3
29  */

30 public class TypeVariableResolver extends TemplateVariableResolver {
31
32     public TypeVariableResolver() {
33     }
34
35     /*
36      * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
37      * @since 3.3
38      */

39     public void resolve(TemplateVariable variable, TemplateContext context) {
40         if (!(variable instanceof MultiVariable)) {
41             super.resolve(variable, context);
42             return;
43         }
44         MultiVariable mv= (MultiVariable) variable;
45         List JavaDoc params= variable.getVariableType().getParams();
46         if (params.isEmpty()) {
47             super.resolve(variable, context);
48             return;
49         }
50         
51         JavaContext jc= (JavaContext) context;
52         String JavaDoc reference= (String JavaDoc) params.get(0);
53         int index= 0;
54         if (params.size() > 1) {
55             String JavaDoc indexParam= (String JavaDoc) params.get(1);
56             try {
57                 index= Integer.parseInt(indexParam);
58             } catch (NumberFormatException JavaDoc x) {
59             }
60         }
61         TemplateVariable refVar= jc.getTemplateVariable(reference);
62         if (refVar instanceof JavaVariable) {
63             JavaVariable jvar= (JavaVariable) refVar;
64             resolve(mv, jvar, index, jc);
65             
66             return;
67         }
68         
69         
70         super.resolve(variable, context);
71     }
72
73     private void resolve(MultiVariable mv, JavaVariable master, int index, JavaContext context) {
74         Object JavaDoc[] choices= master.getChoices();
75         if (choices instanceof Variable[]) {
76             context.addDependency(master, mv);
77             Variable[] variables= (Variable[]) choices;
78             String JavaDoc type= master.getParamType();
79             for (int i= 0; i < choices.length; i++) {
80                 String JavaDoc[] bounds= variables[i].getTypeArgumentBoundSignatures(type, index);
81                 for (int j= 0; j < bounds.length; j++)
82                     bounds[j]= Signature.getSignatureSimpleName(bounds[j]);
83                 mv.setChoices(variables[i], bounds);
84             }
85             mv.setKey(master.getCurrentChoice());
86         } else {
87             super.resolve(mv, context);
88             return;
89         }
90     }
91     
92 }
93
Popular Tags