KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > TemplateVariableResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jface.text.templates;
12
13 import org.eclipse.core.runtime.Assert;
14
15 /**
16  * A <code>TemplateVariableResolver</code> resolves <code>TemplateVariables</code>
17  * of a certain type inside a <code>TemplateContext</code>.
18  * <p>
19  * Clients may instantiate and extend this class.
20  * </p>
21  *
22  * @see TemplateVariable
23  * @since 3.0
24  */

25 public class TemplateVariableResolver {
26
27     /** Type of this resolver. */
28     private String JavaDoc fType= null;
29
30     /** Description of the type resolved by this resolver. */
31     private String JavaDoc fDescription= null;
32
33     /**
34      * Creates an instance of <code>TemplateVariableResolver</code>.
35      *
36      * @param type the name of the type
37      * @param description the description for the type
38      */

39     protected TemplateVariableResolver(String JavaDoc type, String JavaDoc description) {
40         setType(type);
41         setDescription(description);
42     }
43
44     /**
45      * Creates an empty instance.
46      * <p>
47      * This is a framework-only constructor that exists only so that resolvers
48      * can be contributed via an extension point and that should not be called
49      * in client code except for subclass constructors; use
50      * {@link #TemplateVariableResolver(String, String)} instead.
51      * </p>
52      */

53     public TemplateVariableResolver() {
54     }
55
56     /**
57      * Returns the type of this resolver.
58      *
59      * @return the type
60      */

61     public String JavaDoc getType() {
62         return fType;
63     }
64
65     /**
66      * Returns the description for the resolver.
67      *
68      * @return the description for the resolver
69      */

70     public String JavaDoc getDescription() {
71         return fDescription;
72     }
73
74     /**
75      * Returns an instance of the type resolved by the receiver available in <code>context</code>.
76      * To resolve means to provide a binding to a concrete text object (a
77      * <code>String</code>) in the given context.
78      * <p>
79      * The default implementation looks up the type in the context.</p>
80      *
81      * @param context the context in which to resolve the type
82      * @return the name of the text object of this type, or <code>null</code> if it cannot be determined
83      */

84     protected String JavaDoc resolve(TemplateContext context) {
85         return context.getVariable(getType());
86     }
87
88     /**
89      * Returns all possible bindings available in <code>context</code>. The default
90      * implementation simply returns an array which contains the result of
91      * {@link #resolve(TemplateContext)}, or an empty array if that call returns
92      * <code>null</code>.
93      *
94      * @param context the context in which to resolve the type
95      * @return an array of possible bindings of this type in <code>context</code>
96      */

97     protected String JavaDoc[] resolveAll(TemplateContext context) {
98         String JavaDoc binding= resolve(context);
99         if (binding == null)
100             return new String JavaDoc[0];
101         return new String JavaDoc[] { binding };
102     }
103
104     /**
105      * Resolves <code>variable</code> in <code>context</code>. To resolve
106      * means to find a valid binding of the receiver's type in the given <code>TemplateContext</code>.
107      * If the variable can be successfully resolved, its value is set using
108      * {@link TemplateVariable#setValues(String[])}.
109      *
110      * @param context the context in which variable is resolved
111      * @param variable the variable to resolve
112      */

113     public void resolve(TemplateVariable variable, TemplateContext context) {
114         String JavaDoc[] bindings= resolveAll(context);
115         if (bindings.length != 0)
116             variable.setValues(bindings);
117         if (bindings.length > 1)
118             variable.setUnambiguous(false);
119         else
120             variable.setUnambiguous(isUnambiguous(context));
121         variable.setResolved(true);
122     }
123
124     /**
125      * Returns whether this resolver is able to resolve unambiguously. When
126      * resolving a <code>TemplateVariable</code>, its <code>isUmambiguous</code>
127      * state is set to the one of this resolver. By default, this method
128      * returns <code>false</code>. Clients can overwrite this method to give
129      * a hint about whether there should be e.g. prompting for input values for
130      * ambiguous variables.
131      *
132      * @param context the context in which the resolved check should be
133      * evaluated
134      * @return <code>true</code> if the receiver is unambiguously resolvable
135      * in <code>context</code>, <code>false</code> otherwise
136      */

137     protected boolean isUnambiguous(TemplateContext context) {
138         return false;
139     }
140
141     /**
142      * Sets the description.
143      * <p>
144      * This is a framework-only method that exists only so that resolvers
145      * can be contributed via an extension point and that should not be called
146      * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
147      * </p>
148      *
149      * @param description the description of this resolver
150      */

151     public final void setDescription(String JavaDoc description) {
152         Assert.isNotNull(description);
153         Assert.isTrue(fDescription == null); // may only be called once when initialized
154
fDescription= description;
155     }
156
157     /**
158      * Sets the type name.
159      * <p>
160      * This is a framework-only method that exists only so that resolvers
161      * can be contributed via an extension point and that should not be called
162      * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
163      * </p>
164      *
165      * @param type the type name of this resolver
166      */

167     public final void setType(String JavaDoc type) {
168         Assert.isNotNull(type);
169         Assert.isTrue(fType == null); // may only be called once when initialized
170
fType= type;
171     }
172 }
173
Popular Tags