KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > api > CodeTemplate


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.codetemplates.api;
21
22 import java.util.List JavaDoc;
23 import javax.swing.text.JTextComponent JavaDoc;
24 import org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation;
25 import org.netbeans.lib.editor.codetemplates.CodeTemplateSpiPackageAccessor;
26 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateInsertRequest;
27
28 /**
29  * Code template is represented by a parametrized text
30  * that, after pre-processing, can be pasted into a given
31  * text component.
32  * <br/>
33  * Code template instances are either persistent (can be retrieved by
34  * {@link CodeTemplateManager#getCodeTemplates()})
35  * or temporary code templates that can be created
36  * by {@link CodeTemplateManager#createTemporary(String)}.
37  *
38  * @author Miloslav Metelka
39  */

40 public final class CodeTemplate {
41     
42     private final CodeTemplateManagerOperation managerOperation;
43     
44     private final String JavaDoc abbreviation;
45     
46     private final String JavaDoc description;
47     
48     private final String JavaDoc parametrizedText;
49     
50     private final List JavaDoc/*<String>*/ contexts;
51     
52     CodeTemplate(CodeTemplateManagerOperation managerOperation,
53     String JavaDoc abbreviation, String JavaDoc description, String JavaDoc parametrizedText, List JavaDoc/*<String>*/ contexts) {
54         
55         assert (managerOperation != null);
56         if (abbreviation == null) {
57             throw new NullPointerException JavaDoc("abbreviation cannot be null"); // NOI18N
58
}
59         if (description == null) {
60             throw new NullPointerException JavaDoc("description cannot be null"); // NOI18N
61
}
62         if (parametrizedText == null) {
63             throw new NullPointerException JavaDoc("parametrizedText cannot be null"); // NOI18N
64
}
65
66         this.managerOperation = managerOperation;
67         this.abbreviation = abbreviation;
68         this.description = description;
69         this.parametrizedText = parametrizedText;
70         this.contexts = contexts;
71     }
72
73     /**
74      * Insert this code template into the given text component
75      * at the caret position.
76      *
77      * @param component non-null text component.
78      */

79     public void insert(JTextComponent JavaDoc component) {
80         managerOperation.insert(this, component);
81     }
82
83     /**
84      * Get abbreviation that triggers expansion of this code template.
85      *
86      * @return non-null abbreviation that expands to this template.
87      */

88     public String JavaDoc getAbbreviation() {
89         return abbreviation;
90     }
91
92     /**
93      * Get textual description of this code template.
94      * <br>
95      * It's being displayed e.g. in the code completion window.
96      *
97      * @return non-null description text.
98      */

99     public String JavaDoc getDescription() {
100         return description;
101     }
102
103     /**
104      * Get the parametrized text of this code template.
105      * <br>
106      * The parameters have form "${...}" and there can be arbitrary
107      * number of them.
108      *
109      * @return non-null parametrized text.
110      */

111     public String JavaDoc getParametrizedText() {
112         return parametrizedText;
113     }
114     
115     public List JavaDoc/*<String>*/ getContexts() {
116         return contexts;
117     }
118
119     /**
120      * Api-package accessor's method.
121      */

122     CodeTemplateManagerOperation getOperation() {
123         return managerOperation;
124     }
125
126     public String JavaDoc toString() {
127         return "abbrev='" + getAbbreviation() + "', parametrizedText='" + getParametrizedText() + "'"; // NOI18N
128
}
129     
130 }
131
Popular Tags