1 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 ; 27 import java.io.File ; 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 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 id = null; 59 private FileInfo root = null; 60 private final File 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 file) { 69 this.file = file; 70 } 71 72 public FileInfo(final FileInfo parent, final File 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 tmp = getFile(); 166 File 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 getFile() { 180 return file; 181 } 182 183 public Integer 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 toString() { 246 return getFile().toString(); 247 } 248 249 public static final String composeName(String name, String ext) { 250 return (ext != null && ext.length() > 0) ? (name + "." + ext) : name; } 252 253 public static final String getName(String name) { 254 int i = name.lastIndexOf('.'); 255 256 257 return (i <= 0 || i == (name.length()-1)) ? name : name.substring(0, i); 258 } 259 260 public static final String getExt(String name) { 261 int i = name.lastIndexOf('.') + 1; 262 263 264 return ((i <= 1) || (i == name.length())) ? "" : name.substring(i); } 266 } | Popular Tags |