HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Customizing JFileChooser"
Vol. 5, Issue 7, pg. 40

Download files assoicated with this article

	

Listing 1: 

package filechooser; 
import java.io.File; 
import java.util.List; 
public interface DirectoryService { 
public final static int TYPE_LEAGUE = 0; 
public final static int TYPE_TEAM = 1; 
public final static int TYPE_PLAYER = 2; 
/** 
* Returns the children of the parent directory. 
*/ 
public File[] getChildren(File aDir); 
/** 
* Returns the root (in this case, Baseball). 
*/ 
public File getRoot(); 
/** 
* Returns the type (for example, Player). 
*/ 
public int getType(File aFile); 
/** 
* Returns whether the file is traversable. 
*/ 
public boolean isTraversable(File aFile); 
/** 
* Creates a new folder in the containing directory. 
*/ 
public File createNewFolder(File aContainingDir); 
/** 
* Determines if the file should be displayed 
* based on the current filter. 
*/ 
public boolean acceptFilter(File aFile, 
String aCurrentFilter); 
} 
 
Listing 2: 

package filechooser; 
import java.io.*; 
import java.awt.*; 
import javax.swing.*; 
import javax.swing.filechooser.*; 
/** 
* This class provides a view into a generic 
* directory service. 
*/ 
public class DirectoryServiceFileSystemView 
extends FileSystemView { 
private DirectoryService directoryService; 
public DirectoryServiceFileSystemView( 
DirectoryService 
aDirectoryService){ 
this.directoryService = aDirectoryService; 
} 
/** 
* Creates a new folder with a default folder name. 
* This method calls into the currently opened 
* directory service to obtain the directory service 
* system specific way to create a new folder. 
*/ 
public File createNewFolder(File aContainingDir) 
throws IOException { 
File f = directoryService. 
createNewFolder(aContainingDir); 
return f; 
} 
/** 
* Returns all root partitions on this system. 
*/ 
public File[] getRoots() { 
File[] arrFiles = new File[1]; 
arrFiles[0] = directoryService.getRoot(); 
return arrFiles; 
} 
/** 
* Returns whether a file is hidden or not. 
* In the current system, there is no concept of 
* a hidden file. 
*/ 
public boolean isHiddenFile(File f) { 
return false; 
} 
/** 
* Determines if the file is a root partition. 
*/ 
public boolean isRoot(File f) { 
File root = directoryService.getRoot(); 
String rootName = root.getName(); 
String fName = f.getName(); 
return (rootName.equals(fName)); 
} 
/** 
* Returns the user's home directory. 
* The concept of a user's home directory 
* does not directly map to the Directory Service, 
* so, as a result, the root is always returned. 
*/ 
public File getHomeDirectory() { 
return directoryService.getRoot(); 
} 
/** 
* Gets the list of files. Invoked internally 
* by the file chooser when the directory 
* is changed. 
*/ 
public File[] getFiles(File dir, 
boolean useFileHiding){ 
File[] arrFiles = 
directoryService.getChildren(dir); 
return arrFiles; 
} 
} 
  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.