KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > navigator > ProviderRegistry


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.navigator;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import org.netbeans.spi.navigator.NavigatorPanel;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.Repository;
37 import org.openide.loaders.DataFolder;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.FolderLookup;
40 import org.openide.util.Lookup;
41
42 /**
43  * Storage/lookup of NavigatorPanel providers. Providers are mapped to
44  * mime types they support.
45  *
46  * @author Dafe Simonek
47  */

48 class ProviderRegistry {
49     
50     /** folder in layer file system where navigator panels are searched for */
51     private static final String JavaDoc PANELS_FOLDER = "/Navigator/Panels/"; //NOI18N
52
/** template for finding all NavigatorPanel instances in lookup */
53     private static final Lookup.Template<NavigatorPanel> NAV_PANEL_TEMPLATE =
54             new Lookup.Template<NavigatorPanel>(NavigatorPanel.class);
55     
56     /** singleton instance */
57     private static ProviderRegistry instance;
58     
59     /** Mapping between mime types and provider instances. Note that
60      * Collections.EMPTY_LIST serves as special value telling us that
61      * we already searched for providers for specific content type and found
62      * no providers. This ensures no useless repetitive searches.
63      */

64     private Map JavaDoc<String JavaDoc, Collection JavaDoc<? extends NavigatorPanel>> contentTypes2Providers;
65
66
67     /** Singleton, no external instantiation */
68     private ProviderRegistry () {
69     }
70
71     /********* public area *********/
72     
73     public static ProviderRegistry getInstance () {
74         if (instance == null ) {
75             instance = new ProviderRegistry();
76         }
77         return instance;
78     }
79     
80     /** Finds appropriate providers for given data content type
81      * (similar to mime type)
82      * and returns list of provider classes.
83      *
84      * @return Collection of providers, which implements NavigatorPanel interface.
85      * Never return null, only empty List if no provider exists for given content type.
86      */

87     public Collection JavaDoc<? extends NavigatorPanel> getProviders (String JavaDoc contentType) {
88         if (contentTypes2Providers == null) {
89             contentTypes2Providers = new HashMap JavaDoc<String JavaDoc, Collection JavaDoc<? extends NavigatorPanel>>(15);
90         }
91         Collection JavaDoc<? extends NavigatorPanel> result = contentTypes2Providers.get(contentType);
92         if (result == null) {
93             // load and instantiate provider classes
94
result = loadProviders(contentType);
95             contentTypes2Providers.put(contentType, result);
96         }
97             
98         return result;
99     }
100     
101     /******* private stuff ***********/
102
103     
104     /** Returns collection of NavigatorPanels or empty collection if no provider
105      * exist for given content type
106      */

107     private Collection JavaDoc<? extends NavigatorPanel> loadProviders (String JavaDoc contentType) {
108         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
109         FileObject fo = fs.findResource(PANELS_FOLDER + contentType);
110
111         if (fo == null) {
112             // no available providers or malformed contentType
113
Logger.getAnonymousLogger().fine("No providers for content type " + contentType); //NOI18N
114
return Collections.emptyList();
115         }
116         
117         DataFolder.Container dContainer;
118         try {
119             dContainer = DataFolder.findContainer(fo);
120         } catch (IllegalArgumentException JavaDoc exc) {
121             ErrorManager.getDefault().annotate(exc,
122                 "Navigator content type " + contentType +
123                 " is probably malformed, as it doesn't point to folder.");
124             ErrorManager.getDefault().notify(ErrorManager.WARNING, exc);
125             return Collections.emptyList();
126         }
127         
128         FolderLookup fLookup = new FolderLookup(dContainer, "");
129         Lookup.Result<NavigatorPanel> result = fLookup.getLookup().lookup(NAV_PANEL_TEMPLATE);
130
131         return result.allInstances();
132     }
133
134 }
135
Popular Tags