KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > DataSystem


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.core;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedHashSet JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import javax.swing.Action JavaDoc;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileStateInvalidException;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.filesystems.RepositoryEvent;
41 import org.openide.filesystems.RepositoryListener;
42 import org.openide.filesystems.RepositoryReorderedEvent;
43 import org.openide.filesystems.URLMapper;
44 import org.openide.loaders.DataFilter;
45 import org.openide.loaders.DataFolder;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.loaders.InstanceSupport;
49 import org.openide.loaders.RepositoryNodeFactory;
50 import org.openide.nodes.AbstractNode;
51 import org.openide.nodes.Children;
52 import org.openide.nodes.FilterNode;
53 import org.openide.nodes.Node;
54 import org.openide.util.HelpCtx;
55 import org.openide.util.Lookup;
56 import org.openide.util.NbBundle;
57 import org.openide.util.WeakListeners;
58 import org.openide.util.actions.SystemAction;
59
60 /** Data system encapsulates logical structure of more file systems.
61 * It also allows filtering of content of DataFolders
62 *
63 * @author Jaroslav Tulach, Petr Hamernik
64 */

65 public final class DataSystem extends AbstractNode
66 implements RepositoryListener {
67     /** default instance */
68     private static DataSystem def;
69
70     /** the file system pool to work with */
71     private transient Repository fileSystemPool;
72
73     /** filter for the data system */
74     DataFilter filter;
75
76     /** Constructor.
77     * @param fsp file system pool
78     * @param filter the filter for filtering files
79     */

80     private DataSystem(Children ch, Repository fsp, DataFilter filter) {
81         super (ch);
82         fileSystemPool = fsp;
83         this.filter = filter;
84         initialize();
85         setIconBaseWithExtension ("org/netbeans/core/resources/repository.gif"); // NOI18N
86
setName (NbBundle.getBundle (DataSystem.class).getString ("dataSystemName"));
87         setShortDescription (NbBundle.getBundle (DataSystem.class).getString ("CTL_Repository_Hint"));
88         getCookieSet ().add (new InstanceSupport.Instance (fsp));
89     }
90
91     /** Constructor. Uses default file system pool.
92     * @param filter the filter to use
93     */

94     private DataSystem(Children ch, DataFilter filter) {
95         this (ch, Repository.getDefault(), filter);
96     }
97
98     public HelpCtx getHelpCtx () {
99         return new HelpCtx (DataSystem.class);
100     }
101
102     /** Factory for DataSystem instances */
103     public static Node getDataSystem(DataFilter filter) {
104         if (filter == null) {
105             if (def != null) {
106                 return def;
107             }
108             return def = new DataSystem(new DSMap (), DataFilter.ALL);
109         } else {
110             return new DataSystem(new DSMap (), filter);
111         }
112     }
113
114     /** Gets a DataSystem */
115     public static Node getDataSystem() {
116         return getDataSystem(null);
117     }
118
119     void initialize () {
120         fileSystemPool.addRepositoryListener(WeakListeners.create(RepositoryListener.class,
121                                                                   this,
122                                                                   fileSystemPool));
123         Enumeration JavaDoc en = fileSystemPool.getFileSystems ();
124         while (en.hasMoreElements ()) {
125             FileSystem fs = (FileSystem)en.nextElement ();
126             fs.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange ((DSMap)getChildren (), fs));
127         }
128         refresh ();
129     }
130
131     /** writes this node to ObjectOutputStream and its display name
132     */

133     public Node.Handle getHandle() {
134         return filter == DataFilter.ALL ? new DSHandle (null) : new DSHandle(filter);
135     }
136
137
138     public Action JavaDoc[] getActions(boolean context) {
139         return new Action JavaDoc[] {
140                    SystemAction.get (org.openide.actions.FindAction.class),
141                    //Problem with ToolsAction as last item and separator. When ToolsAction
142
//is empty separator is displayed as last item.
143
//null,
144
SystemAction.get (org.openide.actions.ToolsAction.class),
145                    //SystemAction.get (org.openide.actions.PropertiesAction.class), // #12072
146
//SystemAction.get (org.openide.actions.CustomizeAction.class),
147
};
148     }
149
150     /** Called when new file system is added to the pool.
151     * @param ev event describing the action
152     */

153     public void fileSystemAdded (RepositoryEvent ev) {
154         ev.getFileSystem ().addPropertyChangeListener (
155             org.openide.util.WeakListeners.propertyChange ((DSMap)getChildren (), ev.getFileSystem ())
156         );
157         refresh ();
158     }
159
160     /** Called when a file system is deleted from the pool.
161     * @param ev event describing the action
162     */

163     public void fileSystemRemoved (RepositoryEvent ev) {
164         refresh ();
165     }
166     /** Called when the fsp is reordered */
167     public void fileSystemPoolReordered(RepositoryReorderedEvent ev) {
168         refresh ();
169     }
170
171     /** Refreshes the pool.
172     */

173     void refresh () {
174         refresh (null);
175     }
176
177     /** Refreshes the pool.
178     * @param fs file system to remove
179     */

180     void refresh (FileSystem fs) {
181         // XXX hack to show only masterfs and no other filesystems
182
// should later be solved better
183
// XXX should check if fs.root.url.protocol is not 'file' or 'jar', and if so, show it also
184
// (to display network mounts)
185
URLMapper mapper = getMasterFsURLMapper();
186         if (mapper == null) {
187             //original solution based on Repository
188
((DSMap)getChildren ()).refresh (fileSystemPool, fs);
189         } else {
190             ((DSMap)getChildren ()).refreshListRoots(mapper);
191         }
192     }
193
194     private static URLMapper getMasterFsURLMapper() {
195         URLMapper retVal = null;
196         Lookup.Result result = Lookup.getDefault().lookupResult(URLMapper.class);
197         Collection JavaDoc c = result.allInstances();
198         for (Iterator JavaDoc iterator = c.iterator(); iterator.hasNext();) {
199             URLMapper mapper = (URLMapper) iterator.next();
200             if (mapper != null && "org.netbeans.modules.masterfs.MasterURLMapper".equals(mapper.getClass().getName())) {//NOI18N
201
retVal = mapper;
202                 break;
203             }
204         }
205         return retVal;
206     }
207     
208     /** We have customizer */
209     public boolean hasCustomizer() {
210         return true;
211     }
212
213     /** Children that listens to changes in filesystem pool.
214     */

215     static class DSMap extends Children.Keys implements PropertyChangeListener JavaDoc {
216
217         public void propertyChange (PropertyChangeEvent JavaDoc ev) {
218             //System.out.println ("Property change"); // NOI18N
219
DataSystem ds = getDS ();
220             if (ds == null) return;
221
222             if ("root".equals(ev.getPropertyName())) {
223                 FileSystem fs = (FileSystem)ev.getSource ();
224                 ds.refresh (fs);
225                 ds.refresh ();
226             }
227         }
228
229         /** The node */
230         private DataSystem getDS() {
231             return (DataSystem)getNode ();
232         }
233
234         protected Node[] createNodes (Object JavaDoc key) {
235             DataFolder df = (DataFolder)key;
236             Node n = new FilterNode(df.getNodeDelegate(), df.createNodeChildren (getDS ().filter));
237             return new Node[] {n};
238         }
239
240         /** Refreshes the pool.
241         * @param fileSystemPool the pool
242         * @param fs file system to remove
243         */

244         public void refresh(Repository fileSystemPool, FileSystem fs) {
245             @SuppressWarnings JavaDoc("unchecked") Enumeration JavaDoc<FileSystem> en = (Enumeration JavaDoc<FileSystem>)fileSystemPool.getFileSystems();
246             ArrayList JavaDoc<DataFolder> list = new ArrayList JavaDoc<DataFolder>();
247             while (en.hasMoreElements()) {
248                 FileSystem fsystem = en.nextElement();
249                 DataObject root = null;
250                 try {
251                     root = DataObject.find(fsystem.getRoot());
252                 }
253                 catch (DataObjectNotFoundException e) {
254                     Logger.getLogger(DataSystem.class.getName()).log(Level.WARNING, null, e);
255                     // root will remain null and will be accepted
256
// (as that seems safer than not accepting it)
257
}
258                 if ((root instanceof DataFolder) && getDS().filter.acceptDataObject(root)) {
259                     list.add((DataFolder)root);
260                 }
261             }
262             setKeys(list);
263         }
264         
265         private void refreshListRoots(URLMapper mapper) {
266             File JavaDoc[] files = File.listRoots();
267             Set JavaDoc<DataFolder> rootSet = new LinkedHashSet JavaDoc<DataFolder>();
268
269             for (int i = 0; i < files.length; i++) {
270                 File JavaDoc file = files[i];
271                 FileObject fo = fetchFileObject(file, mapper);
272
273                 if (fo != null) {
274                     try {
275                         fo = fo.getFileSystem().getRoot();
276                     }
277                     catch (FileStateInvalidException e) {
278                         continue;
279                     }
280                     DataObject root = null;
281
282                     try {
283                         root = DataObject.find(fo);
284                     }
285                     catch (DataObjectNotFoundException e) {
286                         Logger.getLogger(DataSystem.class.getName()).log(Level.WARNING, null, e);
287                     }
288                     if ((root instanceof DataFolder) &&
289                         getDS().filter.acceptDataObject(root)) {
290                         rootSet.add((DataFolder) root);
291                     }
292                 }
293             }
294             setKeys(rootSet);
295         }
296
297
298         private FileObject fetchFileObject(File JavaDoc file, URLMapper mapper) {
299             /*intentiionally isn't used FileUtil.toFileObject because here can't be
300             called method normalizeFile which causes problems with removeable drives
301             on Windows*/

302             FileObject retVal = null;
303             try {
304                 FileObject[] all = mapper.getFileObjects(toUrl(file));//NOI18N
305
if (all != null && all.length > 0) {
306                     retVal = all [0];
307                 }
308             } catch (MalformedURLException JavaDoc e) {
309                 retVal = null;
310             }
311             return retVal;
312         }
313
314         private URL JavaDoc toUrl(File JavaDoc file) throws MalformedURLException JavaDoc {
315             return (org.openide.util.Utilities.isWindows()) ? new URL JavaDoc ("file:/"+file.getAbsolutePath ()) : file.toURI().toURL();//NOI18N
316
}
317
318     }
319     
320     
321
322     /** Serialization. */
323     private static class DSHandle implements Node.Handle {
324         DataFilter filter;
325
326         static final long serialVersionUID =-2266375092419944364L;
327         public DSHandle(DataFilter f) {
328             filter = f;
329         }
330
331         public Node getNode() {
332             return getDataSystem (filter);
333         }
334     }
335     
336     /** @deprecated No longer useful in the UI. */
337     public static final class NbRepositoryNodeFactory extends RepositoryNodeFactory {
338         
339         public Node repository(DataFilter f) {
340             return DataSystem.getDataSystem(f == DataFilter.ALL ? null : f);
341         }
342         
343     }
344     
345 }
346
Popular Tags