Class FileDialogs
The service consistently configures filters, remembers the last directory
independently for each caller-supplied purpose, appends save extensions, and
confirms overwrites. Methods return Optional.empty() when the user
cancels, allowing action handlers to avoid chooser result-code boilerplate.
Purpose keys
A purpose is a stable application-defined identifier such as
"problem-json" or "plot-export". Open and save share one
remembered directory per purpose (not separate ones) -- in practice a file
of a given kind is saved and reopened from the same folder far more often
than not, so splitting the two would make the common case worse. A caller
that genuinely wants independent histories can already get that by using
distinct purpose strings for each (e.g. "shapefile-import" vs.
"shapefile-export").
The remembered directory is persisted (via Preferences) across
JVM restarts, keyed by purpose -- see lastDirectory(String) and
rememberDirectory(String, Path), which are also available to
callers that build their own JFileChooser (for filtering a caller
cannot express as a simple extension list) but still want to participate
in the same per-purpose directory memory. Before a purpose has any history
(or its remembered directory no longer exists), Environment's
data directory is used as the initial location -- this is purely a
developer-configured default (see Environment.setDataDirectory(java.lang.String),
typically set once at startup from a command-line argument or environment
variable) and is not updated by every dialog use, so it stays
whatever the developer intended rather than drifting to wherever the user
last happened to browse.
Threading
Like other modal Swing dialogs, openFile(java.awt.Component, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType) and saveFile(java.awt.Component, java.lang.String, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)
must be invoked on the EDT. An IllegalStateException is thrown
otherwise so an accidental worker thread call cannot create
platform-dependent UI behavior. lastDirectory(String) and
rememberDirectory(String, Path) have no such requirement.
-
Method Summary
Modifier and TypeMethodDescriptionlastDirectory(String purpose) Returns the best initial directory forpurpose: the last remembered directory if it still exists, otherwiseEnvironment's developer-configured data directory if that exists, otherwise empty -- leaving it to the caller (or, foropenFile(java.awt.Component, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)/saveFile(java.awt.Component, java.lang.String, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType), toJFileChooser's own platform default, typically the home directory).Shows a single-file open dialog.static voidrememberDirectory(String purpose, Path fileOrDirectory) RemembersfileOrDirectory's directory (or itself, if it is already a directory) as the last visited directory forpurpose, forlastDirectory(String)and subsequentopenFile(java.awt.Component, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)/saveFile(java.awt.Component, java.lang.String, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)calls to pick up -- persisted across JVM restarts.Shows a save dialog with extension normalization and overwrite confirmation.
-
Method Details
-
openFile
public static Optional<Path> openFile(Component parent, String purpose, String title, FileType type) Shows a single-file open dialog.- Parameters:
parent- parent component, ornullpurpose- stable directory-history keytitle- dialog titletype- accepted file type- Returns:
- selected path, or empty if cancelled
-
saveFile
public static Optional<Path> saveFile(Component parent, String purpose, String title, String suggestedFilename, FileType type) Shows a save dialog with extension normalization and overwrite confirmation.- Parameters:
parent- parent component, ornullpurpose- stable directory-history keytitle- dialog titlesuggestedFilename- initial filename, ornullfor nonetype- saved file type- Returns:
- normalized target path, or empty if cancelled or overwrite declined
-
lastDirectory
Returns the best initial directory forpurpose: the last remembered directory if it still exists, otherwiseEnvironment's developer-configured data directory if that exists, otherwise empty -- leaving it to the caller (or, foropenFile(java.awt.Component, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)/saveFile(java.awt.Component, java.lang.String, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType), toJFileChooser's own platform default, typically the home directory).Exposed for callers that build their own
JFileChooser(for filteringFileTypecannot express, such as compound extensions) but still want to share this same per-purpose directory memory with the rest of the application.- Parameters:
purpose- stable directory-history key- Returns:
- the resolved initial directory, or empty
-
rememberDirectory
RemembersfileOrDirectory's directory (or itself, if it is already a directory) as the last visited directory forpurpose, forlastDirectory(String)and subsequentopenFile(java.awt.Component, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)/saveFile(java.awt.Component, java.lang.String, java.lang.String, java.lang.String, edu.cnu.mdi.dialog.FileType)calls to pick up -- persisted across JVM restarts.- Parameters:
purpose- stable directory-history keyfileOrDirectory- a file just opened/saved, or a directory just navigated to
-