|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Customizing JFileChooser
Customizing JFileChooser
By: Justin Hill
Jul. 1, 2000 12:00 AM
Front-end architecture and the art of developing GUIs that are functional and intuitive have been a challenge in this industry for quite some time. The advent of Java has made things a little easier; Java's extensive Swing package has assisted in the rapid creation of GUIs for application development. While the package provides a vast array of robust graphical widgets, there comes a point when a developer will stretch the limits of the component by attempting to provide functionality that is either not supported or nonexistent. JFileChooser is an example of one such component. The purpose of this article is to provide the reader with the knowledge and understanding to extend JFileChooser's functionality in order to display information from a generic directory service.
JFileChooser and FileSystemView FileSystemView is an abstract class that's the gateway to the file system. To create a file chooser that accesses an alternate file system, you'll need to provide an implementation of this class and pass it into the constructor of JFileChooser. When JFileChooser constructs itself, it checks to see if a FileSystemView has been provided; if so, it simply uses it. Otherwise, FileSystemView's getFileSystemView() method is invoked. This special method queries the local file system and determines the file separator ("/" in UNIX or "\" in Windows) in order to instantiate one of three default implementations provided in FileSystemView as inner classes. This class doesn't actually access the local file system - or any file system, for that matter. Instead, FileSystemView directly interacts with File objects, which provide more direct access to the local file system. Note that some directory services may not represent themselves as a listing of File objects and therefore will need to be converted to representative File objects so as to enable interaction with FileSystemView and JFileChooser. We'll revisit this issue of converting generic directory service objects to File objects in a later section.
A Look Behind the Scenes As the user interacts with the file chooser, the bulk of the processing work is delegated to FileSystemView's getFiles() method. This method simply takes a File argument as its parameter (the file should be a directory), obtains the files that are in the directory and returns the results as an array of Files. Although a firmer understanding of FileSystemView is necessary, this is enough information to get us started on creating our own extension of JFileChooser.
Customizing JFileChooser The concept of a directory service is an extension of a naming service in that it provides a mechanism for associating objects with a logical name within a searchable structure. A naming service simply maps names to objects. For example, the Domain Name System (DNS) maps people-friendly names (www.yahoo.com) to IP addresses (216.32.74.52). While a naming service allows the lookup of objects based on names, a directory service allows these objects to have elements (or attributes) associated with them. For example, in a directory service of employees, looking up the name Fred Jones would return an object that referred to Fred but also contained attributes like age, salary and job title. Directory services can exist in numerous environments:
Adapter
The interface DirectoryService (see Listing 1) and its concrete implementation (DirectoryServiceSportsAdapter) help to overcome these hurdles. Not only do these classes provide the abstraction layer that allows JFileChooser to connect to alternate directory services without a major code rewrite, they also act as the necessary middle layer communication connection. JFileChooser deals directly with File objects, so the adapter has the ability to convert the directory service objects (whatever they might be) to File objects needed by the file chooser. Furthermore, the adapter allows for asynchronous communication that allows JFileChooser to talk back to the directory service. This mode of communication is important because it allows JFileChooser to access the directory service so as to create new folders, find the root and so on.
DirectoryServiceSportsAdapter The concept of a local cache is important because it prevents having to constantly go back across the wire to get the object corresponding to the File. To guarantee a correct representation of the directory service, when the user selects a different directory, the directory's children are always obtained from the service. However, it's at this point that the directory service objects are cached, and from then on the cached copy is used to perform operations such as checking to see if the current filter allows the File, or obtaining the icon. As mentioned earlier, the bulk of the work of populating JFileChooser is handled via FileSystemView's getFiles(). DirectoryServiceFileSystemView (see Listing 2) provides a specific implementation of FileSystemView by overriding the getFiles() method and delegating the functionality to DirectoryServiceAdapter's getChildren() method. The getChildren() performs the following: Folder folder = (Folder)this.hashmap.get(aDir);If you want to use the extended file chooser widget and its adapter design to connect to an alternate directory service, you'll begin to see the benefits of the abstraction. The advantage of the adapter design pattern is now evident in that any customized directory service adapter implementation is pluggable into this framework.
DirectoryServiceFileChooser and the Helper Classes
JFileChooser Is a JComponent JFileChooser fc = new JFileChooser();Although JFileChooser seems to act like a JDialog, it's actually a JComponent. As a result, when the showDialog() method is invoked, the JFileChooser is created and added to a JDialog, which in turn is handled internally by the JFileChooser. Since JFileChooser is a JComponent, it's possible to do some interesting things with it. For example, you can embed JFileChooser in your own widget simply by creating it and adding it to a specific container. The following code illustrates how to place a JFileChooser in a JFrame: JFrame frame = new JFrame("File Chooser in a Frame");The Selector Dialog The Selector Dialog (see Figure 2) is a dialog with an embedded JFileChooser component. The purpose of this dialog is to allow the user to populate the list on the right side of the component with entities from the left side (the file chooser). One use of this dialog - applying the baseball directory service example - could allow users to specify a list of teams they'd like to search. For example, suppose a person wanted to find all the players in baseball that make more than $5 million a year and play for certain teams. The user(s) could utilize the Selector Dialog to traverse the sports directory service (via the file chooser) and add only the teams they wanted to search. While the left side of the Selector Dialog is simply created using the factory method of DirectoryServiceFileChooser, the remainder of the dialog was constructed to interact with the file chooser (see DirectoryServiceSelectorDialog, available on the JDJ Web site). The three buttons in the middle of the dialog (>>, << and <<<) correspond to Add, Remove and Remove All, respectively, and perform the following functions:
As you can see, the idea of embedding JFileChooser in another component can be both powerful and functional.
Future Enhancements
Conclusion Resources and Acknowledgments
Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||