KickJava   Java API By Example, From Geeks To Geeks.

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


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 template consisting of a name and a pattern.
17  * <p>
18  * Clients may instantiate this class. May become final in the future.
19  * </p>
20  * @since 3.0
21  */

22 public class Template {
23
24     /** The name of this template */
25     private /*final*/ String JavaDoc fName;
26     /** A description of this template */
27     private /*final*/ String JavaDoc fDescription;
28     /** The name of the context type of this template */
29     private /*final*/ String JavaDoc fContextTypeId;
30     /** The template pattern. */
31     private /*final*/ String JavaDoc fPattern;
32     /**
33      * The auto insertable property.
34      * @since 3.1
35      */

36     private final boolean fIsAutoInsertable;
37
38     /**
39      * Creates an empty template.
40      */

41     public Template() {
42         this("", "", "", "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
43
}
44
45     /**
46      * Creates a copy of a template.
47      *
48      * @param template the template to copy
49      */

50     public Template(Template template) {
51         this(template.getName(), template.getDescription(), template.getContextTypeId(), template.getPattern(), template.isAutoInsertable());
52     }
53
54     /**
55      * Creates a template.
56      *
57      * @param name the name of the template
58      * @param description the description of the template
59      * @param contextTypeId the id of the context type in which the template can be applied
60      * @param pattern the template pattern
61      * @deprecated as of 3.1 replaced by {@link #Template(String, String, String, String, boolean)}
62      */

63     public Template(String JavaDoc name, String JavaDoc description, String JavaDoc contextTypeId, String JavaDoc pattern) {
64         this(name, description, contextTypeId, pattern, true); // templates are auto insertable per default
65
}
66
67     /**
68      * Creates a template.
69      *
70      * @param name the name of the template
71      * @param description the description of the template
72      * @param contextTypeId the id of the context type in which the template can be applied
73      * @param pattern the template pattern
74      * @param isAutoInsertable the auto insertable property of the template
75      * @since 3.1
76      */

77     public Template(String JavaDoc name, String JavaDoc description, String JavaDoc contextTypeId, String JavaDoc pattern, boolean isAutoInsertable) {
78         Assert.isNotNull(description);
79         fDescription= description;
80         fName= name;
81         Assert.isNotNull(contextTypeId);
82         fContextTypeId= contextTypeId;
83         fPattern= pattern;
84         fIsAutoInsertable= isAutoInsertable;
85     }
86
87     /*
88      * @see Object#hashCode()
89      */

90     public int hashCode() {
91         return fName.hashCode() ^ fPattern.hashCode() ^ fContextTypeId.hashCode();
92     }
93
94     /**
95      * Sets the description of the template.
96      *
97      * @param description the new description
98      * @deprecated Templates should never be modified
99      */

100     public void setDescription(String JavaDoc description) {
101         Assert.isNotNull(description);
102         fDescription= description;
103     }
104
105     /**
106      * Returns the description of the template.
107      *
108      * @return the description of the template
109      */

110     public String JavaDoc getDescription() {
111         return fDescription;
112     }
113
114     /**
115      * Sets the name of the context type in which the template can be applied.
116      *
117      * @param contextTypeId the new context type name
118      * @deprecated Templates should never be modified
119      */

120     public void setContextTypeId(String JavaDoc contextTypeId) {
121         Assert.isNotNull(contextTypeId);
122         fContextTypeId= contextTypeId;
123     }
124
125     /**
126      * Returns the id of the context type in which the template can be applied.
127      *
128      * @return the id of the context type in which the template can be applied
129      */

130     public String JavaDoc getContextTypeId() {
131         return fContextTypeId;
132     }
133
134     /**
135      * Sets the name of the template.
136      *
137      * @param name the name of the template
138      * @deprecated Templates should never be modified
139      */

140     public void setName(String JavaDoc name) {
141         fName= name;
142     }
143
144     /**
145      * Returns the name of the template.
146      *
147      * @return the name of the template
148      */

149     public String JavaDoc getName() {
150         return fName;
151     }
152
153     /**
154      * Sets the pattern of the template.
155      *
156      * @param pattern the new pattern of the template
157      * @deprecated Templates should never be modified
158      */

159     public void setPattern(String JavaDoc pattern) {
160         fPattern= pattern;
161     }
162
163     /**
164      * Returns the template pattern.
165      *
166      * @return the template pattern
167      */

168     public String JavaDoc getPattern() {
169         return fPattern;
170     }
171
172     /**
173      * Returns <code>true</code> if template is enabled and matches the context,
174      * <code>false</code> otherwise.
175      *
176      * @param prefix the prefix (e.g. inside a document) to match
177      * @param contextTypeName the context type name to match
178      * @return <code>true</code> if template is enabled and matches the context,
179      * <code>false</code> otherwise
180      */

181     public boolean matches(String JavaDoc prefix, String JavaDoc contextTypeName) {
182         return fContextTypeId.equals(contextTypeName);
183     }
184
185     /*
186      * @see java.lang.Object#equals(java.lang.Object)
187      */

188     public boolean equals(Object JavaDoc o) {
189         if (!(o instanceof Template))
190             return false;
191
192         Template t= (Template) o;
193         if (t == this)
194             return true;
195
196         return t.fName.equals(fName)
197                 && t.fPattern.equals(fPattern)
198                 && t.fContextTypeId.equals(fContextTypeId)
199                 && t.fDescription.equals(fDescription)
200                 && t.fIsAutoInsertable == fIsAutoInsertable;
201     }
202
203     /**
204      * Returns the auto insertable property of the template.
205      *
206      * @return the auto insertable property of the template
207      * @since 3.1
208      */

209     public boolean isAutoInsertable() {
210         return fIsAutoInsertable;
211     }
212 }
213
Popular Tags