KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > fold > FoldManagerFactoryProvider


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.modules.editor.fold;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.editor.fold.FoldHierarchy;
26 import org.openide.ErrorManager;
27
28 /**
29  * Provides list of fold factories that produce fold managers
30  * for the given fold hierarchy.
31  *
32  * <p>
33  * The default implementation <code>NbFoldManagerFactoryProvider</code>
34  * in fact first obtains a mime-type by using
35  * <code>hierarchySpi.getComponent().getEditorKit().getContentType()</code>
36  * and then inspects the contents of the following folder in the system FS:<pre>
37  * Editors/<mime-type>/FoldManager
38  * </pre>
39  *
40  * @author Miloslav Metelka
41  * @version 1.00
42  */

43
44 public abstract class FoldManagerFactoryProvider {
45     
46     private static FoldManagerFactoryProvider defaultProvider;
47     
48     private static FoldManagerFactoryProvider emptyProvider;
49     
50     private static boolean forceCustom;
51     
52     /**
53      * Get the default provider used to produce the managers.
54      * <br>
55      * This method gets called by <code>FoldHierarchyExecution</code>
56      * when rebuilding the managers.
57      */

58     public static synchronized FoldManagerFactoryProvider getDefault() {
59         if (defaultProvider == null) {
60             defaultProvider = findDefault();
61         }
62         
63         return defaultProvider;
64     }
65     
66     /**
67      * Return provider that provides empty list of factories.
68      * <br>
69      * This method may be used e.g. by <code>FoldHierarchyExecution</code>
70      * if the code folding is disabled in editor options.
71      */

72     public static FoldManagerFactoryProvider getEmpty() {
73         if (emptyProvider == null) {
74             // Multiple EmptyProvider can be created as method is not synced
75
// but should be no harm
76
emptyProvider = new EmptyProvider();
77         }
78         return emptyProvider;
79     }
80     
81     /**
82      * This method enforces the use of custom provider
83      * instead of the default layer-based provider.
84      * <br>
85      * It can be used e.g. for testing purposes.
86      *
87      * @param forceCustomProvider whether the instance
88      * of the {@link CustomProvider} should be used forcibly.
89      */

90     public static synchronized void setForceCustomProvider(boolean forceCustomProvider) {
91         if (!forceCustom) {
92             defaultProvider = null;
93         }
94         forceCustom = forceCustomProvider;
95     }
96     
97     private static FoldManagerFactoryProvider findDefault() {
98         FoldManagerFactoryProvider provider = null;
99
100         // By default use layer-based fold manager factory registrations.
101
// In case of standalone editor the custom provider
102
// will be used allowing custom fold manager factories registrations
103
// (public packages restrictions should not apply).
104
if (!forceCustom) {
105             try {
106                 org.openide.filesystems.Repository repository
107                     = org.openide.filesystems.Repository.getDefault();
108                 if (repository != null && repository.getDefaultFileSystem() != null) {
109                     provider = new LayerProvider();
110                 }
111             } catch (Throwable JavaDoc t) {
112                 // FileObject class not found -> use layer
113
}
114         }
115
116         if (provider == null) {
117             provider = new CustomProvider();
118         }
119         
120         return provider;
121     }
122     
123     /**
124      * Get fold managers appropriate for the given fold hierarchy.
125      *
126      * @param hierarchy fold hierarchy for which the fold managers
127      * are being created.
128      * @return list of <code>FoldManagerFactory</code>s to be used
129      * for the given hierarchy.
130      * <br>
131      * The order of the factories in the returned list defines
132      * priority of the folds produced by the corresponding manager
133      * (manager produced by the factory being first in the list
134      * produces the most important folds).
135      * <br>
136      * The list must not be modified by the clients.
137      */

138     public abstract List JavaDoc getFactoryList(FoldHierarchy hierarchy);
139
140     
141     /**
142      * Provider giving empty list of factories.
143      */

144     private static final class EmptyProvider extends FoldManagerFactoryProvider {
145         
146         public List JavaDoc getFactoryList(FoldHierarchy hierarchy) {
147             return java.util.Collections.EMPTY_LIST;
148         }
149         
150     }
151     
152 }
153
Popular Tags