KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > ExLocalFileSystem


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
21 package org.netbeans.modules.masterfs;
22
23 import org.openide.filesystems.LocalFileSystem;
24 import org.openide.filesystems.AbstractFileSystem;
25 import org.openide.filesystems.DefaultAttributes;
26 import org.netbeans.modules.masterfs.providers.Attributes;
27
28 import java.io.IOException JavaDoc;
29 import java.io.File JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.beans.PropertyVetoException JavaDoc;
32
33 public class ExLocalFileSystem extends LocalFileSystem {
34     public static ExLocalFileSystem getInstance (File JavaDoc root) throws PropertyVetoException JavaDoc, IOException JavaDoc {
35         ExLocalFileSystem retVal = new ExLocalFileSystem ();
36         if (root.equals(Attributes.getRootForAttributes())) {
37             retVal.attr = new OneFileAttributeAttachedToRoot(retVal.info, retVal.change, retVal.list);
38         } else {
39             retVal.attr = new Attributes(root, retVal.info, retVal.change, retVal.list);
40         }
41         retVal.setRootDirectory(root);
42         
43         return retVal;
44     }
45     
46     public DefaultAttributes getAttributes () {
47         return (DefaultAttributes)attr;
48     }
49     
50     private static class OneFileAttributeAttachedToRoot extends DefaultAttributes {
51
52         public OneFileAttributeAttachedToRoot(
53                 AbstractFileSystem.Info info,
54                 AbstractFileSystem.Change change,
55                 AbstractFileSystem.List list
56                 ) {
57             
58             super(info, change, list, Attributes.ATTRNAME); //NOI18N
59
}
60         
61         
62         public String JavaDoc[] children(String JavaDoc f) {
63             return super.children(f);
64         }
65
66         /* Get the file attribute with the specified name.
67         * @param name the file
68         * @param attrName name of the attribute
69         * @return appropriate (serializable) value or <CODE>null</CODE> if the attribute is unset (or could not be properly restored for some reason)
70         */

71         public Object JavaDoc readAttribute(String JavaDoc name, String JavaDoc attrName) {
72             return super.readAttribute(transformName (name), attrName);
73         }
74
75         /* Set the file attribute with the specified name.
76         * @param name the file
77         * @param attrName name of the attribute
78         * @param value new value or <code>null</code> to clear the attribute. Must be serializable, although particular filesystems may or may not use serialization to store attribute values.
79         * @exception IOException if the attribute cannot be set. If serialization is used to store it, this may in fact be a subclass such as {@link NotSerializableException}.
80         */

81         public void writeAttribute(String JavaDoc name, String JavaDoc attrName, Object JavaDoc value)
82                 throws IOException JavaDoc {
83             super.writeAttribute(transformName (name), attrName, value);
84         }
85
86         /* Get all file attribute names for the file.
87         * @param name the file
88         * @return enumeration of keys (as strings)
89         */

90         public synchronized Enumeration JavaDoc attributes(String JavaDoc name) {
91             return super.attributes(transformName (name));
92         }
93
94         /* Called when a file is renamed, to appropriatelly update its attributes.
95         * <p>
96         * @param oldName old name of the file
97         * @param newName new name of the file
98         */

99         public synchronized void renameAttributes(String JavaDoc oldName, String JavaDoc newName) {
100             super.renameAttributes(transformName (oldName), transformName (newName));
101         }
102
103         /* Called when a file is deleted to also delete its attributes.
104         *
105         * @param name name of the file
106         */

107         public synchronized void deleteAttributes(String JavaDoc name) {
108             super.deleteAttributes(transformName (name));
109         }
110         
111         private String JavaDoc transformName (String JavaDoc name) {
112             char replaceChar = '|';//NOI18N
113
if (name.indexOf(replaceChar) != -1 ) {
114                 StringBuffer JavaDoc transformed = new StringBuffer JavaDoc(name.length() + 50);
115                 for (int i = 0; i < name.length(); i++) {
116                     transformed.append(name.charAt(i));
117                     if (name.charAt(i) == replaceChar)
118                         transformed.append(replaceChar);
119                 }
120                 name = transformed.toString();
121             }
122             return name.replace('/',replaceChar);//NOI18N
123
}
124     }
125 }
126
Popular Tags