comparison dwtx/jface/text/templates/TemplateVariableResolver.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.templates.TemplateVariableResolver;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.core.runtime.Assert;
18
19 /**
20 * A <code>TemplateVariableResolver</code> resolves <code>TemplateVariables</code>
21 * of a certain type inside a <code>TemplateContext</code>.
22 * <p>
23 * Clients may instantiate and extend this class.
24 * </p>
25 *
26 * @see TemplateVariable
27 * @since 3.0
28 */
29 public class TemplateVariableResolver {
30
31 /** Type of this resolver. */
32 private String fType= null;
33
34 /** Description of the type resolved by this resolver. */
35 private String fDescription= null;
36
37 /**
38 * Creates an instance of <code>TemplateVariableResolver</code>.
39 *
40 * @param type the name of the type
41 * @param description the description for the type
42 */
43 protected TemplateVariableResolver(String type, String description) {
44 setType(type);
45 setDescription(description);
46 }
47
48 /**
49 * Creates an empty instance.
50 * <p>
51 * This is a framework-only constructor that exists only so that resolvers
52 * can be contributed via an extension point and that should not be called
53 * in client code except for subclass constructors; use
54 * {@link #TemplateVariableResolver(String, String)} instead.
55 * </p>
56 */
57 public TemplateVariableResolver() {
58 }
59
60 /**
61 * Returns the type of this resolver.
62 *
63 * @return the type
64 */
65 public String getType() {
66 return fType;
67 }
68
69 /**
70 * Returns the description for the resolver.
71 *
72 * @return the description for the resolver
73 */
74 public String getDescription() {
75 return fDescription;
76 }
77
78 /**
79 * Returns an instance of the type resolved by the receiver available in <code>context</code>.
80 * To resolve means to provide a binding to a concrete text object (a
81 * <code>String</code>) in the given context.
82 * <p>
83 * The default implementation looks up the type in the context.</p>
84 *
85 * @param context the context in which to resolve the type
86 * @return the name of the text object of this type, or <code>null</code> if it cannot be determined
87 */
88 protected String resolve(TemplateContext context) {
89 return context.getVariable(getType());
90 }
91
92 /**
93 * Returns all possible bindings available in <code>context</code>. The default
94 * implementation simply returns an array which contains the result of
95 * {@link #resolve(TemplateContext)}, or an empty array if that call returns
96 * <code>null</code>.
97 *
98 * @param context the context in which to resolve the type
99 * @return an array of possible bindings of this type in <code>context</code>
100 */
101 protected String[] resolveAll(TemplateContext context) {
102 String binding= resolve(context);
103 if (binding is null)
104 return new String[0];
105 return new String[] { binding };
106 }
107
108 /**
109 * Resolves <code>variable</code> in <code>context</code>. To resolve
110 * means to find a valid binding of the receiver's type in the given <code>TemplateContext</code>.
111 * If the variable can be successfully resolved, its value is set using
112 * {@link TemplateVariable#setValues(String[])}.
113 *
114 * @param context the context in which variable is resolved
115 * @param variable the variable to resolve
116 */
117 public void resolve(TemplateVariable variable, TemplateContext context) {
118 String[] bindings= resolveAll(context);
119 if (bindings.length !is 0)
120 variable.setValues(bindings);
121 if (bindings.length > 1)
122 variable.setUnambiguous(false);
123 else
124 variable.setUnambiguous(isUnambiguous(context));
125 variable.setResolved(true);
126 }
127
128 /**
129 * Returns whether this resolver is able to resolve unambiguously. When
130 * resolving a <code>TemplateVariable</code>, its <code>isUmambiguous</code>
131 * state is set to the one of this resolver. By default, this method
132 * returns <code>false</code>. Clients can overwrite this method to give
133 * a hint about whether there should be e.g. prompting for input values for
134 * ambiguous variables.
135 *
136 * @param context the context in which the resolved check should be
137 * evaluated
138 * @return <code>true</code> if the receiver is unambiguously resolvable
139 * in <code>context</code>, <code>false</code> otherwise
140 */
141 protected bool isUnambiguous(TemplateContext context) {
142 return false;
143 }
144
145 /**
146 * Sets the description.
147 * <p>
148 * This is a framework-only method that exists only so that resolvers
149 * can be contributed via an extension point and that should not be called
150 * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
151 * </p>
152 *
153 * @param description the description of this resolver
154 */
155 public final void setDescription(String description) {
156 Assert.isNotNull(description);
157 Assert.isTrue(fDescription is null); // may only be called once when initialized
158 fDescription= description;
159 }
160
161 /**
162 * Sets the type name.
163 * <p>
164 * This is a framework-only method that exists only so that resolvers
165 * can be contributed via an extension point and that should not be called
166 * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
167 * </p>
168 *
169 * @param type the type name of this resolver
170 */
171 public final void setType(String type) {
172 Assert.isNotNull(type);
173 Assert.isTrue(fType is null); // may only be called once when initialized
174 fType= type;
175 }
176 }