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