KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.text.EditorKit JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.api.editor.fold.FoldHierarchy;
30 import org.netbeans.spi.editor.fold.FoldManagerFactory;
31
32 /**
33  * Fold manager factory provider that allows factories
34  * to be registered explicitly.
35  * <br>
36  * It can be used for standalone editor.
37  *
38  * @author Miloslav Metelka, Martin Roskanin
39  */

40 public class CustomProvider extends FoldManagerFactoryProvider {
41
42     private static final String JavaDoc FOLDER_NAME = "FoldManager"; //NOI18N
43

44     private final Map JavaDoc mime2factoryList = new HashMap JavaDoc();
45     
46     CustomProvider() {
47     }
48     
49     /**
50      * Register the fold manager factory so that it will be used for the code folding
51      * for the given mime-type.
52      *
53      * @param mimeType mime-type for which the factory is being registered.
54      * @param factory fold manager factory to be registered.
55      * <br>
56      * The factory added sooner will have higher priority regarding
57      * the fold appearance.
58      */

59     public void registerFactory(String JavaDoc mimeType, FoldManagerFactory factory) {
60         assert (mimeType != null) && (factory != null);
61
62         synchronized (mime2factoryList) {
63             List JavaDoc factoryList = (List JavaDoc)mime2factoryList.get(mimeType);
64             if (factoryList == null) {
65                 factoryList = new ArrayList JavaDoc();
66                 mime2factoryList.put(mimeType, factoryList);
67             }
68             factoryList.add(factory);
69         }
70     }
71     
72     /**
73      * Register multiple factories for the given mime-type
74      * in the order as they are present in the array.
75      */

76     public void registerFactories(String JavaDoc mimeType, FoldManagerFactory[] factories) {
77         synchronized (mime2factoryList) {
78             for (int i = 0; i < factories.length; i++) {
79                 registerFactory(mimeType, factories[i]);
80             }
81         }
82     }
83     
84     public void removeAllFactories(String JavaDoc mimeType) {
85         synchronized (mime2factoryList) {
86             mime2factoryList.put(mimeType, null);
87         }
88     }
89
90     public void removeAllFactories() {
91         synchronized (mime2factoryList) {
92             mime2factoryList.clear();
93         }
94     }
95
96     public List JavaDoc getFactoryList(FoldHierarchy hierarchy) {
97         List JavaDoc factoryList = null; // result
98
JTextComponent JavaDoc editorComponent = hierarchy.getComponent();
99         EditorKit JavaDoc kit = editorComponent.getUI().getEditorKit(editorComponent);
100         if (kit != null) {
101             String JavaDoc mimeType = kit.getContentType();
102             if (mimeType != null) {
103                 synchronized (mime2factoryList) {
104                     factoryList = (List JavaDoc)mime2factoryList.get(mimeType);
105                 }
106             }
107         }
108         
109         if (factoryList == null) {
110             return Collections.EMPTY_LIST;
111         }
112         return factoryList;
113     }
114     
115 }
116
Popular Tags