|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.northwestern.at.utils.swing.FileDialogs
public class FileDialogs
File Dialogs.
Provides a static encapsulation of the Swing and AWT file dialogs for "open" and "save as" so that they use the same interface. Typically the AWT FileDialog is used on MAC OS X while the Swing JFileChooser is used on Windows.
To select a file for input ("open"):
String[] fileToOpen = FileDialogs.open( "Open file" );
if ( fileName != null )
{
File f = new File( fileToOpen[ 0 ] , fileToOpen[ 1 ] );
if ( f.exists() )
{
// Open the file.
...
}
}
To select a file for output ("save" or "save as"):
String[] fileToSave = FileDialogs.open( "Save file" );
if ( fileName != null )
{
File f = new File( fileToOpen[ 0 ] , fileToOpen[ 1 ] );
if ( f.exists() )
{
// Save the file.
...
}
}
FileDialogs remembers the last used open (input) and save (output)
directories. You may retrieve these using the
getOpenDirectory
and getSaveDirectory
methods, and set them using the setOpenDirectory
and
setSaveDirectory
methods.
You may create file filter dialogs based upon file extension using
FileExtensionFilter
and set the open and save dialogs to
use them using addFileFilter
. For example, to add
a filter to allow only files with the jpg, png, and svg extensions to
appear in the file dialogs, use the following:
FileExtensionFilter filter = new FileExtensionFilter( new String[]{ "jpg" , "png", "svg" }, new String[]{ "jpeg files" , "png files" , "svg file" } ); FileDialogs.addFileFilter( filter );
File filters persist between class to the open or save methods. To clear the current list of file filters, use:
FileDialogs.clearFileFilters();
This will reset the file dialogs to display all files.
File filters DO NOT WORK with the AWT dialogs under any MS Windows operating system. File filters work fine with the Swing dialogs under Windows. If you want to use filters with Windows, make sure you use the Swing dialogs. The default under Windows is to use the Swing dialogs.
Nested Class Summary | |
---|---|
protected static class |
FileDialogs.FilterWrapper
Class to wrap a file filter. |
Field Summary | |
---|---|
protected static FileExtensionFilter |
allFilesFilter
Filter allowed all files through. |
protected static java.lang.String |
defaultOpenTitle
Default open file dialog title. |
protected static java.lang.String |
defaultSaveTitle
Default save file dialog title. |
protected static javax.swing.JFileChooser |
fileChooser
Swing file chooser dialog. |
protected static java.awt.FileDialog |
fileDialog
AWT file dialog. |
protected static int |
OPEN
Open file dialog type. |
protected static java.lang.String |
openDirectory
Last directory used for opening a file. |
protected static int |
SAVE
Save file dialog type. |
protected static java.lang.String |
saveDirectory
Last directory used for saving a file. |
protected static boolean |
useSwing
True to use Swing JFileChooser dialogs, false to use AWT. |
Constructor Summary | |
---|---|
protected |
FileDialogs()
Don't allow instantiation but do allow overrides. |
Method Summary | |
---|---|
static void |
addFileFilter(FileExtensionFilter fileExtensionFilter)
Add a file filter. |
static void |
clearFileFilters()
Clear any existing file name filters. |
protected static void |
createFileChooser()
Create Swing file chooser dialog. |
protected static void |
createFileDialog()
Create AWT file dialog. |
protected static java.lang.String[] |
doAWTFileDialog(java.lang.String title,
java.lang.String startDirectory,
int dialogType)
Display AWT file dialog. |
protected static java.lang.String[] |
doFileDialog(java.lang.String title,
java.lang.String startDirectory,
int dialogType)
Display file dialog. |
protected static java.lang.String[] |
doFileDialog(java.awt.Window parentWindow,
java.lang.String title,
java.lang.String startDirectory,
int dialogType)
Display file dialog. |
protected static java.lang.String[] |
doSwingFileDialog(java.awt.Window parentWindow,
java.lang.String title,
java.lang.String startDirectory,
int dialogType)
Display Swing file dialog. |
static java.lang.String |
getOpenDirectory()
Get last open directory. |
static java.lang.String |
getSaveDirectory()
Get last save directory. |
static java.lang.String[] |
open()
Display open file dialog. |
static java.lang.String[] |
open(java.lang.String title)
Display open file dialog. |
static java.lang.String[] |
open(java.awt.Window parentWindow)
Display open file dialog. |
static java.lang.String[] |
open(java.awt.Window parentWindow,
java.lang.String title)
Display open file dialog. |
static java.lang.String[] |
save()
Display save file dialog. |
static java.lang.String[] |
save(java.lang.String title)
Display save file dialog. |
static java.lang.String[] |
save(java.awt.Window parentWindow)
Display save file dialog. |
static java.lang.String[] |
save(java.awt.Window parentWindow,
java.lang.String title)
Display save file dialog. |
static void |
setOpenDirectory(java.lang.String directory)
Set last open directory. |
static void |
setSaveDirectory(java.lang.String directory)
Set last save directory. |
protected static javax.swing.filechooser.FileFilter |
wrapFilter(ExtendedFileNameFilter filter)
Wraps an ExtendedFileNameFilter in a FileFilter . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static boolean useSwing
By default, use AWT for Mac OS X, and Swing for other systems.
protected static java.lang.String openDirectory
protected static java.lang.String saveDirectory
protected static java.awt.FileDialog fileDialog
protected static javax.swing.JFileChooser fileChooser
protected static java.lang.String defaultOpenTitle
protected static java.lang.String defaultSaveTitle
protected static FileExtensionFilter allFilesFilter
protected static final int OPEN
protected static final int SAVE
Constructor Detail |
---|
protected FileDialogs()
Method Detail |
---|
protected static void createFileDialog()
protected static void createFileChooser()
protected static java.lang.String[] doAWTFileDialog(java.lang.String title, java.lang.String startDirectory, int dialogType)
title
- Title for open file dialog.startDirectory
- Directory whose contents should be
displayed in dialog.dialogType
- Type of dialog.
OPEN for opening a file.
SAVE for saving a file.
protected static java.lang.String[] doSwingFileDialog(java.awt.Window parentWindow, java.lang.String title, java.lang.String startDirectory, int dialogType)
parentWindow
- Parent window for file dialog.title
- Title for open file dialog.startDirectory
- Directory whose contents should be
displayed in dialog.dialogType
- Type of dialog.
OPEN for opening a file.
SAVE for saving a file.
protected static java.lang.String[] doFileDialog(java.awt.Window parentWindow, java.lang.String title, java.lang.String startDirectory, int dialogType)
parentWindow
- Parent window for dialog.
Only used for Swing JFileChooser.title
- Title for open file dialog.startDirectory
- Directory whose contents should be
displayed in dialog.dialogType
- Type of dialog.
OPEN for opening a file.
SAVE for saving a file.
protected static java.lang.String[] doFileDialog(java.lang.String title, java.lang.String startDirectory, int dialogType)
title
- Title for open file dialog.startDirectory
- Directory whose contents should be
displayed in dialog.dialogType
- Type of dialog.
OPEN for opening a file.
SAVE for saving a file.
public static java.lang.String[] open(java.awt.Window parentWindow, java.lang.String title)
parentWindow
- Parent window for dialog.title
- Title for open file dialog.
public static java.lang.String[] open(java.lang.String title)
title
- Title for open file dialog.
public static java.lang.String[] open(java.awt.Window parentWindow)
parentWindow
- Parent window for dialog.
public static java.lang.String[] open()
public static java.lang.String[] save(java.awt.Window parentWindow, java.lang.String title)
parentWindow
- Parent window for dialog.title
- Title for open file dialog.
public static java.lang.String[] save(java.awt.Window parentWindow)
parentWindow
- Parent window for dialog.
public static java.lang.String[] save(java.lang.String title)
title
- Title for open file dialog.
public static java.lang.String[] save()
public static java.lang.String getOpenDirectory()
public static void setOpenDirectory(java.lang.String directory)
directory
- The name for the directory to be used to select
a file for opening in the next file dialog. public static java.lang.String getSaveDirectory()
public static void setSaveDirectory(java.lang.String directory)
directory
- The name for the directory to be used to select
a file for opening in the next file dialog. public static void addFileFilter(FileExtensionFilter fileExtensionFilter)
fileExtensionFilter
- The file filter.
Note>: File filters DO NOT WORK with the AWT dialogs under Windows!
public static void clearFileFilters()
The file dialogs will return to displaying all files.
protected static javax.swing.filechooser.FileFilter wrapFilter(ExtendedFileNameFilter filter)
ExtendedFileNameFilter
in a FileFilter
.
filter
- The filter.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |