| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.util.Collection;
|
|---|
| 7 |
|
|---|
| 8 | import javax.swing.JFileChooser;
|
|---|
| 9 | import javax.swing.filechooser.FileFilter;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 12 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 13 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
|---|
| 14 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
|---|
| 15 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Helper class for all actions that access the disk.
|
|---|
| 20 | * @since 78
|
|---|
| 21 | */
|
|---|
| 22 | public abstract class DiskAccessAction extends JosmAction {
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Constructs a new {@code DiskAccessAction}.
|
|---|
| 26 | *
|
|---|
| 27 | * @param name The action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 28 | * @param iconName The filename of the icon to use
|
|---|
| 29 | * @param tooltip A longer description of the action that will be displayed in the tooltip
|
|---|
| 30 | * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut
|
|---|
| 31 | * @since 1084
|
|---|
| 32 | */
|
|---|
| 33 | protected DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) {
|
|---|
| 34 | super(name, iconName, tooltip, shortcut, true);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Constructs a new {@code DiskAccessAction}.
|
|---|
| 39 | *
|
|---|
| 40 | * @param name The action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 41 | * @param iconName The filename of the icon to use
|
|---|
| 42 | * @param tooltip A longer description of the action that will be displayed in the tooltip
|
|---|
| 43 | * @param shortcut A ready-created shortcut object or null if you don't want a shortcut
|
|---|
| 44 | * @param register Register this action for the toolbar preferences?
|
|---|
| 45 | * @param toolbarId Identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
|---|
| 46 | * @param installAdapters False, if you don't want to install layer changed and selection changed adapters
|
|---|
| 47 | * @since 5438
|
|---|
| 48 | */
|
|---|
| 49 | protected DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
|
|---|
| 50 | String toolbarId, boolean installAdapters) {
|
|---|
| 51 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Creates a new {@link AbstractFileChooser} and makes it visible.
|
|---|
| 56 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
|---|
| 57 | * @param multiple If true, makes the dialog allow multiple file selections
|
|---|
| 58 | * @param title The string that goes in the dialog window's title bar
|
|---|
| 59 | * @return The {@code AbstractFileChooser}.
|
|---|
| 60 | * @since 1646
|
|---|
| 61 | */
|
|---|
| 62 | public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
|
|---|
| 63 | return createAndOpenFileChooser(open, multiple, title, null);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Creates a new {@link AbstractFileChooser} and makes it visible.
|
|---|
| 68 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
|---|
| 69 | * @param multiple If true, makes the dialog allow multiple file selections
|
|---|
| 70 | * @param title The string that goes in the dialog window's title bar
|
|---|
| 71 | * @param extension The file extension that will be selected as the default file filter
|
|---|
| 72 | * @return The {@code AbstractFileChooser}.
|
|---|
| 73 | * @since 2020
|
|---|
| 74 | */
|
|---|
| 75 | public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {
|
|---|
| 76 | return createAndOpenFileChooser(open, multiple, title, extension, JFileChooser.FILES_ONLY, true, null);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Creates a new {@link AbstractFileChooser} and makes it visible.
|
|---|
| 81 | * @param open If true, pops up an "Open File" dialog. If false, pops up a "Save File" dialog
|
|---|
| 82 | * @param multiple If true, makes the dialog allow multiple file selections
|
|---|
| 83 | * @param title The string that goes in the dialog window's title bar
|
|---|
| 84 | * @param extension The file extension that will be selected as the default file filter
|
|---|
| 85 | * @param selectionMode The selection mode that allows the user to:<br><ul>
|
|---|
| 86 | * <li>just select files ({@code JFileChooser.FILES_ONLY})</li>
|
|---|
| 87 | * <li>just select directories ({@code JFileChooser.DIRECTORIES_ONLY})</li>
|
|---|
| 88 | * <li>select both files and directories ({@code JFileChooser.FILES_AND_DIRECTORIES})</li></ul>
|
|---|
| 89 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
|---|
| 90 | * If false, only the file filters that include {@code extension} will be proposed
|
|---|
| 91 | * @param lastDirProperty The name of the property used to setup the AbstractFileChooser initial directory.
|
|---|
| 92 | * This property will then be updated to the new "last directory" chosen by the user.
|
|---|
| 93 | * If null, the default property "lastDirectory" will be used.
|
|---|
| 94 | * @return The {@code AbstractFileChooser}.
|
|---|
| 95 | * @since 5438
|
|---|
| 96 | */
|
|---|
| 97 | public static AbstractFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension,
|
|---|
| 98 | int selectionMode, boolean allTypes, String lastDirProperty) {
|
|---|
| 99 | 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 | }
|
|---|