Class FileChooserManager
- java.lang.Object
-
- org.openstreetmap.josm.gui.widgets.FileChooserManager
-
public class FileChooserManager extends java.lang.Object
A chained utility class used to create and openAbstractFileChooser
dialogs.
Use only this class if you need to control specifically your AbstractFileChooser dialog.
A simpler usage is to call the
DiskAccessAction.createAndOpenFileChooser(boolean, boolean, java.lang.String)
methods.- Since:
- 5438 (creation), 7578 (rename)
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Predicate<ExtensionFileFilter>
additionalTypes
private java.lang.String
curDir
private javax.swing.filechooser.FileFilter
defaultFilter
private java.lang.String
extension
private AbstractFileChooser
fc
private java.io.File
file
private java.util.Collection<? extends javax.swing.filechooser.FileFilter>
filters
private java.lang.String
lastDirProperty
private boolean
multiple
private boolean
open
static BooleanProperty
PROP_USE_DETAILS_VIEW_FILE_DIALOG
Property to use the details view in file dialogs.static BooleanProperty
PROP_USE_NATIVE_FILE_DIALOG
Property to enable use of native file dialogs.private int
selectionMode
private java.lang.String
title
-
Constructor Summary
Constructors Constructor Description FileChooserManager()
Creates a newFileChooserManager
with default values.FileChooserManager(boolean open)
Creates a newFileChooserManager
.FileChooserManager(boolean open, java.lang.String lastDirProperty)
Creates a newFileChooserManager
.FileChooserManager(boolean open, java.lang.String lastDirProperty, java.lang.String defaultDir)
Creates a newFileChooserManager
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FileChooserManager
additionalTypes(java.util.function.Predicate<ExtensionFileFilter> value)
Builder method to setadditionalTypes
property.FileChooserManager
allTypes(boolean value)
Builder method to setallTypes
property.FileChooserManager
createFileChooser()
Creates a newAbstractFileChooser
with default settings.FileChooserManager
createFileChooser(boolean multiple, java.lang.String title, java.lang.String extension, boolean allTypes, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a file extension.FileChooserManager
createFileChooser(boolean multiple, java.lang.String title, java.util.Collection<? extends javax.swing.filechooser.FileFilter> filters, javax.swing.filechooser.FileFilter defaultFilter, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a collection ofFileFilter
s.FileChooserManager
createFileChooser(boolean multiple, java.lang.String title, javax.swing.filechooser.FileFilter filter, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a singleFileFilter
.FileChooserManager
defaultFilter(javax.swing.filechooser.FileFilter value)
Builder method to setdefaultFilter
property.FileChooserManager
doCreateFileChooser()
BuildsFileChooserManager
object using properties set by builder methods or default values.FileChooserManager
extension(java.lang.String value)
Builder method to setextension
property.FileChooserManager
file(java.io.File value)
Builder method to setfile
property.FileChooserManager
filters(java.util.Collection<? extends javax.swing.filechooser.FileFilter> value)
Builder method to setfilters
property.AbstractFileChooser
getFileChooser()
Replies theAbstractFileChooser
that has been previously created.java.io.File
getFileForSave()
Opens the file chooser dialog, then checks if filename has the given extension.java.lang.String
getInitialDirectory()
Replies the initial directory used to construct theAbstractFileChooser
.FileChooserManager
multiple(boolean value)
Builder method to setmultiple
property.AbstractFileChooser
openFileChooser()
Opens theAbstractFileChooser
that has been created.AbstractFileChooser
openFileChooser(java.awt.Component parent)
Opens theAbstractFileChooser
that has been created and waits for the user to choose a file/directory, or cancel the dialog.
When the user chooses a file or directory, thelastDirProperty
is updated to the chosen directory path.FileChooserManager
selectionMode(int value)
Builder method to setselectionMode
property.FileChooserManager
title(java.lang.String value)
Builder method to settitle
property.
-
-
-
Field Detail
-
PROP_USE_NATIVE_FILE_DIALOG
public static final BooleanProperty PROP_USE_NATIVE_FILE_DIALOG
Property to enable use of native file dialogs.
-
PROP_USE_DETAILS_VIEW_FILE_DIALOG
public static final BooleanProperty PROP_USE_DETAILS_VIEW_FILE_DIALOG
Property to use the details view in file dialogs.
-
open
private final boolean open
-
lastDirProperty
private final java.lang.String lastDirProperty
-
curDir
private final java.lang.String curDir
-
multiple
private boolean multiple
-
title
private java.lang.String title
-
filters
private java.util.Collection<? extends javax.swing.filechooser.FileFilter> filters
-
defaultFilter
private javax.swing.filechooser.FileFilter defaultFilter
-
selectionMode
private int selectionMode
-
extension
private java.lang.String extension
-
additionalTypes
private java.util.function.Predicate<ExtensionFileFilter> additionalTypes
-
file
private java.io.File file
-
fc
private AbstractFileChooser fc
-
-
Constructor Detail
-
FileChooserManager
public FileChooserManager()
Creates a newFileChooserManager
with default values.- See Also:
createFileChooser()
-
FileChooserManager
public FileChooserManager(boolean open)
Creates a newFileChooserManager
.- Parameters:
open
- If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.- See Also:
createFileChooser()
-
FileChooserManager
public FileChooserManager(boolean open, java.lang.String lastDirProperty)
Creates a newFileChooserManager
.- Parameters:
open
- If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.lastDirProperty
- The name of the property used to get the last directory. This directory is used to initialize the AbstractFileChooser. Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path.- See Also:
createFileChooser()
-
FileChooserManager
public FileChooserManager(boolean open, java.lang.String lastDirProperty, java.lang.String defaultDir)
Creates a newFileChooserManager
.- Parameters:
open
- If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.lastDirProperty
- The name of the property used to get the last directory. This directory is used to initialize the AbstractFileChooser. Then, if the user effectively chooses a file or a directory, this property will be updated to the directory path.defaultDir
- The default directory used to initialize the AbstractFileChooser if thelastDirProperty
property value is missing.- See Also:
createFileChooser()
-
-
Method Detail
-
getFileChooser
public final AbstractFileChooser getFileChooser()
Replies theAbstractFileChooser
that has been previously created.- Returns:
- The
AbstractFileChooser
that has been previously created, ornull
if it has not been created yet. - See Also:
createFileChooser()
-
getInitialDirectory
public final java.lang.String getInitialDirectory()
Replies the initial directory used to construct theAbstractFileChooser
.- Returns:
- The initial directory used to construct the
AbstractFileChooser
.
-
createFileChooser
public final FileChooserManager createFileChooser()
Creates a newAbstractFileChooser
with default settings. All files will be accepted.- Returns:
- this
-
createFileChooser
public final FileChooserManager createFileChooser(boolean multiple, java.lang.String title, javax.swing.filechooser.FileFilter filter, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a singleFileFilter
.- Parameters:
multiple
- If true, makes the dialog allow multiple file selectionstitle
- The string that goes in the dialog window's title barfilter
- The only file filter that will be proposed by the dialogselectionMode
- The selection mode that allows the user to:- just select files (
JFileChooser.FILES_ONLY
) - just select directories (
JFileChooser.DIRECTORIES_ONLY
) - select both files and directories (
JFileChooser.FILES_AND_DIRECTORIES
)
- just select files (
- Returns:
- this
- See Also:
DiskAccessAction.createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)
-
createFileChooser
public final FileChooserManager createFileChooser(boolean multiple, java.lang.String title, java.util.Collection<? extends javax.swing.filechooser.FileFilter> filters, javax.swing.filechooser.FileFilter defaultFilter, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a collection ofFileFilter
s.- Parameters:
multiple
- If true, makes the dialog allow multiple file selectionstitle
- The string that goes in the dialog window's title barfilters
- The file filters that will be proposed by the dialogdefaultFilter
- The file filter that will be selected by defaultselectionMode
- The selection mode that allows the user to:- just select files (
JFileChooser.FILES_ONLY
) - just select directories (
JFileChooser.DIRECTORIES_ONLY
) - select both files and directories (
JFileChooser.FILES_AND_DIRECTORIES
)
- just select files (
- Returns:
- this
- See Also:
DiskAccessAction.createAndOpenFileChooser(boolean, boolean, String, Collection, FileFilter, int, String)
-
createFileChooser
public final FileChooserManager createFileChooser(boolean multiple, java.lang.String title, java.lang.String extension, boolean allTypes, int selectionMode)
Creates a newAbstractFileChooser
with given settings for a file extension.- Parameters:
multiple
- If true, makes the dialog allow multiple file selectionstitle
- The string that goes in the dialog window's title barextension
- The file extension that will be selected as the default file filterallTypes
- If true, all the files types known by JOSM will be proposed in the "file type" combobox. If false, only the file filters that includeextension
will be proposedselectionMode
- The selection mode that allows the user to:- just select files (
JFileChooser.FILES_ONLY
) - just select directories (
JFileChooser.DIRECTORIES_ONLY
) - select both files and directories (
JFileChooser.FILES_AND_DIRECTORIES
)
- just select files (
- Returns:
- this
- See Also:
DiskAccessAction.createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)
-
multiple
public FileChooserManager multiple(boolean value)
Builder method to setmultiple
property.- Parameters:
value
- If true, makes the dialog allow multiple file selections- Returns:
- this
-
title
public FileChooserManager title(java.lang.String value)
Builder method to settitle
property.- Parameters:
value
- The string that goes in the dialog window's title bar- Returns:
- this
-
filters
public FileChooserManager filters(java.util.Collection<? extends javax.swing.filechooser.FileFilter> value)
Builder method to setfilters
property.- Parameters:
value
- The file filters that will be proposed by the dialog- Returns:
- this
-
defaultFilter
public FileChooserManager defaultFilter(javax.swing.filechooser.FileFilter value)
Builder method to setdefaultFilter
property.- Parameters:
value
- The file filter that will be selected by default- Returns:
- this
-
selectionMode
public FileChooserManager selectionMode(int value)
Builder method to setselectionMode
property.- Parameters:
value
- The selection mode that allows the user to:- just select files (
JFileChooser.FILES_ONLY
) - just select directories (
JFileChooser.DIRECTORIES_ONLY
) - select both files and directories (
JFileChooser.FILES_AND_DIRECTORIES
)
- just select files (
- Returns:
- this
-
extension
public FileChooserManager extension(java.lang.String value)
Builder method to setextension
property.- Parameters:
value
- The file extension that will be selected as the default file filter- Returns:
- this
-
additionalTypes
public FileChooserManager additionalTypes(java.util.function.Predicate<ExtensionFileFilter> value)
Builder method to setadditionalTypes
property.- Parameters:
value
- matching types will additionally be added to the "file type" combobox.- Returns:
- this
-
allTypes
public FileChooserManager allTypes(boolean value)
Builder method to setallTypes
property.- Parameters:
value
- If true, all the files types known by JOSM will be proposed in the "file type" combobox. If false, only the file filters that includeextension
will be proposed- Returns:
- this
-
file
public FileChooserManager file(java.io.File value)
Builder method to setfile
property.- Parameters:
value
-File
object with default filename- Returns:
- this
-
doCreateFileChooser
public FileChooserManager doCreateFileChooser()
BuildsFileChooserManager
object using properties set by builder methods or default values.- Returns:
- this
-
openFileChooser
public final AbstractFileChooser openFileChooser()
Opens theAbstractFileChooser
that has been created.- Returns:
- the
AbstractFileChooser
if the user effectively chooses a file or directory.null
if the user cancelled the dialog.
-
openFileChooser
public AbstractFileChooser openFileChooser(java.awt.Component parent)
Opens theAbstractFileChooser
that has been created and waits for the user to choose a file/directory, or cancel the dialog.
When the user chooses a file or directory, thelastDirProperty
is updated to the chosen directory path.- Parameters:
parent
- The Component used as the parent of the AbstractFileChooser. If null, usesMainApplication.getMainFrame()
.- Returns:
- the
AbstractFileChooser
if the user effectively chooses a file or directory.null
if the user cancelled the dialog.
-
getFileForSave
public java.io.File getFileForSave()
Opens the file chooser dialog, then checks if filename has the given extension. If not, adds the extension and asks for overwrite if filename exists.- Returns:
- the
File
ornull
if the user cancelled the dialog.
-
-