KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > graph > actions > SystemFileSystemSupport


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.web.jsf.navigation.graph.actions;
22
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.JSeparator JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.cookies.InstanceCookie;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.Repository;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.FolderInstance;
36
37
38 /**
39  * Provides support for retrieving instances from the system filesystem (SFS) folder.
40  * Currently it supports retrieving <code>Action</code> instances.
41  * <p>
42  * Note: It doesn't support the retrieving of instances from the subfolders.
43  * </p>
44  *
45  * @author Peter Zavadsky
46  */

47 public final class SystemFileSystemSupport {
48
49     /** Inteface defining the action provider. */
50     interface ActionsProvider {
51         Action JavaDoc[] getActions();
52     }
53
54     /** Dummy impl of <code>ActionsProvider</code> used for the cases
55      * the folder in the system filesystem doesn't exist. */

56     private static ActionsProvider DUMMY_ACTIONS_PROVIDER = new ActionsProvider() {
57         public Action JavaDoc[] getActions() {
58             return new Action JavaDoc[0];
59         }
60     };
61
62     /** Maps <code>DataFolder</code> to <code>ActionsProvider</code>. */
63     private static final Map JavaDoc dataFolder2actionsProvider = new WeakHashMap JavaDoc();
64
65
66     private SystemFileSystemSupport() {
67     }
68
69
70     /** Provides the actions retrieved from the specified folder in SFS.
71      * If the specified folder doesn't exist, an empty array is returned.
72      * The <code>null</code> values in the array represent separators.
73      * <p>
74      * Note: It doesn't retrieve the actions from the subfolders.
75      * </p>
76      * @param folderPath specifies the path to the folder in SFS
77      * @return Action[] */

78     public static Action JavaDoc[] getActions(String JavaDoc folderPath) {
79         return getActionProvider(folderPath).getActions();
80     }
81
82     /** Gets <code>ActionProvider</code> for specified folder in SFS.
83      * @param folderPath specifies the path to the folder in SFS */

84     private static ActionsProvider getActionProvider(String JavaDoc folderPath) {
85         DataFolder dataFolder = getDataFolder(folderPath);
86         if (dataFolder == null) {
87             return DUMMY_ACTIONS_PROVIDER;
88         }
89
90         synchronized (dataFolder2actionsProvider) {
91             ActionsProvider actionsProvider = (ActionsProvider)dataFolder2actionsProvider.get(dataFolder);
92             if (actionsProvider == null) {
93                 actionsProvider = new DefaultActionsProvider(dataFolder);
94                 dataFolder2actionsProvider.put(dataFolder, actionsProvider);
95             }
96             return actionsProvider;
97         }
98     }
99
100
101     private static DataFolder getDataFolder(String JavaDoc folderPath) {
102         FileObject fileObject = Repository.getDefault().getDefaultFileSystem().findResource(folderPath);
103         if (fileObject == null) {
104             return null;
105         }
106
107         return DataFolder.findFolder(fileObject);
108     }
109
110
111     private static class DefaultActionsProvider extends FolderInstance implements ActionsProvider {
112
113         public DefaultActionsProvider(DataFolder dataFolder) {
114             super(dataFolder);
115         }
116
117         /** Gets the action array. */
118         public Action JavaDoc[] getActions() {
119             try {
120                 return (Action JavaDoc[])instanceCreate();
121             } catch (IOException JavaDoc e) {
122                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
123             } catch (ClassNotFoundException JavaDoc e) {
124                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
125             }
126
127             return new Action JavaDoc[0];
128         }
129
130         /** Creates the actions. */
131         protected Object JavaDoc createInstance(InstanceCookie[] cookies)
132         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
133             List JavaDoc actions = new ArrayList JavaDoc();
134             for (int i = 0; i < cookies.length; i++) {
135                 Class JavaDoc clazz = cookies[i].instanceClass();
136                 if (JSeparator JavaDoc.class.isAssignableFrom(clazz)) {
137                     // XXX <code>null</code> is interpreted as a separator.
138
actions.add(null);
139                     continue;
140                 }
141
142                 Object JavaDoc object;
143                 try {
144                     object = cookies[i].instanceCreate();
145                 } catch (IOException JavaDoc e) {
146                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
147                     continue;
148                 } catch (ClassNotFoundException JavaDoc e) {
149                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
150                     continue;
151                 }
152                 
153                 if (object instanceof Action JavaDoc) {
154                     actions.add(object);
155                     continue;
156                 } else {
157                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
158                             new IllegalStateException JavaDoc("There is an unexpected object=" + object + // NOI18N
159
", in the folder instance=" + this)); // NOI18N
160
continue;
161                 }
162             }
163
164             return actions.toArray(new Action JavaDoc[0]);
165         }
166
167         /** Currently not recursive. */
168         protected InstanceCookie acceptFolder(DataFolder df) {
169             return null;
170         }
171     } // End of DefaultActionsProvider class.
172

173 }
174
175
Popular Tags