Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce00cd7349c87d9b270f7d2e100525011979fa29 (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
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.templates;

import org.eclipse.jface.text.Assert;

/**
 * A template consisting of a name and a pattern.
 * <p>
 * Clients may instantiate this class. May become final in the future.
 * </p>
 * @since 3.0
 */
public class Template {

	/** The name of this template */
	private /*final*/ String fName;
	/** A description of this template */
	private /*final*/ String fDescription;
	/** The name of the context type of this template */
	private /*final*/ String fContextTypeId;
	/** The template pattern. */
	private /*final*/ String fPattern;
	/**
	 * The auto insertable property. 
	 * @since 3.1
	 */
	private final boolean fIsAutoInsertable;

	/**
	 * Creates an empty template.
	 */
	public Template() {
		this("", "", "", "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	/**
	 * Creates a copy of a template.
	 *
	 * @param template the template to copy
	 */
	public Template(Template template) {
		this(template.getName(), template.getDescription(), template.getContextTypeId(), template.getPattern(), template.isAutoInsertable());
	}

	/**
	 * Creates a template.
	 *
	 * @param name the name of the template
	 * @param description the description of the template
	 * @param contextTypeId the id of the context type in which the template can be applied
	 * @param pattern the template pattern
	 * @deprecated as of 3.1 replaced by {@link #Template(String, String, String, String, boolean)}
	 */
	public Template(String name, String description, String contextTypeId, String pattern) {
		this(name, description, contextTypeId, pattern, true); // templates are auto insertable per default
	}

	/**
	 * Creates a template.
	 *
	 * @param name the name of the template
	 * @param description the description of the template
	 * @param contextTypeId the id of the context type in which the template can be applied
	 * @param pattern the template pattern
	 * @param isAutoInsertable the auto insertable property of the template
	 * @since 3.1
	 */
	public Template(String name, String description, String contextTypeId, String pattern, boolean isAutoInsertable) {
		Assert.isNotNull(description);
		fDescription= description;
		fName= name;
		Assert.isNotNull(contextTypeId);
		fContextTypeId= contextTypeId;
		fPattern= pattern;
		fIsAutoInsertable= isAutoInsertable;
	}

	/*
	 * @see Object#hashCode()
	 */
	public int hashCode() {
		return fName.hashCode() ^ fPattern.hashCode() ^ fContextTypeId.hashCode();
	}

	/**
	 * Sets the description of the template.
	 *
	 * @param description the new description
	 * @deprecated Templates should never be modified
	 */
	public void setDescription(String description) {
		Assert.isNotNull(description);
		fDescription= description;
	}

	/**
	 * Returns the description of the template.
	 *
	 * @return the description of the template
	 */
	public String getDescription() {
		return fDescription;
	}

	/**
	 * Sets the name of the context type in which the template can be applied.
	 *
	 * @param contextTypeId the new context type name
	 * @deprecated Templates should never be modified
	 */
	public void setContextTypeId(String contextTypeId) {
		Assert.isNotNull(contextTypeId);
		fContextTypeId= contextTypeId;
	}

	/**
	 * Returns the id of the context type in which the template can be applied.
	 *
	 * @return the id of the context type in which the template can be applied
	 */
	public String getContextTypeId() {
		return fContextTypeId;
	}

	/**
	 * Sets the name of the template.
	 *
	 * @param name the name of the template
	 * @deprecated Templates should never be modified
	 */
	public void setName(String name) {
		fName= name;
	}

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

	/**
	 * Sets the pattern of the template.
	 *
	 * @param pattern the new pattern of the template
	 * @deprecated Templates should never be modified
	 */
	public void setPattern(String pattern) {
		fPattern= pattern;
	}

	/**
	 * Returns the template pattern.
	 *
	 * @return the template pattern
	 */
	public String getPattern() {
		return fPattern;
	}

	/**
	 * Returns <code>true</code> if template is enabled and matches the context,
	 * <code>false</code> otherwise.
	 *
	 * @param prefix the prefix (e.g. inside a document) to match
	 * @param contextTypeName the context type name to match
	 * @return <code>true</code> if template is enabled and matches the context,
	 * <code>false</code> otherwise
	 */
	public boolean matches(String prefix, String contextTypeName) {
		return fContextTypeId.equals(contextTypeName);
	}

	/*
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object o) {
		if (!(o instanceof Template))
			return false;

		Template t= (Template) o;
		if (t == this)
			return true;

		return t.fName.equals(fName)
				&& t.fPattern.equals(fPattern)
				&& t.fContextTypeId.equals(fContextTypeId)
				&& t.fDescription.equals(fDescription)
				&& t.fIsAutoInsertable == fIsAutoInsertable;
	}

	/**
	 * Returns the auto insertable property of the template.
	 * 
	 * @return the auto insertable property of the template
	 * @since 3.1
	 */
	public boolean isAutoInsertable() {
		return fIsAutoInsertable;
	}
}

Back to the top