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.gui.ExtendedDialog;
011import org.openstreetmap.josm.gui.MainApplication;
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        setHelpId(ht("/Action/Save"));
031    }
032
033    /**
034     * Returns the unique instance.
035     * @return the unique instance
036     */
037    public static SaveAction getInstance() {
038        return instance;
039    }
040
041    @Override public File getFile(Layer layer) {
042        File f = layer.getAssociatedFile();
043        if (f != null && !f.exists()) {
044            f = null;
045        }
046
047        // Ask for overwrite in case of GpxLayer: GpxLayers usually are imports
048        // and modifying is an error most of the time.
049        if (f != null && layer instanceof GpxLayer) {
050            ExtendedDialog dialog = new ExtendedDialog(
051                    MainApplication.getMainFrame(),
052                    tr("Overwrite"),
053                    tr("Overwrite"), tr("Cancel"))
054                .setButtonIcons("save_as", "cancel")
055                .setContent(tr("File {0} exists. Overwrite?", f.getName()));
056            if (dialog.showDialog().getValue() != 1) {
057                f = null;
058            }
059        }
060        return f == null ? layer.createAndOpenSaveFileChooser() : f;
061    }
062}