KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.text.templates.TemplateContext;
19 import org.eclipse.jface.text.templates.TemplateVariable;
20 import org.eclipse.jface.text.templates.TemplateVariableResolver;
21
22 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable;
23
24 /**
25  * Resolves template variables to non-conflicting names that adhere to the naming conventions and
26  * match the parameter (fully qualified name).
27  *
28  * @since 3.3
29  */

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

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

48     public void resolve(TemplateVariable variable, TemplateContext context) {
49         List JavaDoc params= variable.getVariableType().getParams();
50         String JavaDoc param;
51         if (params.size() == 0)
52             param= fDefaultType;
53         else
54             param= (String JavaDoc) params.get(0);
55         JavaContext jc= (JavaContext) context;
56         TemplateVariable ref= jc.getTemplateVariable(param);
57         MultiVariable mv= (MultiVariable) variable;
58         if (ref instanceof MultiVariable) {
59             // reference is another variable
60
MultiVariable refVar= (MultiVariable) ref;
61             jc.addDependency(refVar, mv);
62             
63             refVar.getAllChoices();
64             Object JavaDoc[] types= flatten(refVar.getAllChoices());
65             for (int i= 0; i < types.length; i++) {
66                 String JavaDoc[] names= jc.suggestVariableNames(mv.toString(types[i]));
67                 mv.setChoices(types[i], names);
68             }
69             
70             mv.setKey(refVar.getCurrentChoice());
71             jc.markAsUsed(mv.getDefaultValue());
72         } else {
73             // reference is a Java type name
74
jc.addImport(param);
75             String JavaDoc[] names= jc.suggestVariableNames(param);
76             mv.setChoices(names);
77             jc.markAsUsed(names[0]);
78         }
79     }
80
81     private Object JavaDoc[] flatten(Object JavaDoc[][] allValues) {
82         List JavaDoc flattened= new ArrayList JavaDoc(allValues.length);
83         for (int i= 0; i < allValues.length; i++) {
84             flattened.addAll(Arrays.asList(allValues[i]));
85         }
86         return flattened.toArray(new Object JavaDoc[flattened.size()]);
87     }
88 }
89
Popular Tags