23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public void setFileView(fileView)<br />

public FileView getFileView()<br />

<strong>Java</strong> I/O<br />

The current file view determines how information about files is interpreted and displayed to<br />

the user. For instance, you can use a file view to display names but not extensions, icons for<br />

files, last-modified dates of files, file sizes, and more. In general, the more information you<br />

choose to display in the file chooser, the slower your choosers are to display and the longer it<br />

takes to switch directories.<br />

The JFileChooser class has five methods that return information about a file. These methods<br />

are used to display the list of files from which the user can select:<br />

public String getName(File f)<br />

public String getDescription(File f)<br />

public String getTypeDescription(File f)<br />

public Icon getIcon(File f)<br />

public boolean isTraversable(File f)<br />

Each of these methods delegates its work to the file chooser's internal FileView object. Most<br />

of the time the default file view is enough. However, you can write your own subclass of<br />

FileView that implements all five of these methods, then install it in the file chooser with<br />

setFileView(). The getName() method should return the name of the file to be displayed to<br />

the user. The getDescription() method returns a short description of the file, generally not<br />

shown to the user. getTypeDescription() should return a short description of the general<br />

kind of file, also generally not shown to the user. The getIcon() method returns a<br />

javax.swing.ImageIcon object for the type of file, which is generally shown to the user to<br />

the left of the filename. Finally, isTraversable() should return true for directories the user<br />

can enter; returning false for a directory prevents the user from opening it. Example 13.10 is<br />

a FileView class that describes compressed files.<br />

Example 13.10. CompressedFileView<br />

import java.io.*;<br />

import javax.swing.*;<br />

import javax.swing.filechooser.*;<br />

public class CompressedFileView extends FileView {<br />

ImageIcon zipIcon = new ImageIcon("images/zipIcon.gif");<br />

ImageIcon gzipIcon = new ImageIcon("images/gzipIcon.gif");<br />

ImageIcon deflateIcon = new ImageIcon("images/deflateIcon.gif");<br />

public String getName(File f) {<br />

return f.getName();<br />

}<br />

public String getTypeDescription(File f) {<br />

}<br />

if (f.getName().endsWith(".zip")) return "Zip archive";<br />

if (f.getName().endsWith(".gz")) return "Gzipped file";<br />

if (f.getName().endsWith(".dfl")) return "Deflated file";<br />

return null;<br />

325

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!