KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > utils > FileInfo


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.masterfs.filebasedfs.utils;
21
22 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.WriteLockUtils;
23 import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory;
24 import org.netbeans.modules.masterfs.filebasedfs.naming.UNCName;
25
26 import javax.swing.filechooser.FileSystemView JavaDoc;
27 import java.io.File JavaDoc;
28 import org.netbeans.modules.masterfs.filebasedfs.naming.FileNaming;
29 import org.openide.filesystems.FileObject;
30
31 public final class FileInfo {
32     private static final FileSystemView JavaDoc FILESYSTEMVIEW = FileSystemView.getFileSystemView();
33     private static boolean IS_WINDOWS = org.openide.util.Utilities.isWindows();
34
35     public static final int FLAG_isFile = 0;
36     public static final int FLAG_isDirectory = 1;
37     public static final int FLAG_exists = 2;
38     public static final int FLAG_isComputeNode = 3;
39     public static final int FLAG_isWindowsFloppy = 4;
40     public static final int FLAG_isUnixSpecialFile = 5;
41     public static final int FLAG_isUNC = 6;
42     public static final int FLAG_isFloppy = 7;
43     public static final int FLAG_isWindows = 8;
44     public static final int FLAG_isConvertibleToFileObject = 9;
45
46
47     private int isFile = -1;
48     private int isDirectory = -1;
49     private int exists = -1;
50     private int isComputeNode = -1;
51     private int isWindowsFloppy = -1;
52     private int isUnixSpecialFile = -1;
53     private int isUNC = -1;
54     private int isFloppy = -1;
55     private int isWindows = -1;
56     private int isConvertibleToFileObject = -1;
57
58     private Integer JavaDoc id = null;
59     private FileInfo root = null;
60     private final File JavaDoc file;
61     
62     private FileInfo parent = null;
63     private FileNaming fileNaming = null;
64     private FileObject fObject = null;
65     
66
67
68     public FileInfo(final File JavaDoc file) {
69         this.file = file;
70     }
71
72     public FileInfo(final FileInfo parent, final File JavaDoc file) {
73         this (file);
74         this.parent = parent;
75     }
76     
77     public boolean isFile() {
78         if (isFile == -1) {
79             isFile = (getFile().isFile()) ? 1 : 0;
80         }
81         return (isFile == 0) ? false : true;
82     }
83
84
85     public boolean isDirectory() {
86         if (isDirectory == -1) {
87             isDirectory = (getFile().isDirectory()) ? 1 : 0;
88         }
89         return (isDirectory == 0) ? false : true;
90     }
91
92
93     public boolean exists() {
94         if (exists == -1) {
95             exists = (getFile().exists()) ? 1 : 0;
96         }
97         return (exists == 0) ? false : true;
98     }
99
100     public boolean isComputeNode() {
101         if (isComputeNode == -1) {
102             isComputeNode = (FileInfo.FILESYSTEMVIEW.isComputerNode(getFile())) ? 1 : 0;
103         }
104
105         return (isComputeNode == 1) ? true : false;
106     }
107
108
109     public boolean isWindowsFloppy() {
110         if (isFloppy == -1) {
111             isFloppy = (FileInfo.FILESYSTEMVIEW.isFloppyDrive(getFile())) ? 1 : 0;
112         }
113         return (isFloppy == 1) ? true : false;
114     }
115
116
117     public boolean isUnixSpecialFile() {
118         if (isUnixSpecialFile == -1) {
119             isUnixSpecialFile = (!IS_WINDOWS && !isDirectory() && !isFile() && exists()) ? 1 : 0;
120         }
121         return (isUnixSpecialFile == 1) ? true : false;
122     }
123
124
125     public boolean isUNCFolder() {
126         if (isUNC == -1) {
127             isUNC = ((getFile() instanceof UNCName.UNCFile) || ((isWindows() && !isFile() && !isDirectory() && !exists() && isComputeNode()))) ? 1 : 0;
128         }
129         return (isUNC == 1) ? true : false;
130     }
131
132
133     public boolean isWindows() {
134         return FileInfo.IS_WINDOWS;
135     }
136     
137     public boolean isFloppy() {
138         if (isFloppy == -1) {
139             isFloppy = (FileInfo.FILESYSTEMVIEW.isFloppyDrive(getFile())) ? 1 : 0;
140         }
141
142         return (isFloppy == 1) ? true : false;
143     }
144
145
146
147
148     public boolean isConvertibleToFileObject() {
149         if (isConvertibleToFileObject == -1) {
150             isConvertibleToFileObject = (exists() && isSupportedFile()) ? 1 : 0;
151         }
152         
153         return (isConvertibleToFileObject == 1) ? true : false;
154     }
155
156     public boolean isSupportedFile() {
157         return (!getFile().getName().equals(".nbattrs") &&
158                 !WriteLockUtils.hasActiveLockFileSigns(getFile().getAbsolutePath()) &&
159                 (getFile().getParent() != null || !isWindowsFloppy())) ;
160     }
161
162
163     public FileInfo getRoot() {
164         if (root == null) {
165             File JavaDoc tmp = getFile();
166             File JavaDoc retVal = tmp;
167             while (tmp != null) {
168                 retVal = tmp;
169                 tmp = tmp.getParentFile();
170             }
171             
172             root = new FileInfo (retVal);
173         }
174         
175         return root;
176     }
177
178
179     public File JavaDoc getFile() {
180         return file;
181     }
182
183     public Integer JavaDoc getID() {
184         if (id == null) {
185             id = NamingFactory.createID(getFile());
186         }
187         return id;
188     }
189
190     public FileInfo getParent() {
191         return parent;
192     }
193     
194     public void setValueForFlag (int flag, boolean value) {
195         switch (flag) {
196             case FLAG_exists:
197                  exists = (value) ? 1 : 0;
198                 break;
199              case FLAG_isComputeNode:
200                  isComputeNode = (value) ? 1 : 0;
201                 break;
202              case FLAG_isConvertibleToFileObject:
203                  isConvertibleToFileObject = (value) ? 1 : 0;
204                 break;
205              case FLAG_isDirectory:
206                  isDirectory = (value) ? 1 : 0;
207                 break;
208              case FLAG_isFile:
209                  isFile = (value) ? 1 : 0;
210                 break;
211              case FLAG_isFloppy:
212                  isFloppy = (value) ? 1 : 0;
213                 break;
214              case FLAG_isUNC:
215                  isUNC = (value) ? 1 : 0;
216                 break;
217              case FLAG_isUnixSpecialFile:
218                  isUnixSpecialFile = (value) ? 1 : 0;
219                 break;
220              case FLAG_isWindows:
221                  isWindows = (value) ? 1 : 0;
222                 break;
223              case FLAG_isWindowsFloppy:
224                  isWindowsFloppy = (value) ? 1 : 0;
225                 break;
226         }
227     }
228
229     public FileNaming getFileNaming() {
230         return fileNaming;
231     }
232
233     public void setFileNaming(FileNaming fileNaming) {
234         this.fileNaming = fileNaming;
235     }
236
237     public FileObject getFObject() {
238         return fObject;
239     }
240
241     public void setFObject(FileObject fObject) {
242         this.fObject = fObject;
243     }
244
245     public String JavaDoc toString() {
246     return getFile().toString();
247     }
248
249     public static final String JavaDoc composeName(String JavaDoc name, String JavaDoc ext) {
250         return (ext != null && ext.length() > 0) ? (name + "." + ext) : name;//NOI18N
251
}
252
253     public static final String JavaDoc getName(String JavaDoc name) {
254         int i = name.lastIndexOf('.');
255         
256         /** period at first position is not considered as extension-separator */
257         return (i <= 0 || i == (name.length()-1)) ? name : name.substring(0, i);
258     }
259     
260     public static final String JavaDoc getExt(String JavaDoc name) {
261         int i = name.lastIndexOf('.') + 1;
262         
263         /** period at first position is not considered as extension-separator */
264         return ((i <= 1) || (i == name.length())) ? "" : name.substring(i); // NOI18N
265
}
266 }
Popular Tags