KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > Registry


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.editor;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.EventListenerList JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.modules.editor.lib2.DocumentsRegistry;
31 import org.openide.util.WeakListeners;
32
33 /**
34 * All the documents and components register here so that
35 * they become available to the processing that crosses
36 * different components and documents such as cross document
37 * position stack or word matching.
38 *
39 * @author Miloslav Metelka
40 * @version 1.00
41 * @deprecated Without any replacement.
42 */

43 public class Registry {
44
45     private static final EventListenerList JavaDoc listenerList
46     = new EventListenerList JavaDoc();
47
48     private static final PropertyChangeListener JavaDoc PCL = new PropertyChangeListener JavaDoc() {
49         public void propertyChange(PropertyChangeEvent JavaDoc pce) {
50             ChangeListener JavaDoc[] listeners
51                 = (ChangeListener JavaDoc[])listenerList.getListeners(ChangeListener JavaDoc.class);
52             ChangeEvent JavaDoc evt = new ChangeEvent JavaDoc(Registry.class);
53             for (int i = 0; i < listeners.length; i++) {
54                 listeners[i].stateChanged(evt);
55             }
56         }
57     };
58
59     static {
60         DocumentsRegistry.addPropertyChangeListener(PCL);
61     }
62     
63     /** Add weak listener to listen to change of activity of documents or components.
64      * The caller must
65      * hold the listener object in some instance variable to prevent it
66      * from being garbage collected.
67      * @param l listener to add
68      */

69     public static void addChangeListener(ChangeListener JavaDoc l) {
70         listenerList.add(ChangeListener JavaDoc.class, WeakListeners.change(l, null));
71     }
72
73     /** Remove listener for changes in activity. It's optional
74      * to remove the listener. It would be done automatically
75      * if the object holding the listener would be garbage collected.
76      * @param l listener to remove
77      */

78     public static void removeChangeListener(ChangeListener JavaDoc l) {
79         // no-op
80
}
81
82     /** Get document ID from the document.
83      * @return document id or -1 if document was not yet added to the registry
84      * by <code>addDocument()</code>.
85      */

86     public static int getID(BaseDocument doc) {
87         return DocumentsRegistry.getID(doc);
88     }
89     
90     /** Get component ID from the component.
91      * @return component id or -1 if component was not yet added to the registry
92      * by <code>addComponent()</code>.
93      */

94     public static int getID(JTextComponent JavaDoc c) {
95         return DocumentsRegistry.getID(c);
96     }
97
98     /** Get document when its ID is known.
99      * It's rather cheap operation.
100      * @param docID document ID. It can be retrieved from the document
101      * by <code>getID(doc)</code>.
102      * @return document instance or null when document no longer exists
103      */

104     public static BaseDocument getDocument(int docID) {
105         // TODO: wrap non-BaseDocuments somehow
106
Document JavaDoc doc = DocumentsRegistry.getDocument(docID);
107         return doc instanceof BaseDocument ? (BaseDocument) doc : null;
108     }
109
110     /** Get component when its ID is known.
111      * It's rather cheap operation.
112      * @param compID component ID. It can be retrieved from the component
113      * by <code>getID(c)</code>.
114      * @return component instance or null when document no longer exists
115      */

116     public static JTextComponent JavaDoc getComponent(int compID) {
117         return DocumentsRegistry.getComponent(compID);
118     }
119
120     /** Add document to registry. Doesn't search for repetitive
121      * adding.
122      * @return registry unique ID of the document
123      */

124     public static int addDocument(BaseDocument doc) {
125         return DocumentsRegistry.addDocument(doc);
126     }
127
128     /** Add component to registry. If the component is already registered
129      * it returns the existing} ID. The document that is currently assigned
130      * to the component is _not_ registered automatically.
131      * @return ID of the component
132      */

133     public static synchronized int addComponent(JTextComponent JavaDoc c) {
134         return DocumentsRegistry.addComponent(c);
135     }
136
137     /** Remove component from registry. It's usually done when
138      * the UI of the component is being deinstalled.
139      * @return ID that the component had in the registry. The possible
140      * new ID will be different from this one. -1 will be returned
141      * if the component was not yet added to the registry.
142      */

143     public static synchronized int removeComponent(JTextComponent JavaDoc c) {
144         return DocumentsRegistry.removeComponent(c);
145     }
146
147     /** Put the component to the first position in the array of last accessed
148     * components. The activate of document is also called automatically.
149     */

150     public static void activate(JTextComponent JavaDoc c) {
151         DocumentsRegistry.activate(c);
152     }
153
154     /** Put the document to the first position in the array of last accessed
155      * documents. The document must be registered otherwise nothing
156      * is done.
157      * @param doc document to be activated
158      */

159     public static void activate(BaseDocument doc) {
160         DocumentsRegistry.activate(doc);
161     }
162     
163     public static synchronized BaseDocument getMostActiveDocument() {
164         // TODO: wrap non-BaseDocuments somehow
165
Document JavaDoc doc = DocumentsRegistry.getMostActiveDocument();
166         return doc instanceof BaseDocument ? (BaseDocument) doc : null;
167     }
168
169     public static synchronized BaseDocument getLeastActiveDocument() {
170         // TODO: wrap non-BaseDocuments somehow
171
Document JavaDoc doc = DocumentsRegistry.getLeastActiveDocument();
172         return doc instanceof BaseDocument ? (BaseDocument) doc : null;
173     }
174
175     public static BaseDocument getLessActiveDocument(BaseDocument doc) {
176         // TODO: wrap non-BaseDocuments somehow
177
Document JavaDoc doc2 = DocumentsRegistry.getLessActiveDocument(doc);
178         return doc2 instanceof BaseDocument ? (BaseDocument) doc2 : null;
179     }
180
181     public static synchronized BaseDocument getLessActiveDocument(int docID) {
182         // TODO: wrap non-BaseDocuments somehow
183
Document JavaDoc doc2 = DocumentsRegistry.getLessActiveDocument(docID);
184         return doc2 instanceof BaseDocument ? (BaseDocument) doc2 : null;
185     }
186
187     public static BaseDocument getMoreActiveDocument(BaseDocument doc) {
188         // TODO: wrap non-BaseDocuments somehow
189
Document JavaDoc doc2 = DocumentsRegistry.getMoreActiveDocument(doc);
190         return doc2 instanceof BaseDocument ? (BaseDocument) doc2 : null;
191     }
192
193     public static synchronized BaseDocument getMoreActiveDocument(int docID) {
194         // TODO: wrap non-BaseDocuments somehow
195
Document JavaDoc doc2 = DocumentsRegistry.getMoreActiveDocument(docID);
196         return doc2 instanceof BaseDocument ? (BaseDocument) doc2 : null;
197     }
198
199     /** Get the iterator over the active documents. It starts with
200      * the most active document till the least active document.
201      * It's just the current snapshot so the iterator will
202      * not reflect future changes.
203      */

204     public static synchronized Iterator JavaDoc getDocumentIterator() {
205         // TODO: wrap non-BaseDocuments somehow
206
return DocumentsRegistry.getDocumentIterator();
207     }
208
209     public static synchronized JTextComponent JavaDoc getMostActiveComponent() {
210         return DocumentsRegistry.getMostActiveComponent();
211     }
212
213     public static synchronized JTextComponent JavaDoc getLeastActiveComponent() {
214         return DocumentsRegistry.getLeastActiveComponent();
215     }
216
217     public static JTextComponent JavaDoc getLessActiveComponent(JTextComponent JavaDoc c) {
218         return DocumentsRegistry.getLessActiveComponent(c);
219     }
220
221     public static synchronized JTextComponent JavaDoc getLessActiveComponent(int compID) {
222         return DocumentsRegistry.getLessActiveComponent(compID);
223     }
224
225     public static JTextComponent JavaDoc getMoreActiveComponent(JTextComponent JavaDoc c) {
226         return DocumentsRegistry.getMoreActiveComponent(c);
227     }
228
229     public static synchronized JTextComponent JavaDoc getMoreActiveComponent(int compID) {
230         return DocumentsRegistry.getMoreActiveComponent(compID);
231     }
232
233     /** Get the iterator over the active components. It starts with
234     * the most active component till the least active component.
235     */

236     public static synchronized Iterator JavaDoc getComponentIterator() {
237         return DocumentsRegistry.getComponentIterator();
238     }
239
240     /** Debug the registry into string. */
241     public static synchronized String JavaDoc registryToString() {
242         return DocumentsRegistry.registryToString();
243     }
244 }
245
Popular Tags