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