[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[290] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.io.File;
|
---|
| 8 | import java.io.IOException;
|
---|
[4371] | 9 | import java.util.Collection;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
[5014] | 12 |
|
---|
[1949] | 13 | import javax.swing.JFileChooser;
|
---|
[290] | 14 | import javax.swing.JOptionPane;
|
---|
[1949] | 15 | import javax.swing.filechooser.FileFilter;
|
---|
[290] | 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
[1397] | 18 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[1523] | 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[290] | 20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[7578] | 21 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
[1949] | 22 | import org.openstreetmap.josm.io.FileExporter;
|
---|
[1084] | 23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[290] | 24 |
|
---|
[9676] | 25 | /**
|
---|
| 26 | * Abstract superclass of save actions.
|
---|
| 27 | * @since 290
|
---|
| 28 | */
|
---|
[1820] | 29 | public abstract class SaveActionBase extends DiskAccessAction {
|
---|
[290] | 30 |
|
---|
[9676] | 31 | /**
|
---|
| 32 | * Constructs a new {@code SaveActionBase}.
|
---|
| 33 | * @param name The action's text as displayed on the menu (if it is added to a menu)
|
---|
| 34 | * @param iconName The filename of the icon to use
|
---|
| 35 | * @param tooltip A longer description of the action that will be displayed in the tooltip
|
---|
| 36 | * @param shortcut A ready-created shortcut object or {@code null} if you don't want a shortcut
|
---|
| 37 | */
|
---|
[1808] | 38 | public SaveActionBase(String name, String iconName, String tooltip, Shortcut shortcut) {
|
---|
[1169] | 39 | super(name, iconName, tooltip, shortcut);
|
---|
| 40 | }
|
---|
[1023] | 41 |
|
---|
[4371] | 42 | @Override
|
---|
[1169] | 43 | public void actionPerformed(ActionEvent e) {
|
---|
[5014] | 44 | if (!isEnabled())
|
---|
[1808] | 45 | return;
|
---|
[9413] | 46 | doSave();
|
---|
[1373] | 47 | }
|
---|
| 48 |
|
---|
[9676] | 49 | /**
|
---|
| 50 | * Saves the active layer.
|
---|
| 51 | * @return {@code true} if the save operation succeeds
|
---|
| 52 | */
|
---|
[1808] | 53 | public boolean doSave() {
|
---|
[10318] | 54 | Layer layer = Main.getLayerManager().getActiveLayer();
|
---|
| 55 | if (layer != null && layer.isSavable()) {
|
---|
| 56 | return doSave(layer);
|
---|
[1750] | 57 | }
|
---|
[5459] | 58 | return false;
|
---|
[1808] | 59 | }
|
---|
[290] | 60 |
|
---|
[9676] | 61 | /**
|
---|
| 62 | * Saves the given layer.
|
---|
| 63 | * @param layer layer to save
|
---|
| 64 | * @return {@code true} if the save operation succeeds
|
---|
| 65 | */
|
---|
[1808] | 66 | public boolean doSave(Layer layer) {
|
---|
[8510] | 67 | if (!layer.checkSaveConditions())
|
---|
[1808] | 68 | return false;
|
---|
[10007] | 69 | return doInternalSave(layer, getFile(layer));
|
---|
[4114] | 70 | }
|
---|
| 71 |
|
---|
[7204] | 72 | /**
|
---|
| 73 | * Saves a layer to a given file.
|
---|
| 74 | * @param layer The layer to save
|
---|
| 75 | * @param file The destination file
|
---|
| 76 | * @param checkSaveConditions if {@code true}, checks preconditions before saving. Set it to {@code false} to skip it
|
---|
| 77 | * if preconditions have already been checked (as this check can prompt UI dialog in EDT it may be best in some cases
|
---|
| 78 | * to do it earlier).
|
---|
| 79 | * @return {@code true} if the layer has been successfully saved, {@code false} otherwise
|
---|
| 80 | * @since 7204
|
---|
| 81 | */
|
---|
| 82 | public static boolean doSave(Layer layer, File file, boolean checkSaveConditions) {
|
---|
| 83 | if (checkSaveConditions && !layer.checkSaveConditions())
|
---|
[1808] | 84 | return false;
|
---|
[4114] | 85 | return doInternalSave(layer, file);
|
---|
| 86 | }
|
---|
[290] | 87 |
|
---|
[5014] | 88 | private static boolean doInternalSave(Layer layer, File file) {
|
---|
[1169] | 89 | if (file == null)
|
---|
[1373] | 90 | return false;
|
---|
[290] | 91 |
|
---|
[1949] | 92 | try {
|
---|
| 93 | boolean exported = false;
|
---|
[6815] | 94 | boolean canceled = false;
|
---|
[10407] | 95 | for (FileExporter exporter : ExtensionFileFilter.getExporters()) {
|
---|
[1949] | 96 | if (exporter.acceptFile(file, layer)) {
|
---|
| 97 | exporter.exportData(file, layer);
|
---|
| 98 | exported = true;
|
---|
[6815] | 99 | canceled = exporter.isCanceled();
|
---|
[4463] | 100 | break;
|
---|
[1949] | 101 | }
|
---|
| 102 | }
|
---|
| 103 | if (!exported) {
|
---|
[2017] | 104 | JOptionPane.showMessageDialog(Main.parent, tr("No Exporter found! Nothing saved."), tr("Warning"),
|
---|
[1949] | 105 | JOptionPane.WARNING_MESSAGE);
|
---|
| 106 | return false;
|
---|
[6815] | 107 | } else if (canceled) {
|
---|
| 108 | return false;
|
---|
[1949] | 109 | }
|
---|
[9510] | 110 | if (!layer.isRenamed()) {
|
---|
| 111 | layer.setName(file.getName());
|
---|
| 112 | }
|
---|
[1949] | 113 | layer.setAssociatedFile(file);
|
---|
[2025] | 114 | if (layer instanceof OsmDataLayer) {
|
---|
| 115 | ((OsmDataLayer) layer).onPostSaveToFile();
|
---|
| 116 | }
|
---|
[1949] | 117 | Main.parent.repaint();
|
---|
| 118 | } catch (IOException e) {
|
---|
[6643] | 119 | Main.error(e);
|
---|
[1949] | 120 | return false;
|
---|
| 121 | }
|
---|
[9303] | 122 | addToFileOpenHistory(file);
|
---|
[1373] | 123 | return true;
|
---|
[1169] | 124 | }
|
---|
[319] | 125 |
|
---|
[1169] | 126 | protected abstract File getFile(Layer layer);
|
---|
[290] | 127 |
|
---|
[1169] | 128 | /**
|
---|
[1808] | 129 | * Refreshes the enabled state
|
---|
[1949] | 130 | *
|
---|
[1808] | 131 | */
|
---|
[1820] | 132 | @Override
|
---|
| 133 | protected void updateEnabledState() {
|
---|
[10318] | 134 | Layer activeLayer = Main.getLayerManager().getActiveLayer();
|
---|
| 135 | setEnabled(activeLayer != null && activeLayer.isSavable());
|
---|
[1808] | 136 | }
|
---|
[1949] | 137 |
|
---|
[5456] | 138 | /**
|
---|
[6830] | 139 | * Creates a new "Save" dialog for a single {@link ExtensionFileFilter} and makes it visible.<br>
|
---|
[5456] | 140 | * When the user has chosen a file, checks the file extension, and confirms overwrite if needed.
|
---|
[6069] | 141 | *
|
---|
[5456] | 142 | * @param title The dialog title
|
---|
| 143 | * @param filter The dialog file filter
|
---|
| 144 | * @return The output {@code File}
|
---|
[8419] | 145 | * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, FileFilter, int, String)
|
---|
[5456] | 146 | * @since 5456
|
---|
| 147 | */
|
---|
| 148 | public static File createAndOpenSaveFileChooser(String title, ExtensionFileFilter filter) {
|
---|
[7578] | 149 | AbstractFileChooser fc = createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, null);
|
---|
[5456] | 150 | return checkFileAndConfirmOverWrite(fc, filter.getDefaultExtension());
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
[6830] | 154 | * Creates a new "Save" dialog for a given file extension and makes it visible.<br>
|
---|
[5456] | 155 | * When the user has chosen a file, checks the file extension, and confirms overwrite if needed.
|
---|
[6069] | 156 | *
|
---|
[5456] | 157 | * @param title The dialog title
|
---|
| 158 | * @param extension The file extension
|
---|
| 159 | * @return The output {@code File}
|
---|
| 160 | * @see DiskAccessAction#createAndOpenFileChooser(boolean, boolean, String, String)
|
---|
| 161 | */
|
---|
[1949] | 162 | public static File createAndOpenSaveFileChooser(String title, String extension) {
|
---|
[7578] | 163 | AbstractFileChooser fc = createAndOpenFileChooser(false, false, title, extension);
|
---|
[5456] | 164 | return checkFileAndConfirmOverWrite(fc, extension);
|
---|
| 165 | }
|
---|
[6069] | 166 |
|
---|
[9670] | 167 | /**
|
---|
| 168 | * Checks if selected filename has the given extension. If not, adds the extension and asks for overwrite if filename exists.
|
---|
| 169 | *
|
---|
| 170 | * @param fc FileChooser where file was already selected
|
---|
[9676] | 171 | * @param extension file extension
|
---|
[9670] | 172 | * @return the {@code File} or {@code null} if the user cancelled the dialog.
|
---|
| 173 | */
|
---|
| 174 | public static File checkFileAndConfirmOverWrite(AbstractFileChooser fc, String extension) {
|
---|
[9676] | 175 | if (fc == null)
|
---|
| 176 | return null;
|
---|
[1949] | 177 | File file = fc.getSelectedFile();
|
---|
[6069] | 178 |
|
---|
[6011] | 179 | FileFilter ff = fc.getFileFilter();
|
---|
| 180 | if (!ff.accept(file)) {
|
---|
| 181 | // Extension of another filefilter given ?
|
---|
| 182 | for (FileFilter cff : fc.getChoosableFileFilters()) {
|
---|
| 183 | if (cff.accept(file)) {
|
---|
| 184 | fc.setFileFilter(cff);
|
---|
| 185 | return file;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | // No filefilter accepts current filename, add default extension
|
---|
| 189 | String fn = file.getPath();
|
---|
[9721] | 190 | if (extension != null && ff.accept(new File(fn + '.' + extension))) {
|
---|
[9466] | 191 | fn += '.' + extension;
|
---|
| 192 | } else if (ff instanceof ExtensionFileFilter) {
|
---|
[8846] | 193 | fn += '.' + ((ExtensionFileFilter) ff).getDefaultExtension();
|
---|
[1949] | 194 | }
|
---|
[3570] | 195 | file = new File(fn);
|
---|
[9687] | 196 | if (!fc.getSelectedFile().exists() && !confirmOverwrite(file))
|
---|
[5457] | 197 | return null;
|
---|
[1949] | 198 | }
|
---|
[3863] | 199 | return file;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[9676] | 202 | /**
|
---|
| 203 | * Asks user to confirm overwiting a file.
|
---|
| 204 | * @param file file to overwrite
|
---|
| 205 | * @return {@code true} if the file can be written
|
---|
| 206 | */
|
---|
[4911] | 207 | public static boolean confirmOverwrite(File file) {
|
---|
[9676] | 208 | if (file == null || file.exists()) {
|
---|
[2070] | 209 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
| 210 | Main.parent,
|
---|
| 211 | tr("Overwrite"),
|
---|
| 212 | new String[] {tr("Overwrite"), tr("Cancel")}
|
---|
| 213 | );
|
---|
| 214 | dialog.setContent(tr("File exists. Overwrite?"));
|
---|
[8061] | 215 | dialog.setButtonIcons(new String[] {"save_as", "cancel"});
|
---|
[2070] | 216 | dialog.showDialog();
|
---|
[8345] | 217 | return dialog.getValue() == 1;
|
---|
[2070] | 218 | }
|
---|
[3863] | 219 | return true;
|
---|
[1949] | 220 | }
|
---|
[4371] | 221 |
|
---|
[8802] | 222 | static void addToFileOpenHistory(File file) {
|
---|
| 223 | final String filepath;
|
---|
[4371] | 224 | try {
|
---|
| 225 | filepath = file.getCanonicalPath();
|
---|
| 226 | } catch (IOException ign) {
|
---|
[9676] | 227 | Main.warn(ign);
|
---|
[4371] | 228 | return;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | int maxsize = Math.max(0, Main.pref.getInteger("file-open.history.max-size", 15));
|
---|
| 232 | Collection<String> oldHistory = Main.pref.getCollection("file-open.history");
|
---|
[7005] | 233 | List<String> history = new LinkedList<>(oldHistory);
|
---|
[4374] | 234 | history.remove(filepath);
|
---|
[4371] | 235 | history.add(0, filepath);
|
---|
| 236 | Main.pref.putCollectionBounded("file-open.history", maxsize, history);
|
---|
| 237 | }
|
---|
[290] | 238 | }
|
---|