Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ce50581d41d45c65926de69ad5337c4d4c4e1b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.templates;

import org.eclipse.jface.text.Assert;

/**
 * A <code>TemplateVariable</code> represents a set of positions into a
 * <code>TemplateBuffer</code> with identical content each. <code>TemplateVariableResolver</code>s
 * can be used to resolve a template variable to a symbol available from the
 * <code>TemplateContext</code>.
 * <p>
 * Clients may instantiate and extend this class.
 * </p>
 * 
 * @see TemplateVariableResolver
 * @see TemplateBuffer
 * @since 3.0
 */
public class TemplateVariable {

	/** The type name of the variable */
	private final String fType;
	/** The name of the variable. */
	private final String fName;
	/** The offsets of the variable. */
	private int[] fOffsets;
	/** Flag indicating if the variable has been resolved unambiguously. */
	private boolean fIsUnambiguous;
	/**
	 * The proposal strings available for this variable. The first string is
	 * the default value.
	 */
	private String[] fValues;
	
	/**
	 * Creates a template variable. The type is used as the name of the
	 * variable.
	 * 
	 * @param type the type of the variable
	 * @param defaultValue the default value of the variable
	 * @param offsets the array of offsets of the variable
	 */
	public TemplateVariable(String type, String defaultValue, int[] offsets) {
		this(type, new String[] { defaultValue }, offsets);
	}

	/**
	 * Creates a template variable.
	 * 
	 * @param type the type of the variable
	 * @param name the name of the variable
	 * @param defaultValue the default value of the variable
	 * @param offsets the array of offsets of the variable
	 */
	public TemplateVariable(String type, String name, String defaultValue, int[] offsets) {
		this(type, name, new String[] { defaultValue }, offsets);
	}

	/**
	 * Creates a template variable with multiple possible values. The type is
	 * used as the name of the template.
	 * 
	 * @param type the type of the template variable
	 * @param values the values available at this variable, non-empty
	 * @param offsets the array of offsets of the variable
	 */
	public TemplateVariable(String type, String[] values, int[] offsets) {
		this(type, type, values, offsets);
	}

	/**
	 * Creates a template variable with multiple possible values.
	 * 
	 * @param type the type of the variable
	 * @param name the name of the variable
	 * @param values the values available at this variable, non-empty
	 * @param offsets the array of offsets of the variable
	 */
	public TemplateVariable(String type, String name, String[] values, int[] offsets) {
		Assert.isNotNull(type);
		Assert.isNotNull(name);
		fType= type;
		fName= name;
		setValues(values);
		setOffsets(offsets);
		setUnambiguous(false);
	}

	/**
	 * Returns the type of the variable.
	 * 
	 * @return the type of the variable
	 */
	public String getType() {
	    return fType;
	}	

	/**
	 * Returns the name of the variable.
	 * 
	 * @return the name of the variable
	 */
	public String getName() {
	    return fName;
	}	

	/**
	 * Returns the default value of the variable.
	 * 
	 * @return the default value of the variable
	 */
	public String getDefaultValue() {
	 	return getValues()[0];
	}
	
	/**
	 * Returns the possible values for this variable. The returned array is
	 * owned by this variable and must not be modified.
	 * 
	 * @return the possible values for this variable
	 */
	public String[] getValues() {
		return fValues;
	}
	
	/**
	 * Returns the length of the variable.
	 * 
	 * @return the length of the variable
	 */
	public int getLength() {
	 	return getDefaultValue().length();   
	}
	
	/**
	 * Sets the offsets of the variable.
	 * 
	 * @param offsets the new offsets of the variable
	 */
	public void setOffsets(int[] offsets) {
	 	fOffsets= offsets; 
	}
	
	/**
	 * Returns the offsets of the variable.
	 * 
	 * @return the length of the variable
	 */
	public int[] getOffsets() {
	 	return fOffsets;   
	}
	
	/**
	 * Sets the default value for this variable. This is a shortcut for
	 * <code>setValues(new String[] { value })</code>.
	 * 
	 * @param value the new default value
	 */
	public final void setValue(String value) {
		setValues(new String[] { value });
	}
	
	/**
	 * Sets the possible values for this variable, with the first being the
	 * default value.
	 * 
	 * @param values a non-empty array of values
	 */
	public void setValues(String[] values) {
		Assert.isTrue(values.length > 0);
		fValues= values;
	}

	/**
	 * Sets the isUnambiguous flag of the variable.
	 * 
	 * @param unambiguous the new unambiguous state of the variable
	 */
	public void setUnambiguous(boolean unambiguous) {
	    fIsUnambiguous= unambiguous;
	}

	/**
	 * Returns <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise.
	 * 
	 * @return <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise
	 */	
	public boolean isUnambiguous() {
	 	return fIsUnambiguous;   
	}

}

Back to the top