// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.gui.widgets;
import java.awt.Component;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.DiskAccessAction;
import org.openstreetmap.josm.actions.ExtensionFileFilter;
import org.openstreetmap.josm.actions.SaveActionBase;
import org.openstreetmap.josm.data.preferences.BooleanProperty;
/**
* A chained utility class used to create and open {@link AbstractFileChooser} dialogs.
* Use only this class if you need to control specifically your AbstractFileChooser dialog.
*
* A simpler usage is to call the {@link DiskAccessAction#createAndOpenFileChooser} methods.
*
* @since 5438 (creation)
* @since 7578 (rename)
*/
public class FileChooserManager {
/**
* Property to enable use of native file dialogs.
*/
public static final BooleanProperty PROP_USE_NATIVE_FILE_DIALOG = new BooleanProperty("use.native.file.dialog",
// Native dialogs do not support file filters, so do not set them as default, except for OS X where they never worked
Main.isPlatformOsx());
private final boolean open;
private final String lastDirProperty;
private final String curDir;
private AbstractFileChooser fc;
/**
* Creates a new {@code FileChooserManager}.
* @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.
* @see #createFileChooser
*/
public FileChooserManager(boolean open) {
this(open, null);
}
// CHECKSTYLE.OFF: LineLength
/**
* Creates a new {@code FileChooserManager}.
* @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.
* @param 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 #createFileChooser
*/
public FileChooserManager(boolean open, String lastDirProperty) {
this(open, lastDirProperty, null);
}
/**
* Creates a new {@code FileChooserManager}.
* @param open If true, "Open File" dialogs will be created. If false, "Save File" dialogs will be created.
* @param 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.
* @param defaultDir The default directory used to initialize the AbstractFileChooser if the {@code lastDirProperty} property value is missing.
* @see #createFileChooser
*/
public FileChooserManager(boolean open, String lastDirProperty, String defaultDir) {
this.open = open;
this.lastDirProperty = lastDirProperty == null || lastDirProperty.isEmpty() ? "lastDirectory" : lastDirProperty;
this.curDir = Main.pref.get(this.lastDirProperty).isEmpty() ?
defaultDir == null || defaultDir.isEmpty() ? "." : defaultDir
: Main.pref.get(this.lastDirProperty);
}
// CHECKSTYLE.ON: LineLength
/**
* Replies the {@code AbstractFileChooser} that has been previously created.
* @return The {@code AbstractFileChooser} that has been previously created, or {@code null} if it has not been created yet.
* @see #createFileChooser
*/
public final AbstractFileChooser getFileChooser() {
return fc;
}
/**
* Replies the initial directory used to construct the {@code AbstractFileChooser}.
* @return The initial directory used to construct the {@code AbstractFileChooser}.
*/
public final String getInitialDirectory() {
return curDir;
}
/**
* Creates a new {@link AbstractFileChooser} with default settings. All files will be accepted.
* @return this
*/
public final FileChooserManager createFileChooser() {
return doCreateFileChooser(false, null, null, null, null, JFileChooser.FILES_ONLY, false);
}
/**
* Creates a new {@link AbstractFileChooser} with given settings for a single {@code FileFilter}.
*
* @param multiple If true, makes the dialog allow multiple file selections
* @param title The string that goes in the dialog window's title bar
* @param filter The only file filter that will be proposed by the dialog
* @param selectionMode The selection mode that allows the user to: