com.informagen.FilterByMacOSType
com.informagen.FilterBySuffix

Selecting filetypes using MacOS file signatures in Java


FilterByMacOSType.java
This class implements the "java.io.FilenameFilter" interface so that Macintosh OS creator and file type signatures can be used to select files when used with "java.io.FileDialog".

This FilenameFilter can only be used on Macintosh platform. See the usage example below.
public static boolean isMacintosh()
public void addSignature(String macCreator, String macOSType)
public boolean accept(File directory, String filename)
FilterBySuffix.java
This class implements the "java.io.FilenameFilter" interface so that files can be selected by suffix(s), also know as file extensions, when used with "java.io.FileDialog".

This FilenameFilter can be used with MacOS, Windows, or UNIX platforms.
public void addSuffix(String dotSuffix)
public boolean accept(File directory, String filename)



Usage:

When using the Standard File Dialog on the Macintosh is goes against Apple's Human Interface Guildlines to display all available files when only specific types of files are valid. Platforms such as Windows and UNIX use file suffixes, as known as file extensions, to indicate the file type, this convention is not routinely used by Macintosh users. The Macintosh OS uses two "invisible" fields called the "Creator" and "OSType" to specify filetype.

The class "FilterByMacOSType" was written so that on the Macintosh files can be specified according to the Human Interface Guildlines and not require the Macintosh user to adopt an artificial suffix naming syntax.

The class "FilterByMacOSType" has a static function which can be used to determine the current platform. The class "FilterBySuffix" can be used with Windows or UNIX platforms, the "dot" is optional in the suffix. It is safe to include the class "FilterByMacOSType" on non-Macintosh platforms as long as it is not instanced nor its methods involked. This class uses the contents of MRJClasses.jar which is part of the standard MRJ (Macintosh Runtime for Java) installation.

Creator and OSType are specified as four character Java Strings, the string "****" can be used to indicate a wildcard match for either the creator and OSType.

import com.informagen.FilterByMacOSType;
import com.informagen.FilterBySuffix;

import java.awt.FileDialog;

import java.io.File;
import java.io.FilenameFilter;

             .
             .
             .


FileDialog dialog = new FileDialog(this, "Select an image file", FileDialog.LOAD);

dialog.setDirectory(System.getProperty("user.dir"));

FilenameFilter fileFilter;


// Use the built-in Platform test to see if we're on a Macintosh
			
if ( FilterByMacOSType.isMacintosh() ) {

    FilterByMacOSType macFileFilter = new FilterByMacOSType();


    // "****" means match any application type

    macFileFilter.addSignature("****", "GIFf");
    macFileFilter.addSignature("****", "GIF ");
    macFileFilter.addSignature("****", "JPEG");

    fileFilter = macFileFilter;
				
} else {

    FilterBySuffix suffixFileFilter = new FilterBySuffix();

    suffixFileFilter.addSuffix(".gif");
    suffixFileFilter.addSuffix(".jpg");
    suffixFileFilter.addSuffix(".jpeg");
		
    fileFilter = suffixFileFilter;
}
			
dialog.setFilenameFilter(fileFilter);
dialog.setVisible(true);
			
if ( dialog.getFile() != null ) {

    File aFile = new File(dialog.getDirectory(), dialog.getFile());

             .
             .
             .

    }
			




Copyright ©1999 Informagen, Inc.