Understanding JDIC File-Type Associations

   
By Jack Conradson, March 2006  

Articles Index

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:

  • Setting desktop file-type associations
  • Launching a web browser into an Abstract Window Toolkit (AWT) canvas
  • Packaging Java Web Start software programs into native installers
  • Supporting tray icons and pop-up menus
  • Launching desktop programs to open, edit, and print files

This article will take a closer look at the first of the five features listed above: file-type associations.

What Is an Association?

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 .doc or .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: open, print, and 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 .txr extension.

 
Figure 1: Associations in Windows XP

JDIC Association Classes

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 AssociationService, Association, and 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.

  • org.jdesktop.jdic.filetypes.Association
    The Association class 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.
  • org.jdesktop.jdic.filetypes.Action
    The Action class allows the programmer to specify actions for file types. Actions include open or 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 Action objects to Association objects for them to have a corresponding file type.
  • org.jdesktop.jdic.filetypes.AssociationService
    The AssociationService class allows programmers to add or remove file types from the table of desktop file types. The AssociationService can add and remove file types at both the administrator and the user level. The class uses an Association to add or remove file types. When the AssociationService adds 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 AssociationService object. The AssociationService can also search the table of file types for a specific file extension or MIME type. If the searched-for file type is found, an Association object representing that file type will be returned. The AssociationService will be given access to the entire table of file types, so it is possible for a programmer to change something like a .doc file 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.

Registering Associations

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, .LOG, .LOG, or .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.

import org.jdesktop.jdic.desktop.*;
                  

import org.jdesktop.jdic.filetypes.*;
                  

                   

AssociationService serv = new AssociationService();
                  

Association logassoc = new Association();
                  

                   

// Adds the .log type to the Association object.
                  

                   

logassoc.addFileExtension("LOG");
                  

           
                  

// Adds an Action to the Association object that will
                  

// open a .log file with Windows Notepad.
                  

                   

logassoc.addAction(
                  

  new org.jdesktop.jdic.filetypes.Action(
                  

    "open",
                  

    "C:\\WINDOWS\\system32\\NOTEPAD.EXE %1"));
                  

                 
                  

try {
                  

                   

   // Adds the .log Association to the file types' table
                  

   // at the user level using an AssociationService object.
                  

                   

   serv.registerUserAssociation(logassoc);
                  

                   

}
                  

catch (java.lang.IllegalArgumentException e) {
                  

                   

   // This exception will be caught if the given Association is not valid
                  

   // to be added to the table of file types.
                                     

                   

   System.err.println(e);
                  

                   

}
                  

catch (AssociationAlreadyRegisteredException e) {
                  

                   

   // This exception will be caught if the Association already
                  

   // exists in the table of file types.
                  

                   

   System.err.println(e);
                  

                   

}
                  

catch (RegisterFailedException e) {
                  

                   

   // This exception will be caught if the Association was
                  

   // unable to be added to the table of file types.
                  

                   

   System.err.println(e);
                  

                   

}
                  

                
 

After the .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.

 
Figure 2: Dialog-Box Windows After Creating a .LOG  Association

Removing Associations

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.

import org.jdesktop.jdic.desktop.*;
                  

import org.jdesktop.jdic.filetypes.*;
                  

                   

AssociationService serv = new AssociationService();
                  

                   

// This uses an AssociationService to search the table of file
                  

// types for the .log extension. If the .log file is found,
                  

// an Association object representing the .log file type
                  

// will be returned. Otherwise, null is returned.
                  

                   

Association logassoc = serv.getFileExtensionAssociation("LOG");
                  

                   

try {
                  

                   

   // The AssociationService will remove the .log file type from
                  

   // the table of file types.
                  

                   

   serv.unregisterUserAssociation(logassoc);
                  

                   

} catch (java.lang.IllegalArgumentException e) {
                  

                   

   // This exception will be caught if the given Association is not valid
                  

   // to be removed from the table of file types.
                  

                   

   System.err.println(e);
                  

                   

} catch (AssociationNotRegisteredException e) {
                  

                   

   // This exception will be caught if the Association does not already  
                  

   // exist in the table of file types.
                  

                   

   System.err.println(e);
                  

                   

} catch (RegisterFailedException e) {
                  

                   

   // This exception will be caughtif the association was unable to be
                  

   // removed from the table of file types.
                  

                   

   System.err.println(e);
                  

                   

}
                  

                
 

After the .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.

 
Figure 3: Windows After Removing a .LOG  Association

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 open or 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.

 
Figure 4: The Complete Example Application Running in Windows XP

Conclusion

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.

Source Code

You can 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.

For More Information

JDIC home page
Java.net
The org.jdesktop.jdic.filetypes package Javadocs
NetBeans IDE

About the Author

Jack Conradson is a Sun Microsystems intern working as a writer with the JSC 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.

Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.