1 23 24 package org.gjt.sp.jedit.io; 25 26 import java.awt.Color ; 28 import java.io.*; 29 import org.gjt.sp.jedit.*; 30 import org.gjt.sp.jedit.browser.VFSBrowser; 31 import org.gjt.sp.jedit.browser.FileCellRenderer; 32 import org.gjt.sp.util.Log; 33 import org.gjt.sp.util.IOUtilities; 34 35 import javax.swing.*; 36 38 42 public class VFSFile implements Serializable 43 { 44 55 public static int findCompletion(VFSFile[] files, int start, int end, 56 String str, boolean dirsOnly) 57 { 58 for(int i = start; i < end; i++) 59 { 60 VFSFile file = files[i]; 61 String matchAgainst = (MiscUtilities.isAbsolutePath(str) 62 ? file.getPath() : file.getName()); 63 64 if(dirsOnly && file.getType() == VFSFile.FILE) 65 continue; 66 67 else if(matchAgainst.equals(str)) 68 return i; 69 else if(matchAgainst.regionMatches(true,0,str,0,str.length())) 70 return i; 71 } 72 73 return -1; 74 } 76 public static String findCompletion(String path, String complete, 78 VFSBrowser browser, boolean dirsOnly) 79 { 80 Log.log(Log.DEBUG,VFSFile.class,"findCompletion(" + path + ',' + complete 81 + ',' + dirsOnly + ')'); 82 83 if(complete.equals("~")) 84 return System.getProperty("user.home"); 85 else if(complete.equals("-")) 86 return browser.getView().getBuffer().getDirectory(); 87 else if(complete.equals("..")) 88 return MiscUtilities.getParentOfPath(path); 89 90 if(MiscUtilities.isAbsolutePath(complete)) 91 { 92 if(MiscUtilities.isURL(complete)) 93 return complete; 94 else 95 path = "roots:"; 96 } 97 98 VFS vfs = VFSManager.getVFSForPath(path); 99 if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) == 0) 100 return null; 101 Object session = vfs.createVFSSession(path,browser); 102 if(session == null) 103 return null; 104 105 try 106 { 107 VFSFile[] files = vfs._listFiles(session,path,browser); 108 int index = findCompletion(files,0,files.length,complete,dirsOnly); 109 if(index != -1) 110 return files[index].path; 111 } 112 catch(IOException e) 113 { 114 VFSManager.error(e,path,browser); 115 } 116 finally 117 { 118 try 119 { 120 vfs._endVFSSession(session,browser); 121 } 122 catch(IOException e) 123 { 124 VFSManager.error(e,path,browser); 125 } 126 } 127 128 return null; 129 } 131 137 public final Icon getIcon(boolean expanded) 138 { 139 return getIcon(expanded, jEdit._getBuffer(getSymlinkPath()) != null); 140 } 142 149 public Icon getIcon(boolean expanded, boolean openBuffer) 150 { 151 return getDefaultIcon(expanded, openBuffer); 152 } 154 160 public final Icon getDefaultIcon(boolean expanded, boolean openBuffer) 161 { 162 if(getType() == DIRECTORY) 163 return expanded ? FileCellRenderer.openDirIcon : FileCellRenderer.dirIcon; 164 else if(getType() == FILESYSTEM) 165 return FileCellRenderer.filesystemIcon; 166 else if(openBuffer) 167 return FileCellRenderer.openFileIcon; 168 else 169 return FileCellRenderer.fileIcon; 170 } 172 176 public final Icon getDefaultIcon(boolean expanded) 177 { 178 return getDefaultIcon(expanded, jEdit._getBuffer(getSymlinkPath()) != null); 179 } 181 public static final int FILE = 0; 183 public static final int DIRECTORY = 1; 184 public static final int FILESYSTEM = 2; 185 187 191 public String name; 192 195 public String path; 196 199 public String symlinkPath; 200 203 public String deletePath; 204 207 public int type; 208 211 public long length; 212 215 public boolean hidden; 216 219 public boolean canRead; 220 223 public boolean canWrite; 224 226 230 public VFSFile() 231 { 232 } 234 public VFSFile(String name, String path, String deletePath, 236 int type, long length, boolean hidden) 237 { 238 this.name = name; 239 this.path = path; 240 this.deletePath = deletePath; 241 this.symlinkPath = path; 242 this.type = type; 243 this.length = length; 244 this.hidden = hidden; 245 if(path != null) 246 { 247 VFS vfs = VFSManager.getVFSForPath(path); 249 canRead = ((vfs.getCapabilities() & VFS.READ_CAP) != 0); 250 canWrite = ((vfs.getCapabilities() & VFS.WRITE_CAP) != 0); 251 } 252 } 254 258 public VFS getVFS() 259 { 260 return VFSManager.getVFSForPath(path); 261 } 263 public String getName() 265 { 266 return name; 267 } 269 public void setName(String name) 271 { 272 this.name = name; 273 } 275 289 public boolean isBinary(Object session) 290 throws IOException 291 { 292 Reader reader = null; 293 InputStream in = getVFS()._createInputStream(session,getPath(), 294 false,jEdit.getActiveView()); 295 if(in == null) 296 throw new IOException("Unable to get a Stream for this file "+this); 297 298 try 299 { 300 reader = MiscUtilities.autodetect(in, null); 301 return MiscUtilities.isBinary(reader); 302 } 303 finally 304 { 305 IOUtilities.closeQuietly(reader); 306 } 307 } 309 public String getPath() 311 { 312 return path; 313 } 315 public void setPath(String path) 317 { 318 this.path = path; 319 } 321 public String getSymlinkPath() 323 { 324 return symlinkPath; 325 } 327 public void setSymlinkPath(String symlinkPath) 329 { 330 this.symlinkPath = symlinkPath; 331 } 333 public String getDeletePath() 335 { 336 return deletePath; 337 } 339 public void setDeletePath(String deletePath) 341 { 342 this.deletePath = deletePath; 343 } 345 public int getType() 347 { 348 return type; 349 } 351 public void setType(int type) 353 { 354 this.type = type; 355 } 357 public long getLength() 359 { 360 return length; 361 } 363 public void setLength(long length) 365 { 366 this.length = length; 367 } 369 public boolean isHidden() 371 { 372 return hidden; 373 } 375 public void setHidden(boolean hidden) 377 { 378 this.hidden = hidden; 379 } 381 public boolean isReadable() 383 { 384 return canRead; 385 } 387 public void setReadable(boolean canRead) 389 { 390 this.canRead = canRead; 391 } 393 public boolean isWriteable() 395 { 396 return canWrite; 397 } 399 public void setWriteable(boolean canWrite) 401 { 402 this.canWrite = canWrite; 403 } 405 protected boolean colorCalculated; 406 protected Color color; 407 408 417 public String getExtendedAttribute(String name) 418 { 419 if(name.equals(VFS.EA_TYPE)) 420 { 421 switch(getType()) 422 { 423 case FILE: 424 return jEdit.getProperty("vfs.browser.type.file"); 425 case DIRECTORY: 426 return jEdit.getProperty("vfs.browser.type.directory"); 427 case FILESYSTEM: 428 return jEdit.getProperty("vfs.browser.type.filesystem"); 429 default: 430 throw new IllegalArgumentException (); 431 } 432 } 433 else if(name.equals(VFS.EA_STATUS)) 434 { 435 if(isReadable()) 436 { 437 if(isWriteable()) 438 return jEdit.getProperty("vfs.browser.status.rw"); 439 else 440 return jEdit.getProperty("vfs.browser.status.ro"); 441 } 442 else 443 { 444 if(isWriteable()) 445 return jEdit.getProperty("vfs.browser.status.append"); 446 else 447 return jEdit.getProperty("vfs.browser.status.no"); 448 } 449 } 450 else if(name.equals(VFS.EA_SIZE)) 451 { 452 if(getType() != FILE) 453 return null; 454 else 455 return MiscUtilities.formatFileSize(getLength()); 456 } 457 else 458 return null; 459 } 461 public Color getColor() 463 { 464 if(!colorCalculated) 465 { 466 colorCalculated = true; 467 color = VFS.getDefaultColorFor(name); 468 } 469 470 return color; 471 } 473 public String toString() 475 { 476 return name; 477 } 479 protected boolean fetchedAttrs() 481 { 482 return fetchedAttrs; 483 } 485 protected void fetchAttrs() 487 { 488 fetchedAttrs = true; 489 } 491 private boolean fetchedAttrs; 492 } 493 | Popular Tags |