001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.KeyEvent;
008import java.io.File;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.gui.ExtendedDialog;
012import org.openstreetmap.josm.gui.layer.GpxLayer;
013import org.openstreetmap.josm.gui.layer.Layer;
014import org.openstreetmap.josm.tools.Shortcut;
015
016/**
017 * Export the data as an OSM xml file.
018 *
019 * @author imi
020 */
021public final class SaveAction extends SaveActionBase {
022    private static SaveAction instance = new SaveAction();
023
024    /**
025     * Construct the action with "Save" as label.
026     */
027    private SaveAction() {
028        super(tr("Save"), "save", tr("Save the current data."),
029                Shortcut.registerShortcut("system:save", tr("File: {0}", tr("Save")), KeyEvent.VK_S, Shortcut.CTRL));
030        putValue("help", ht("/Action/Save"));
031    }
032
033    public static SaveAction getInstance() {
034        return instance;
035    }
036
037    @Override public File getFile(Layer layer) {
038        File f = layer.getAssociatedFile();
039        if (f != null && !f.exists()) {
040            f = null;
041        }
042
043        // Ask for overwrite in case of GpxLayer: GpxLayers usually are imports
044        // and modifying is an error most of the time.
045        if (f != null && layer instanceof GpxLayer) {
046            ExtendedDialog dialog = new ExtendedDialog(
047                    Main.parent,
048                    tr("Overwrite"),
049                    new String[] {tr("Overwrite"), tr("Cancel")}
050            );
051            dialog.setButtonIcons(new String[] {"save_as", "cancel"});
052            dialog.setContent(tr("File {0} exists. Overwrite?", f.getName()));
053            dialog.showDialog();
054            int ret = dialog.getValue();
055            if (ret != 1) {
056                f = null;
057            }
058        }
059        return f == null ? layer.createAndOpenSaveFileChooser() : f;
060    }
061}