Ticket #17238: 17238-v3.patch

File 17238-v3.patch, 7.6 KB (added by GerdP, 5 years ago)
  • src/org/openstreetmap/josm/actions/GpxExportAction.java

     
    99import java.awt.event.KeyEvent;
    1010import java.io.File;
    1111import java.io.IOException;
     12import java.nio.file.InvalidPathException;
    1213import java.text.MessageFormat;
    1314
    1415import javax.swing.JOptionPane;
     
    2021import org.openstreetmap.josm.gui.layer.Layer;
    2122import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2223import org.openstreetmap.josm.tools.CheckParameterUtil;
    23 import org.openstreetmap.josm.tools.Logging;
    2424import org.openstreetmap.josm.tools.Shortcut;
    2525
    2626/**
     
    107107            if (exporter.acceptFile(file, layer)) {
    108108                try {
    109109                    exporter.exportData(file, layer);
    110                 } catch (IOException e) {
    111                     Logging.error(e);
     110                } catch (IOException | InvalidPathException e) {
     111                    SaveActionBase.showAndLogException(e);
    112112                }
    113113            }
    114114        }
  • src/org/openstreetmap/josm/actions/SaveActionBase.java

     
    66import java.awt.event.ActionEvent;
    77import java.io.File;
    88import java.io.IOException;
     9import java.nio.file.InvalidPathException;
    910import java.util.Collection;
    1011import java.util.LinkedList;
    1112import java.util.List;
     
    2526import org.openstreetmap.josm.spi.preferences.Config;
    2627import org.openstreetmap.josm.tools.Logging;
    2728import org.openstreetmap.josm.tools.Shortcut;
     29import org.openstreetmap.josm.tools.Utils;
    2830
    2931/**
    3032 * Abstract superclass of save actions.
     
    120122                ((OsmDataLayer) layer).onPostSaveToFile();
    121123            }
    122124            MainApplication.getMainFrame().repaint();
    123         } catch (IOException e) {
    124             Logging.error(e);
     125        } catch (IOException | InvalidPathException e) {
     126            showAndLogException(e);
    125127            return false;
    126128        }
    127129        addToFileOpenHistory(file);
     
    239241        history.add(0, filepath);
    240242        PreferencesUtils.putListBounded(Config.getPref(), "file-open.history", maxsize, history);
    241243    }
     244
     245    static void showAndLogException(Exception e) {
     246        GuiHelper.runInEDT(() ->
     247        JOptionPane.showMessageDialog(
     248                MainApplication.getMainFrame(),
     249                tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>",
     250                        Utils.escapeReservedCharactersHTML(e.getClass().getSimpleName() + " - " + e.getMessage())),
     251                tr("Error"),
     252                JOptionPane.ERROR_MESSAGE
     253                ));
     254
     255        Logging.error(e);
     256    }
    242257}
  • src/org/openstreetmap/josm/gui/io/importexport/FileExporter.java

     
    1616 */
    1717public abstract class FileExporter implements ActiveLayerChangeListener {
    1818
     19    /** the  ExtensionFileFilter filter used by this exporter */
    1920    public final ExtensionFileFilter filter;
    2021
    2122    private boolean enabled;
  • src/org/openstreetmap/josm/gui/io/importexport/GpxExporter.java

     
    1010import java.io.File;
    1111import java.io.IOException;
    1212import java.io.OutputStream;
    13 import java.nio.file.InvalidPathException;
    1413import java.text.MessageFormat;
    1514import java.time.Year;
    1615import java.util.Optional;
     
    3837import org.openstreetmap.josm.spi.preferences.Config;
    3938import org.openstreetmap.josm.tools.CheckParameterUtil;
    4039import org.openstreetmap.josm.tools.GBC;
    41 import org.openstreetmap.josm.tools.Logging;
    4240
    4341/**
    4442 * Exports data to a .gpx file. Data may be native GPX or OSM data which will be converted.
     
    205203            gpxData.put(META_KEYWORDS, keywords.getText());
    206204        }
    207205
    208         try (OutputStream fo = Compression.getCompressedFileOutputStream(file)) {
    209             new GpxWriter(fo).write(gpxData);
    210             fo.flush();
    211         } catch (IOException | InvalidPathException ex) {
    212             Logging.error(ex);
    213             JOptionPane.showMessageDialog(MainApplication.getMainFrame(), tr("Error while exporting {0}:\n{1}", fn, ex.getMessage()),
    214                     tr("Error"), JOptionPane.ERROR_MESSAGE);
     206        try (OutputStream fo = Compression.getCompressedFileOutputStream(file); GpxWriter writer = new GpxWriter(fo)) {
     207            writer.write(gpxData);
    215208        }
    216209    }
    217210
  • src/org/openstreetmap/josm/gui/io/importexport/OsmExporter.java

     
    6666     * @param layer Data layer. Must be an instance of {@link OsmDataLayer}.
    6767     * @param noBackup if {@code true}, the potential backup file created if the output file already exists will be deleted
    6868     *                 after a successful export
     69     * @throws IOException in case of IO errors
     70     * @throws InvalidPathException when file name cannot be converted into a Path
    6971     * @throws IllegalArgumentException if {@code layer} is not an instance of {@code OsmDataLayer}
    7072     */
    71     public void exportData(File file, Layer layer, boolean noBackup) {
     73    public void exportData(File file, Layer layer, boolean noBackup) throws IOException {
    7274        if (!(layer instanceof OsmDataLayer)) {
    7375            throw new IllegalArgumentException(
    7476                    MessageFormat.format("Expected instance of OsmDataLayer. Got ''{0}''.", layer.getClass().getName()));
     
    8082        return Compression.getCompressedFileOutputStream(file);
    8183    }
    8284
    83     private void save(File file, OsmDataLayer layer, boolean noBackup) {
     85    private void save(File file, OsmDataLayer layer, boolean noBackup) throws IOException {
    8486        File tmpFile = null;
    8587        try {
     88
    8689            // use a tmp file because if something errors out in the process of writing the file,
    8790            // we might just end up with a truncated file.  That can destroy lots of work.
    8891            if (file.exists()) {
     
    97100            layer.onPostSaveToFile();
    98101        } catch (IOException | InvalidPathException e) {
    99102            Logging.error(e);
    100             JOptionPane.showMessageDialog(
    101                     MainApplication.getMainFrame(),
    102                     tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>",
    103                             Utils.escapeReservedCharactersHTML(e.getClass().getSimpleName() + " - " + e.getMessage())),
    104                     tr("Error"),
    105                     JOptionPane.ERROR_MESSAGE
    106             );
    107103
    108104            try {
    109105                // if the file save failed, then the tempfile will not be deleted. So, restore the backup if we made one.
     
    120116                        JOptionPane.ERROR_MESSAGE
    121117                );
    122118            }
     119            // re-throw original error
     120            throw e;
    123121        }
    124122    }
    125123