2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.api.editor.settings;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.List;
49
* The definition of a code template. A code template is basically a piece of
50
* code with an abbreviation associated to it. When a user types the abbreviation
51
* to the editor and presses the expansion key the code associated with the
52
* abbreviation gets expanded. The code can contain various parameter that the user
53
* can enter during the expansion.
55
* <p>The <code>CodeTemplateDescription</code>s can be obtained from
56
* <code>CodeTemplateSettings</code> class that can be loaded from <code>MimeLookup</code>
57
* for a particular mime type. See the example below.
60
* Lookup l = MimeLookup.getLookup(MimePath.parse(mimePath));
61
* CodeTemplateSettings cds = l.lookup(CodeTemplateSettings.class);
62
* List<CodeTemplateDescription> codeTemplates = cds.getCodeTemplateDescriptions();
65
* @see CodeTemplateSettings
66
* @author Miloslav Metelka
68
public final class CodeTemplateDescription {
70
private final String abbreviation;
71
private final String description;
72
private final String parametrizedText;
73
private final List<String> contexts;
74
private final String uniqueId;
77
* Creates a new code template description. It call the other constructor
78
* passing <code>null</code> for the <code>contexts</code> parameter.
80
* @param abbreviation The abbreviation text that expands this code template.
81
* @param description The code template's display text.
82
* @param parametrizedText The actual code template that will get expanded when
83
* a user writes the abbreviation in the editor.
85
public CodeTemplateDescription(String abbreviation, String description, String parametrizedText) {
86
this(abbreviation, description, parametrizedText, null, null);
90
* Creates a new code template description.
92
* <p>Usually clients do not need to create <code>CodeTemplateDescription</code>s
93
* by themselvs. Instead they use <code>MimeLookup</code> and <code>CodeTemplateSettings</code>
94
* to access code templates registered in the system.
96
* @param abbreviation The abbreviation text that expands this code template.
97
* @param description The code template's display text.
98
* Can be <code>null</code>
99
* @param parametrizedText The actual code template that will get expanded when
100
* a user writes the abbreviation in the editor.
101
* @param contexts The list of context ids that apply for this code template.
102
* Can be <code>null</code>
103
* @param uniqueId The id uniquely identifying this template. If you pass
104
* non-<code>null</code> value, please make sure that it is really a unique
105
* id for this template. Can be <code>null</code>.
107
public CodeTemplateDescription(
110
String parametrizedText,
111
List<String> contexts,
114
assert abbreviation != null : "The abbreviation parameter can't be null"; //NOI18N
115
assert parametrizedText != null : "The parametrizedText parameter can't be null"; //NOI18N
117
this.abbreviation = abbreviation;
118
this.description = description;
119
this.parametrizedText = parametrizedText;
120
this.contexts = contexts == null ?
121
Collections.<String>emptyList() :
122
Collections.unmodifiableList(new ArrayList<String>(contexts));
123
this.uniqueId = uniqueId;
127
* Gets the abbreviation text that triggers expansion of this code template.
129
* <p>The abbreviation text should be unique among all code templates defined
130
* for a one mime type so that each code template can be expanded individually.
132
* @return The abbreviation text that expands this code template.
134
public String getAbbreviation() {
139
* Gets textual description of this code template. It's a display text
140
* that can be shown in UI such as the code completion window or Tools-Options
143
* @return The display text for this code template or <code>null</code> if this
144
* code template has no descriptions.
146
public String getDescription() {
151
* Gets the code text of this code template.
153
* This is the text that will be expanded when a user types the abbreviation
154
* in the editor and presses the expansion key. The text can contain parameters
155
* in the form of "${...}".
157
* @return The code text with parameters.
159
public String getParametrizedText() {
160
return parametrizedText;
164
* Gets the list of contexts that apply for this code template. The contexts
165
* are simply unique identifiers used by the infrastructure to filter out
166
* code templates that are not suitable for the editor context, where a user
169
* <p>The actual identifiers are defined by each particular language (mime type)
170
* and can be different for different languages. The language defines contexts
171
* for its constructs such as loops, methods, classes, if-else blocks, etc. and
172
* than tags each code template available for that language with a context,
173
* where it is meaningful to apply the template.
175
* @return The contexts for this code template.
177
public List<String> getContexts() {
182
* Gets an id that can be used for identifying this template. A code template
183
* does not generally have to have a unique id, but if it has one it is
184
* guaranteed to uniquely identify the template.
186
* <p class="nonnormative">Unique ids can be useful for tools importing and
187
* exporting code templates from other applications such as TextMate, etc.
189
* @return The unique id or <code>null</code>.
192
public String getUniqueId() {
196
public String toString() {
197
return "abbrev='" + getAbbreviation() + "', parametrizedText='" + getParametrizedText() + "'"; // NOI18N