001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import java.util.Collection;
005
006import javax.swing.JFileChooser;
007import javax.swing.filechooser.FileFilter;
008
009import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
010import org.openstreetmap.josm.gui.widgets.FileChooserManager;
011import org.openstreetmap.josm.tools.Shortcut;
012
013/**
014 * Helper class for all actions that access the disk.
015 * @since 78
016 */
017public abstract class DiskAccessAction extends JosmAction {
018
019    /**
020     * Constructs a new {@code DiskAccessAction}.
021     *
022     * @param name The action's text as displayed on the menu (if it is added to a menu)
023     * @param iconName The filename of the icon to use
024     * @param tooltip A longer description of the action that will be displayed in the tooltip
025     * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut
026     * @since 1084
027     */
028    public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) {
029        super(name, iconName, tooltip, shortcut, true);
030    }
031
032    /**
033     * Constructs a new {@code DiskAccessAction}.
034     *
035     * @param name The action's text as displayed on the menu (if it is added to a menu)
036     * @param iconName The filename of the icon to use
037     * @param tooltip  A longer description of the action that will be displayed in the tooltip
038     * @param shortcut A ready-created shortcut object or null if you don't want a shortcut
039     * @param register Register this action for the toolbar preferences?
040     * @param toolbarId Identifier for the toolbar preferences. The iconName is used, if this parameter is null
041     * @param installAdapters False, if you don't want to install layer changed and selection changed adapters
042     * @since 5438
043     */
044    public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
045            String toolbarId, boolean installAdapters) {
046        super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
047    }
048
049    /**
050     * Creates a new {@link AbstractFileChooser} and makes it visible.
051     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
052     * @param multiple If true, makes the dialog allow multiple file selections
053     * @param title The string that goes in the dialog window's title bar
054     * @return The {@code AbstractFileChooser}.
055     * @since 1646
056     */
057    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
058        return createAndOpenFileChooser(open, multiple, title, null);
059    }
060
061    /**
062     * Creates a new {@link AbstractFileChooser} and makes it visible.
063     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
064     * @param multiple If true, makes the dialog allow multiple file selections
065     * @param title The string that goes in the dialog window's title bar
066     * @param extension The file extension that will be selected as the default file filter
067     * @return The {@code AbstractFileChooser}.
068     * @since 2020
069     */
070    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {
071        return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);
072    }
073
074    /**
075     * Creates a new {@link AbstractFileChooser} and makes it visible.
076     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
077     * @param multiple If true, makes the dialog allow multiple file selections
078     * @param title The string that goes in the dialog window's title bar
079     * @param extension The file extension that will be selected as the default file filter
080     * @param selectionMode The selection mode that allows the user to:<br><ul>
081     *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
082     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
083     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
084     * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
085     *                 If false, only the file filters that include {@code extension} will be proposed
086     * @param lastDirProperty The name of the property used to setup the AbstractFileChooser initial directory.
087     *        This property will then be updated to the new "last directory" chosen by the user.
088     *        If null, the default property "lastDirectory" will be used.
089     * @return The {@code AbstractFileChooser}.
090     * @since 5438
091     */
092    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension,
093            int selectionMode, boolean allTypes, String lastDirProperty) {
094        return new FileChooserManager(open, lastDirProperty)
095            .createFileChooser(multiple, title, extension, allTypes, selectionMode).openFileChooser();
096    }
097
098    /**
099     * Creates a new {@link AbstractFileChooser} for a single {@link FileFilter} and makes it visible.
100     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
101     * @param multiple If true, makes the dialog allow multiple file selections
102     * @param title The string that goes in the dialog window's title bar
103     * @param filter The only file filter that will be proposed by the dialog
104     * @param selectionMode The selection mode that allows the user to:<br><ul>
105     *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
106     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
107     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
108     * @param lastDirProperty The name of the property used to setup the AbstractFileChooser initial directory.
109     * This property will then be updated to the new "last directory" chosen by the user
110     * @return The {@code AbstractFileChooser}.
111     * @since 5438
112     */
113    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, FileFilter filter,
114            int selectionMode, String lastDirProperty) {
115        return new FileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filter, selectionMode).openFileChooser();
116    }
117
118    /**
119     * Creates a new {@link AbstractFileChooser} for several {@link FileFilter}s and makes it visible.
120     * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
121     * @param multiple If true, makes the dialog allow multiple file selections
122     * @param title The string that goes in the dialog window's title bar
123     * @param filters The file filters that will be proposed by the dialog
124     * @param defaultFilter The file filter that will be selected by default
125     * @param selectionMode The selection mode that allows the user to:<br><ul>
126     *                      <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
127     *                      <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
128     *                      <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
129     * @param lastDirProperty The name of the property used to setup the JFileChooser initial directory.
130     * This property will then be updated to the new "last directory" chosen by the user
131     * @return The {@code AbstractFileChooser}.
132     * @since 5438
133     */
134    public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title,
135            Collection<? extends FileFilter> filters, FileFilter defaultFilter, int selectionMode, String lastDirProperty) {
136        return new FileChooserManager(open, lastDirProperty).createFileChooser(multiple, title, filters, defaultFilter, selectionMode)
137                .openFileChooser();
138    }
139}