public class FileChooserBuilder extends Object
Class
object as an argument for this purpose). That key is
used to look up the most recently-used directory from any previous invocations
with the same key. This makes it easy to have your user interface
“remember” where the user keeps particular types of files, and
saves the user from having to navigate through the same set of directories
every time they need to locate a file from a particular place.
FileChooserBuilder
's methods each return this
, so
it is possible to chain invocations to simplify setting up a file chooser.
Example usage:
//The default dir to use if no value is stored File home = new File (System.getProperty("user.home") + File.separator + "lib"); //Now build a file chooser and invoke the dialog in one line of code //"libraries-dir" is our unique key File toAdd = new FileChooserBuilder ("libraries-dir").setTitle("Add Library"). setDefaultWorkingDirectory(home).setApproveText("Add").showOpenDialog(); //Result will be null if the user clicked cancel or closed the dialog w/o OK if (toAdd != null) { //do something }
Instances of this class are intended to be thrown away after use. Typically you create a builder, set it to create file choosers as you wish, then use it to show a dialog or create a file chooser you then do something with.
Supports the most common subset of JFileChooser functionality; if you need to do something exotic with a file chooser, you are probably better off creating your own.
Note: If you use the constructor that takes a Class
object,
please use new FileChooserBuilder(MyClass.class)
, not
new FileChooserBuilder(getClass())
. This avoids unexpected
behavior in the case of subclassing.
Modifier and Type | Class and Description |
---|---|
static interface |
FileChooserBuilder.BadgeProvider
Provides "badges" for icons that indicate files or folders of particular
interest to the user.
|
static interface |
FileChooserBuilder.SelectionApprover
Object which can approve the selection (enabling the OK button or
equivalent) in a JFileChooser.
|
Constructor and Description |
---|
FileChooserBuilder(Class type)
Create a new FileChooserBuilder using the name of the passed class
as the metadata for looking up a starting directory from previous
application sessions or invocations.
|
FileChooserBuilder(String dirKey)
Create a new FileChooserBuilder.
|
Modifier and Type | Method and Description |
---|---|
FileChooserBuilder |
addDefaultFileFilters()
Add all default file filters to the file chooser.
|
FileChooserBuilder |
addFileFilter(FileFilter filter)
Equivalent to calling
JFileChooser.addChoosableFileFilter(filter) . |
static FileChooserBuilder |
create(FileSystem fileSystem)
Creates a new FileChooserBuilder which can interact with the given file system.
|
JFileChooser |
createFileChooser()
Create a JFileChooser that conforms to the parameters set in this
builder.
|
FileChooserBuilder |
forceUseOfDefaultWorkingDirectory(boolean val)
Force use of the failover directory - i.e.
|
FileChooserBuilder |
setAcceptAllFileFilterUsed(boolean accept)
Determines whether the
AcceptAll FileFilter is used
as an available choice in the choosable filter list. |
FileChooserBuilder |
setAccessibleDescription(String aDescription)
Set the accessible description for any file choosers created by this
builder
|
FileChooserBuilder |
setApproveText(String val)
Set the text on the OK button for any file chooser dialogs produced
by this builder.
|
FileChooserBuilder |
setBadgeProvider(FileChooserBuilder.BadgeProvider provider)
Provide an implementation of BadgeProvider which will "badge" the
icons of some files.
|
FileChooserBuilder |
setControlButtonsAreShown(boolean val)
Show/hide control buttons
|
FileChooserBuilder |
setDefaultWorkingDirectory(File dir)
Set the current directory which should be used only if
a last-used directory cannot be found for the key string passed
into this builder's constructor.
|
FileChooserBuilder |
setDirectoriesOnly(boolean val)
Set whether or not any file choosers created by this builder will show
only directories.
|
FileChooserBuilder |
setFileFilter(FileFilter filter)
Set a file filter which filters the list of selectable files.
|
FileChooserBuilder |
setFileHiding(boolean fileHiding)
Enable file hiding in any created file choosers
|
FileChooserBuilder |
setFilesOnly(boolean val) |
FileChooserBuilder |
setSelectionApprover(FileChooserBuilder.SelectionApprover approver)
Set a selection approver which can display an "Overwrite file?"
or similar dialog if necessary, when the user presses the accept button
in the file chooser dialog.
|
FileChooserBuilder |
setTitle(String val)
Set the dialog title for any JFileChoosers created by this builder.
|
File[] |
showMultiOpenDialog()
Show an open dialog that allows multiple selection.
|
File |
showOpenDialog()
Show an open dialog with a file chooser set up according to the
parameters of this builder.
|
File |
showSaveDialog()
Show a save dialog with the file chooser set up according to the
parameters of this builder.
|
public FileChooserBuilder(Class type)
type
- A non-null class object, typically the calling classpublic FileChooserBuilder(String dirKey)
dirKey
- A non-null ad-hoc string. If a FileChooser was previously
used with the same string as is passed, then the initial directorypublic static FileChooserBuilder create(FileSystem fileSystem)
fileSystem
- A virtual file systempublic FileChooserBuilder setDirectoriesOnly(boolean val)
val
- true if files should not be shownpublic FileChooserBuilder setFilesOnly(boolean val)
public FileChooserBuilder setBadgeProvider(FileChooserBuilder.BadgeProvider provider)
provider
- A badge provider which will alter the icon of files
or folders that may be of particular interest to the userpublic FileChooserBuilder setTitle(String val)
val
- A localized, human-readable titlepublic FileChooserBuilder setApproveText(String val)
val
- A short, localized, human-readable stringpublic FileChooserBuilder setFileFilter(FileFilter filter)
filter
- public FileChooserBuilder setAcceptAllFileFilterUsed(boolean accept)
AcceptAll FileFilter
is used
as an available choice in the choosable filter list.
If false, the AcceptAll
file filter is removed from
the list of available file filters.
If true, the AcceptAll
file filter will become the
the actively used file filter.accept
- whether the AcceptAll FileFilter
is usedpublic FileChooserBuilder setDefaultWorkingDirectory(File dir)
dir
- A directory to root any created file choosers on if
there is no stored path for this builder's keypublic FileChooserBuilder setFileHiding(boolean fileHiding)
fileHiding
- Whether or not to hide files. Default is no.public FileChooserBuilder setControlButtonsAreShown(boolean val)
val
- Whether or not to hide files. Default is no.public FileChooserBuilder setAccessibleDescription(String aDescription)
aDescription
- The descriptionpublic JFileChooser createFileChooser()
public FileChooserBuilder forceUseOfDefaultWorkingDirectory(boolean val)
val
- public File[] showMultiOpenDialog()
public File showOpenDialog()
public File showSaveDialog()
public FileChooserBuilder addFileFilter(FileFilter filter)
JFileChooser.addChoosableFileFilter(filter)
.
Adds another file filter that can be displayed in the file filters combo
box in the file chooser.filter
- The file filter to addpublic FileChooserBuilder addDefaultFileFilters()
MIMEResolver.Registration.showInFileChooser()
,
MIMEResolver.ExtensionRegistration.showInFileChooser()
public FileChooserBuilder setSelectionApprover(FileChooserBuilder.SelectionApprover approver)
approver
- A SelectionApprover which will determine if the selection
is valid