KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > io > VFSFile


1 /*
2  * VFSFile.java - A file residing on a virtual file system
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 2005 Slava Pestov
7  * Portions copyright (C) 2007 Matthieu Casanova
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */

23
24 package org.gjt.sp.jedit.io;
25
26 //{{{ Imports
27
import java.awt.Color JavaDoc;
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 //}}}
37

38 /**
39  * A directory entry returned from a file listing.
40  * @since jEdit 4.3pre2
41  */

42 public class VFSFile implements Serializable
43 {
44     //{{{ findCompletion() method
45
/**
46      * Return the index of a file whose name matches the given string,
47      * in a case-insensitive manner. Exact matches are preferred.
48      * @param files The list of files
49      * @param start The start index, inclusive
50      * @param start The end index, exclusive
51      * @param str The string to match
52      * @param dirsOnly Only match directories?
53      * @since jEdit 4.3pre3
54      */

55     public static int findCompletion(VFSFile[] files, int start, int end,
56         String JavaDoc str, boolean dirsOnly)
57     {
58         for(int i = start; i < end; i++)
59         {
60             VFSFile file = files[i];
61             String JavaDoc matchAgainst = (MiscUtilities.isAbsolutePath(str)
62                 ? file.getPath() : file.getName());
63
64             if(dirsOnly && file.getType() == VFSFile.FILE)
65                 continue;
66             /* try exact match first */
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     } //}}}
75

76     //{{{ findCompletion() method
77
public static String JavaDoc findCompletion(String JavaDoc path, String JavaDoc 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 JavaDoc 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     } //}}}
130

131     //{{{ getIcon() method
132
/**
133      * Returns the icon for the file.
134      *
135      * @since jEdit 4.3pre9
136      */

137     public final Icon getIcon(boolean expanded)
138     {
139         return getIcon(expanded, jEdit._getBuffer(getSymlinkPath()) != null);
140     } //}}}
141

142     //{{{ getIcon() method
143
/**
144      * Returns the icon for the file.
145      * Implementations of File system browsers can override this method
146      *
147      * @since jEdit 4.3pre9
148      */

149     public Icon getIcon(boolean expanded, boolean openBuffer)
150     {
151         return getDefaultIcon(expanded, openBuffer);
152     } //}}}
153

154     //{{{ getDefaultIcon() method
155
/**
156      * Returns the default icon for the file.
157      *
158      * @since jEdit 4.3pre9
159      */

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     } //}}}
171

172     //{{{ getDefaultIcon() method
173
/**
174      * @since jEdit 4.3pre9
175      */

176     public final Icon getDefaultIcon(boolean expanded)
177     {
178         return getDefaultIcon(expanded, jEdit._getBuffer(getSymlinkPath()) != null);
179     } //}}}
180

181     //{{{ File types
182
public static final int FILE = 0;
183     public static final int DIRECTORY = 1;
184     public static final int FILESYSTEM = 2;
185     //}}}
186

187     //{{{ Instance variables
188
/**
189      * @deprecated Use the accessor/mutator methods instead.
190      */

191     public String JavaDoc name;
192     /**
193      * @deprecated Use the accessor/mutator methods instead.
194      */

195     public String JavaDoc path;
196     /**
197      * @deprecated Use the accessor/mutator methods instead.
198      */

199     public String JavaDoc symlinkPath;
200     /**
201      * @deprecated Use the accessor/mutator methods instead.
202      */

203     public String JavaDoc deletePath;
204     /**
205      * @deprecated Use the accessor/mutator methods instead.
206      */

207     public int type;
208     /**
209      * @deprecated Use the accessor/mutator methods instead.
210      */

211     public long length;
212     /**
213      * @deprecated Use the accessor/mutator methods instead.
214      */

215     public boolean hidden;
216     /**
217      * @deprecated Use the accessor/mutator methods instead.
218      */

219     public boolean canRead;
220     /**
221      * @deprecated Use the accessor/mutator methods instead.
222      */

223     public boolean canWrite;
224     //}}}
225

226     //{{{ VFSFile constructor
227
/**
228      * @since jEdit 4.3pre2
229      */

230     public VFSFile()
231     {
232     } //}}}
233

234     //{{{ VFSFile constructor
235
public VFSFile(String JavaDoc name, String JavaDoc path, String JavaDoc 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             // maintain backwards compatibility
248
VFS vfs = VFSManager.getVFSForPath(path);
249             canRead = ((vfs.getCapabilities() & VFS.READ_CAP) != 0);
250             canWrite = ((vfs.getCapabilities() & VFS.WRITE_CAP) != 0);
251         }
252     } //}}}
253

254     //{{{ getVFS() method
255
/**
256      * @return The originating virtual file system of this file.
257      */

258     public VFS getVFS()
259     {
260         return VFSManager.getVFSForPath(path);
261     } //}}}
262

263     //{{{ getName() method
264
public String JavaDoc getName()
265     {
266         return name;
267     } //}}}
268

269     //{{{ setName() method
270
public void setName(String JavaDoc name)
271     {
272         this.name = name;
273     } //}}}
274

275     //{{{ isBinary() method
276
/**
277      * Check if a file is binary file.
278      * To check if a file is binary, we will check the first characters 100
279      * (jEdit property vfs.binaryCheck.length)
280      * If more than 1 (jEdit property vfs.binaryCheck.count), the
281      * file is declared binary.
282      * This is not 100% because sometimes the autodetection could fail.
283      *
284      * @param session the VFS session
285      * @return <code>true</code> if the file was detected as binary
286      * @throws IOException IOException If an I/O error occurs
287      * @since jEdit 4.3pre5
288      */

289     public boolean isBinary(Object JavaDoc 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     } //}}}
308

309     //{{{ getPath() method
310
public String JavaDoc getPath()
311     {
312         return path;
313     } //}}}
314

315     //{{{ setPath() method
316
public void setPath(String JavaDoc path)
317     {
318         this.path = path;
319     } //}}}
320

321     //{{{ getSymlinkPath() method
322
public String JavaDoc getSymlinkPath()
323     {
324         return symlinkPath;
325     } //}}}
326

327     //{{{ setSymlinkPath() method
328
public void setSymlinkPath(String JavaDoc symlinkPath)
329     {
330         this.symlinkPath = symlinkPath;
331     } //}}}
332

333     //{{{ getDeletePath() method
334
public String JavaDoc getDeletePath()
335     {
336         return deletePath;
337     } //}}}
338

339     //{{{ setDeletePath() method
340
public void setDeletePath(String JavaDoc deletePath)
341     {
342         this.deletePath = deletePath;
343     } //}}}
344

345     //{{{ getType() method
346
public int getType()
347     {
348         return type;
349     } //}}}
350

351     //{{{ setType() method
352
public void setType(int type)
353     {
354         this.type = type;
355     } //}}}
356

357     //{{{ getLength() method
358
public long getLength()
359     {
360         return length;
361     } //}}}
362

363     //{{{ setLength() method
364
public void setLength(long length)
365     {
366         this.length = length;
367     } //}}}
368

369     //{{{ isHidden() method
370
public boolean isHidden()
371     {
372         return hidden;
373     } //}}}
374

375     //{{{ setHidden() method
376
public void setHidden(boolean hidden)
377     {
378         this.hidden = hidden;
379     } //}}}
380

381     //{{{ isReadable() method
382
public boolean isReadable()
383     {
384         return canRead;
385     } //}}}
386

387     //{{{ setReadable() method
388
public void setReadable(boolean canRead)
389     {
390         this.canRead = canRead;
391     } //}}}
392

393     //{{{ isWriteable() method
394
public boolean isWriteable()
395     {
396         return canWrite;
397     } //}}}
398

399     //{{{ setWriteable() method
400
public void setWriteable(boolean canWrite)
401     {
402         this.canWrite = canWrite;
403     } //}}}
404

405     protected boolean colorCalculated;
406     protected Color JavaDoc color;
407
408     //{{{ getExtendedAttribute() method
409
/**
410      * Returns the value of an extended attribute. Note that this
411      * returns formatted strings (eg, "10 Mb" for a file size of
412      * 1048576 bytes). If you need access to the raw data, access
413      * fields and methods of this class.
414      * @param name The extended attribute name
415      * @since jEdit 4.2pre1
416      */

417     public String JavaDoc getExtendedAttribute(String JavaDoc 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 JavaDoc();
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     } //}}}
460

461     //{{{ getColor() method
462
public Color JavaDoc getColor()
463     {
464         if(!colorCalculated)
465         {
466             colorCalculated = true;
467             color = VFS.getDefaultColorFor(name);
468         }
469
470         return color;
471     } //}}}
472

473     //{{{ toString() method
474
public String JavaDoc toString()
475     {
476         return name;
477     } //}}}
478

479     //{{{ fetchedAttrs() method
480
protected boolean fetchedAttrs()
481     {
482         return fetchedAttrs;
483     } //}}}
484

485     //{{{ fetchAttrs() method
486
protected void fetchAttrs()
487     {
488         fetchedAttrs = true;
489     } //}}}
490

491     private boolean fetchedAttrs;
492 }
493
Popular Tags