|By Jack Conradson, March 2006|
Sun Microsystems is sponsoring the JDesktop Integration Components (JDIC) project, which allows Java technology access to some operating system (OS)-specific features, including the following:
This article will take a closer look at the first of the five features listed above: file-type associations.
Every OS -- whether Microsoft Windows, Linux, Solaris Operating Environment, or Apple Macintosh OS X -- has some type of desktop graphical user interface (GUI) to represent the files on computers along with the programs the computers can run. However, when a user clicks on a data file, how does the OS know what program to use to open the file? When the user decides to change a file name in Windows but neglects to add on a file extension (such as
.txt), why does Windows suddenly forget the application that the program works with?
The answer to these questions is simple: Each OS has a data table to hold information relating to the file extension. Each file extension is associated with a file type, and each file type in the table has one or more actions associated with it. These actions typically include things such as opening, editing, or printing the file. In addition, each action will have the file path to an executable that can perform the associated function. When an action is performed on a file, the OS will look into this table to find a default executable to perform the proper action. If the OS cannot find that information in the table, it will ask the user to select a program to perform the desired action.
In Windows XP, you can access the table of file types through the Folder Options, located at the bottom of the Tools menu of any file-system window. Figure 1 shows this table with the
.TXT extension opened for editing. Select the
TXT extension and press the Advanced button near the bottom of the dialog box. Note that three actions now appear in a subdialog box:
printto. Select the
open action and press Edit to raise a third dialog box. Note that the
open action uses the
NOTEPAD.EXE executable whenever the
open action is performed on the file type associated with the
.txt extension. If the user inadvertently renames a
.txt file to end in
.txr, the desktop would no longer know what program to use to open the file because the table of file types does not contain a
With the founding mantra "Write Once, Run Anywhere," the Java platform has long avoided OS-specific tasks. This included Java technology applications having access to the table of desktop file types. JDIC has addressed this problem. By installing the
JDIC libraries, Java platform programmers will have access to several classes to work with associations. The three primary classes that you should be familiar with are
Action. All three are located in the
org.jdesktop.jdic.filetypes package. Note that this article refers only to the
Action class in the
org.jdesktop.jdic.filetypes package and has no relation to any AWT or Java Foundation Classes/Swing (JFC/Swing) classes.
Associationclass allows programmers to create new file types or edit existing ones. The class includes methods to edit the file extension type, the MIME extension type, the icon that goes with the file type, and the actions that are associated with the file type.
Actionclass allows the programmer to specify actions for file types. Actions include
edit, as previously mentioned. You can then give each action an executable's file path to perform the correct action on the file. You must add
Associationobjects for them to have a corresponding file type.
AssociationServiceclass allows programmers to add or remove file types from the table of desktop file types. The
AssociationServicecan add and remove file types at both the administrator and the user level. The class uses an
Associationto add or remove file types. When the
AssociationServiceadds or edits a file type in the table, the file type is persistent. It will last beyond the life of the Java technology application that created or edited the file type, and it will last in the table of file types until it is removed either externally or by an
AssociationServicecan also search the table of file types for a specific file extension or MIME type. If the searched-for file type is found, an
Associationobject representing that file type will be returned. The
AssociationServicewill be given access to the entire table of file types, so it is possible for a programmer to change something like a
.docfile extension to open with StarOffice instead of Microsoft Word.
The following examples illustrate how these classes can be used. Note that all the code examples in this article use the latest stable release of JDIC, version 0.9.1.
The first example associates the
.log file extension with an
open action such that the
.log file type will be opened with Windows Notepad. The example then adds the
.log file type to the table of file types. The Windows OS ignores case, so we can write the extension as either
.loG. Once the code is executed, a user could double-click on a
.log file to open it with Notepad. Again, the
Association is persistent, meaning that it will stay in the table of file types even after the Java technology application that added it has been terminated.
.log file type is added, the table of file types will look similar to Figure 2. A
.log file, as it would appear in the Windows file system, is also included in the figure to show what the icon will look like after the addition.
Not only can you add associations to the OS, you can remove them as well. The following code will remove the
.log association from the table of file types. It uses an
AssociationService object to search for the
.log association and then remove it permanently from the table of file types. As expected, after the Java technology application that removed the
.log file type is terminated, the
.log file type will remain removed.
.log file type is removed, the table of file types will look similar to Figure 3. A
.log file is also included in the figure to show what the icon will look like after the file type is removed.
Although this example illustrates what JDIC can do with file types, it has at least one major limitation. If the file types are to be associated with various actions such as
edit, they will need an executable to open or edit them. The executable's path is completely system dependent, so using actions will not allow for platform independence. However, most of JDIC's features are not as platform dependent as the file types' classes are.
The examples this article has discussed present the gist of a complete runnable example. You can download a NetBeans IDE 5.0 project for a complete application at the end of this article, with source code that shows how to use JDIC desktop associations. This example uses a
JFrame with only a menu bar to allow the user to add and remove a
.jlog file type from the table of file types. When the
.jlog file type is in the table of file types, the user can open a
.jlog file created for this example by the Java technology application. Opening the
.jlog file from within the application requires a little help from one of JDIC's other classes --
org.jdesktop.jdic.desktop.Desktop. This class gives Java technology applications the ability to open, edit, and print files using native programs.
Figure 4 shows the application running in Windows XP, with the error log menu visible. The example assumes that the
.jlog file type is not currently in the table of file types.
With JDIC's many powerful features, Java technology programmers now have a tool that gives them access to formerly inaccessible OS-specific features. This article has discussed how JDIC works with file-type associations across several operating systems, and it has presented a complete example that allows the user to reset a file association on the host OS. As evidenced by this application, the Java platform is starting to bridge the gap between native applications and Java technology applications with JDIC.
download the source code for the complete application as a NetBeans IDE 5.0 project. If you are interested in only the source-code files, look inside the archive's
src directory. Note that you will need to download the latest
JDesktop Integration Components (JDIC) libraries and include them either as a compile-time library in the NetBeans Project Properties or in the classpath if you are compiling and running this application outside of the NetBeans IDE.
Jack Conradson is a Sun Microsystems intern working as a writer with the java.sun.com team. A fourth-year student at California Polytechnic State University in San Luis Obispo, he plans to graduate next quarter with a degree in computer science.