001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.editor.codetemplates.api;
043:
044: import java.util.List;
045: import javax.swing.text.JTextComponent;
046: import org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation;
047: import org.netbeans.lib.editor.codetemplates.ParametrizedTextParser;
048:
049: /**
050: * Code template is represented by a parametrized text
051: * that, after pre-processing, can be pasted into a given
052: * text component.
053: * <br/>
054: * Code template instances are either persistent (can be retrieved by
055: * {@link CodeTemplateManager#getCodeTemplates()})
056: * or temporary code templates that can be created
057: * by {@link CodeTemplateManager#createTemporary(String)}.
058: *
059: * @author Miloslav Metelka
060: */
061: public final class CodeTemplate {
062:
063: private final CodeTemplateManagerOperation managerOperation;
064: private final String abbreviation;
065: private final String description;
066: private final String parametrizedText;
067: private final List<String> contexts;
068: private final String mimePath;
069:
070: private String singleLineText = null;
071:
072: CodeTemplate(CodeTemplateManagerOperation managerOperation,
073: String abbreviation, String description,
074: String parametrizedText, List<String> contexts,
075: String mimePath) {
076:
077: assert (managerOperation != null);
078: if (abbreviation == null) {
079: throw new NullPointerException(
080: "abbreviation cannot be null"); // NOI18N
081: }
082: if (parametrizedText == null) {
083: throw new NullPointerException(
084: "parametrizedText cannot be null"); // NOI18N
085: }
086:
087: this .managerOperation = managerOperation;
088: this .abbreviation = abbreviation;
089: this .description = description;
090: this .parametrizedText = parametrizedText;
091: this .contexts = contexts;
092: this .mimePath = mimePath;
093: }
094:
095: /**
096: * Insert this code template into the given text component
097: * at the caret position.
098: *
099: * @param component non-null text component.
100: */
101: public void insert(JTextComponent component) {
102: CodeTemplateManagerOperation.insert(this , component);
103: }
104:
105: /**
106: * Get abbreviation that triggers expansion of this code template.
107: *
108: * @return non-null abbreviation that expands to this template.
109: */
110: public String getAbbreviation() {
111: return abbreviation;
112: }
113:
114: /**
115: * Get textual description of this code template.
116: * <br>
117: * It's being displayed e.g. in the code completion window.
118: *
119: * @return The description text or <code>null</code> if this template does
120: * not have description.
121: */
122: public String getDescription() {
123: return description;
124: }
125:
126: /**
127: * Get the parametrized text of this code template.
128: * <br>
129: * The parameters have form "${...}" and there can be arbitrary
130: * number of them.
131: *
132: * @return non-null parametrized text.
133: */
134: public String getParametrizedText() {
135: return parametrizedText;
136: }
137:
138: public List<String> getContexts() {
139: return contexts;
140: }
141:
142: /**
143: * Api-package accessor's method.
144: * @return CodeTemplateManagerOperation
145: */
146: /* package */CodeTemplateManagerOperation getOperation() {
147: return managerOperation;
148: }
149:
150: /* package */String getSingleLineText() {
151: if (singleLineText == null) {
152: String singleLine;
153: int nlInd = parametrizedText.indexOf('\n'); //NOI18N
154: if (nlInd != -1) {
155: singleLine = parametrizedText.substring(0, nlInd)
156: + "..."; // NOI18N
157: } else {
158: singleLine = parametrizedText;
159: }
160:
161: singleLineText = ParametrizedTextParser.parseToHtml(
162: new StringBuffer(), singleLine).toString();
163: }
164: return singleLineText;
165: }
166:
167: /* package */String getMimePath() {
168: return mimePath;
169: }
170:
171: @Override
172: public String toString() {
173: return "abbrev='" + getAbbreviation() + "', parametrizedText='"
174: + getParametrizedText() + "'"; // NOI18N
175: }
176:
177: }
|