Mercurial > projects > dwt-addons
comparison dwtx/jface/text/templates/TemplateVariable.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, 2007 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.TemplateVariable; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 | |
18 import dwtx.core.runtime.Assert; | |
19 import dwtx.jface.text.TextUtilities; | |
20 | |
21 /** | |
22 * A <code>TemplateVariable</code> represents a set of positions into a | |
23 * <code>TemplateBuffer</code> with identical content each. <code>TemplateVariableResolver</code>s | |
24 * can be used to resolve a template variable to a symbol available from the | |
25 * <code>TemplateContext</code>. A resolved variable may have one or more possible | |
26 * {@link #getValues() values} which may be presented to the user as choices. If there is no user | |
27 * interaction the {@link #getDefaultValue() default value} is chosen as textual representation of | |
28 * the variable. | |
29 * <p> | |
30 * Clients may instantiate and extend this class. | |
31 * </p> | |
32 * | |
33 * @see TemplateVariableResolver | |
34 * @see TemplateBuffer | |
35 * @since 3.0 | |
36 */ | |
37 public class TemplateVariable { | |
38 | |
39 /** The type name of the variable */ | |
40 private final TemplateVariableType fType; | |
41 /** The name of the variable. */ | |
42 private final String fName; | |
43 /** The initial length in the template pattern. */ | |
44 private final int fInitialLength; | |
45 /** The offsets of the variable. */ | |
46 private int[] fOffsets; | |
47 /** Flag indicating if the variable has been resolved unambiguously. */ | |
48 private bool fIsUnambiguous; | |
49 /** Flag indicating if the variable has been resolved by a resolver. */ | |
50 private bool fIsResolved; | |
51 /** | |
52 * The proposal strings available for this variable. The first string is | |
53 * the default value. | |
54 */ | |
55 private String[] fValues; | |
56 | |
57 /** | |
58 * Creates a template variable. The type is used as the name of the | |
59 * variable. | |
60 * | |
61 * @param type the type of the variable | |
62 * @param defaultValue the default value of the variable | |
63 * @param offsets the array of offsets of the variable | |
64 */ | |
65 public TemplateVariable(String type, String defaultValue, int[] offsets) { | |
66 this(type, new String[] { defaultValue }, offsets); | |
67 } | |
68 | |
69 /** | |
70 * Creates a template variable. | |
71 * | |
72 * @param type the type of the variable | |
73 * @param name the name of the variable | |
74 * @param defaultValue the default value of the variable | |
75 * @param offsets the array of offsets of the variable | |
76 */ | |
77 public TemplateVariable(String type, String name, String defaultValue, int[] offsets) { | |
78 this(type, name, new String[] { defaultValue }, offsets); | |
79 } | |
80 | |
81 /** | |
82 * Creates a template variable. | |
83 * | |
84 * @param type the type of the variable | |
85 * @param name the name of the variable | |
86 * @param defaultValue the default value of the variable | |
87 * @param offsets the array of offsets of the variable | |
88 * @since 3.3 | |
89 */ | |
90 public TemplateVariable(TemplateVariableType type, String name, String defaultValue, int[] offsets) { | |
91 this(type, name, new String[] { defaultValue }, offsets); | |
92 } | |
93 | |
94 /** | |
95 * Creates a template variable with multiple possible values. The type is | |
96 * used as the name of the template. | |
97 * | |
98 * @param type the type of the template variable | |
99 * @param values the values available at this variable, non-empty | |
100 * @param offsets the array of offsets of the variable | |
101 */ | |
102 public TemplateVariable(String type, String[] values, int[] offsets) { | |
103 this(type, type, values, offsets); | |
104 } | |
105 | |
106 /** | |
107 * Creates a template variable with multiple possible values. | |
108 * | |
109 * @param type the type of the variable | |
110 * @param name the name of the variable | |
111 * @param values the values available at this variable, non-empty | |
112 * @param offsets the array of offsets of the variable | |
113 */ | |
114 public TemplateVariable(String type, String name, String[] values, int[] offsets) { | |
115 this(new TemplateVariableType(type), name, values, offsets); | |
116 } | |
117 | |
118 /** | |
119 * Creates a template variable with multiple possible values. | |
120 * | |
121 * @param type the type of the variable | |
122 * @param name the name of the variable | |
123 * @param values the values available at this variable, non-empty | |
124 * @param offsets the array of offsets of the variable | |
125 * @since 3.3 | |
126 */ | |
127 TemplateVariable(TemplateVariableType type, String name, String[] values, int[] offsets) { | |
128 Assert.isNotNull(type); | |
129 Assert.isNotNull(name); | |
130 fType= type; | |
131 fName= name; | |
132 setValues(values); | |
133 setOffsets(offsets); | |
134 setUnambiguous(false); | |
135 setResolved(false); | |
136 fInitialLength= values[0].length(); | |
137 } | |
138 | |
139 /** | |
140 * Returns the type name of the variable. | |
141 * | |
142 * @return the type name of the variable | |
143 */ | |
144 public String getType() { | |
145 return fType.getName(); | |
146 } | |
147 | |
148 /** | |
149 * Returns the type of the variable. | |
150 * | |
151 * @return the type of the variable | |
152 * @since 3.3 | |
153 */ | |
154 public TemplateVariableType getVariableType() { | |
155 return fType; | |
156 } | |
157 | |
158 /** | |
159 * Returns the name of the variable. | |
160 * | |
161 * @return the name of the variable | |
162 */ | |
163 public String getName() { | |
164 return fName; | |
165 } | |
166 | |
167 /** | |
168 * Returns the default value of the variable. Typically, this is the first of | |
169 * the possible values (see {@link #getValues()}. | |
170 * | |
171 * @return the default value of the variable | |
172 */ | |
173 public String getDefaultValue() { | |
174 return getValues()[0]; | |
175 } | |
176 | |
177 /** | |
178 * Returns the possible values for this variable. The returned array is owned by this variable | |
179 * and must not be modified. The array is not empty. | |
180 * | |
181 * @return the possible values for this variable | |
182 */ | |
183 public String[] getValues() { | |
184 return fValues; | |
185 } | |
186 | |
187 /** | |
188 * Returns the length of the variable's default value. | |
189 * | |
190 * @return the length of the variable | |
191 */ | |
192 public int getLength() { | |
193 return getDefaultValue().length(); | |
194 } | |
195 | |
196 /** | |
197 * Returns the initial length of the variable. The initial length is the lenght as it occurred | |
198 * in the template pattern and is used when resolving a template to update the pattern with the | |
199 * resolved values of the variable. | |
200 * | |
201 * @return the initial length of the variable | |
202 * @since 3.3 | |
203 */ | |
204 final int getInitialLength() { | |
205 return fInitialLength; | |
206 } | |
207 | |
208 /** | |
209 * Sets the offsets of the variable. | |
210 * | |
211 * @param offsets the new offsets of the variable | |
212 */ | |
213 public void setOffsets(int[] offsets) { | |
214 fOffsets= TextUtilities.copy(offsets); | |
215 } | |
216 | |
217 /** | |
218 * Returns the offsets of the variable. The returned array is | |
219 * owned by this variable and must not be modified. | |
220 * | |
221 * @return the length of the variable | |
222 */ | |
223 public int[] getOffsets() { | |
224 return fOffsets; | |
225 } | |
226 | |
227 /** | |
228 * Resolves the variable to a single value. This is a shortcut for | |
229 * <code>setValues(new String[] { value })</code>. | |
230 * | |
231 * @param value the new default value | |
232 */ | |
233 public final void setValue(String value) { | |
234 setValues(new String[] { value }); | |
235 } | |
236 | |
237 /** | |
238 * Resolves the variable to several possible values for this variable, with the first being the | |
239 * default value. | |
240 * | |
241 * @param values a non-empty array of values | |
242 */ | |
243 public void setValues(String[] values) { | |
244 Assert.isTrue(values.length > 0); | |
245 fValues= TextUtilities.copy(values); | |
246 setResolved(true); | |
247 } | |
248 | |
249 /** | |
250 * Sets the <em>isUnambiguous</em> flag of the variable. | |
251 * | |
252 * @param unambiguous the new unambiguous state of the variable | |
253 */ | |
254 public void setUnambiguous(bool unambiguous) { | |
255 fIsUnambiguous= unambiguous; | |
256 if (unambiguous) | |
257 setResolved(true); | |
258 } | |
259 | |
260 /** | |
261 * Returns <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise. | |
262 * | |
263 * @return <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise | |
264 */ | |
265 public bool isUnambiguous() { | |
266 return fIsUnambiguous; | |
267 } | |
268 | |
269 /** | |
270 * Sets the <em>resolved</em> flag of the variable. | |
271 * | |
272 * @param resolved the new <em>resolved</em> state | |
273 * @since 3.3 | |
274 */ | |
275 public void setResolved(bool resolved) { | |
276 fIsResolved= resolved; | |
277 } | |
278 | |
279 /** | |
280 * Returns <code>true</code> if the variable has been resolved, <code>false</code> | |
281 * otherwise. | |
282 * | |
283 * @return <code>true</code> if the variable has been resolved, <code>false</code> otherwise | |
284 * @since 3.3 | |
285 */ | |
286 public bool isResolved() { | |
287 return fIsResolved; | |
288 } | |
289 } |