KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.WeakHashMap 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.api.editor.mimelookup.MimeLookup;
31 import org.netbeans.spi.editor.fold.FoldManager;
32 import org.netbeans.spi.editor.fold.FoldManagerFactory;
33 import org.openide.ErrorManager;
34 import org.openide.cookies.InstanceCookie;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileSystem;
37 import org.openide.filesystems.Repository;
38 import org.openide.loaders.DataFolder;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.util.Lookup;
42
43 /**
44  * Fold manager factory provider that obtains the factories
45  * by reading the xml layer.
46  * <br>
47  * The registration are read from the following folder in the system FS:
48  * <pre>
49  * Editors/&lt;mime-type&gt;/FoldManager
50  * </pre>
51  * For example java fold manager factories should be registered in
52  * <pre>
53  * Editors/text/x-java/FoldManager
54  * </pre>
55  *
56
57  *
58  * @author Miloslav Metelka, Martin Roskanin
59  */

60 class LayerProvider extends FoldManagerFactoryProvider {
61
62     private static final String JavaDoc FOLDER_NAME = "FoldManager"; //NOI18N
63

64     private Map JavaDoc kit2factoryList;
65     
66     LayerProvider() {
67         kit2factoryList = new WeakHashMap JavaDoc();
68     }
69
70     public List JavaDoc getFactoryList(FoldHierarchy hierarchy) {
71         List JavaDoc factoryList = null; // result
72
JTextComponent JavaDoc editorComponent = hierarchy.getComponent();
73         EditorKit JavaDoc kit = editorComponent.getUI().getEditorKit(editorComponent);
74         if (kit != null) {
75             factoryList = (List JavaDoc)kit2factoryList.get(kit);
76             if (factoryList == null) { // not cached yet
77
String JavaDoc mimeType = kit.getContentType();
78                 if (mimeType != null) {
79                     MimeLookup mimeLookup = MimeLookup.getMimeLookup(mimeType);
80                     factoryList = new ArrayList JavaDoc();
81                     factoryList.addAll(mimeLookup.lookup(new Lookup.Template(FoldManagerFactory.class)).allInstances());
82                 }
83             } // not yet cached
84
}
85         
86         if (factoryList == null) {
87             return Collections.EMPTY_LIST;
88         }
89         return factoryList;
90     }
91     
92 }
93
Popular Tags