KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.lib.editor.codetemplates.CodeTemplateApiPackageAccessor;
26 import org.netbeans.lib.editor.codetemplates.CodeTemplateManagerOperation;
27 import org.netbeans.lib.editor.codetemplates.CodeTemplateSpiPackageAccessor;
28 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateProcessor;
29
30 /**
31  * Code template manager maintains list of code templates
32  * for a particular document type and allows temporary
33  * code templates to be created.
34  *
35  * @author Miloslav Metelka
36  */

37 public final class CodeTemplateManager {
38     
39     static {
40         CodeTemplateApiPackageAccessor.register(new ApiAccessor());
41     }
42     
43     /**
44      * Get an instance of the manager for the given document.
45      *
46      * @param doc document for which the instance of the manager should be obtained.
47      */

48     public static CodeTemplateManager get(Document JavaDoc doc) {
49         return CodeTemplateManagerOperation.getManager(doc);
50     }
51     
52     private CodeTemplateManagerOperation operation;
53     
54     private CodeTemplateManager(CodeTemplateManagerOperation operation) {
55         this.operation = operation;
56     }
57
58     /**
59      * Get unmodifiable collection of the code templates for this manager.
60      * <br/>
61      * This method will block until the code templates will be loaded.
62      *
63      * @return non-null unmodifiable collection of the code templates.
64      */

65     public Collection JavaDoc/*<CodeTemplate>*/ getCodeTemplates() {
66         waitLoaded(); // Wait for the manager to become loaded with the templates.
67
return operation.getCodeTemplates();
68     }
69     
70     /**
71      * Create temporary code template for an ad hoc insertion.
72      *
73      * @param parametrizedText non-null parametrized text of the code template.
74      * @return non-null code template that can be inserted into the document.
75      */

76     public CodeTemplate createTemporary(String JavaDoc parametrizedText) {
77         return new CodeTemplate(operation, "", "", parametrizedText, null); // NOI18N
78
}
79     
80     /**
81      * Check whether asynchronous loading of the code templates into this manager
82      * was already finished.
83      *
84      * @return true if this manager is already loaded with the code templates
85      * or false if the templates are still being loaded.
86      * @see #registerLoadedListener(ChangeListener)
87      */

88     public boolean isLoaded() {
89         return operation.isLoaded();
90     }
91     
92     /**
93      * Wait for this manager to become loaded with the code templates.
94      * <br>
95      * If this manager is already loaded this method returns immediately.
96      */

97     public void waitLoaded() {
98         operation.waitLoaded();
99     }
100
101     /**
102      * Register change listener waiting for the state when this manager
103      * becomes loaded with the code templates.
104      *
105      * <p>
106      * In case the manager is already loaded the registered listener
107      * will be fired immediately in the same thread.
108      *
109      * <p>
110      * There is no unregistration of the listeners because they are
111      * forgotten automatically once the manager becomes loaded.
112      *
113      * @param listener change listener to be fired once this manager becomes loaded
114      * by the code templates.
115      */

116     public void registerLoadedListener(ChangeListener JavaDoc listener) {
117         operation.registerLoadedListener(listener);
118     }
119     
120     CodeTemplateManagerOperation getOperation() {
121         return operation;
122     }
123
124     private static final class ApiAccessor extends CodeTemplateApiPackageAccessor {
125
126         public CodeTemplateManager createCodeTemplateManager(CodeTemplateManagerOperation operation) {
127             return new CodeTemplateManager(operation);
128         }
129
130         public CodeTemplateManagerOperation getOperation(CodeTemplateManager manager) {
131             return manager.getOperation();
132         }
133
134         public CodeTemplateManagerOperation getOperation(CodeTemplate codeTemplate) {
135             return codeTemplate.getOperation();
136         }
137         
138         public CodeTemplate createCodeTemplate(
139         CodeTemplateManagerOperation managerOperation,
140         String JavaDoc abbreviation, String JavaDoc description, String JavaDoc parametrizedText) {
141             return new CodeTemplate(managerOperation, abbreviation, description, parametrizedText, null);
142         }
143         
144     }
145
146 }
147
Popular Tags