Changeset 6621 in josm


Ignore:
Timestamp:
2014-01-04T03:38:53+01:00 (10 years ago)
Author:
Don-vip
Message:

fix #9262 - Cancelling opening file should not result in an error message

Location:
trunk/src/org/openstreetmap/josm/io
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/FileImporter.java

    r6248 r6621  
    1818import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    1919import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
     20import org.openstreetmap.josm.gui.Notification;
    2021import org.openstreetmap.josm.gui.layer.Layer;
    2122import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     23import org.openstreetmap.josm.gui.util.GuiHelper;
    2224
    2325public abstract class FileImporter implements Comparable<FileImporter>, LayerChangeListener {
     
    6668            importData(f, progressMonitor);
    6769            return true;
     70        } catch (IllegalDataException e) {
     71            Throwable cause = e.getCause();
     72            if (cause instanceof ImportCancelException) {
     73                displayCancel(cause);
     74            } else {
     75                displayError(f, e);
     76            }
     77            return false;
    6878        } catch (Exception e) {
    69             e.printStackTrace();
    70             HelpAwareOptionPane.showMessageDialogInEDT(
    71                     Main.parent,
    72                     tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
    73                     tr("Error"),
    74                     JOptionPane.ERROR_MESSAGE, null
    75             );
     79            displayError(f, e);
    7680            return false;
    7781        }
    7882    }
     83   
     84    private static void displayError(File f, Exception e) {
     85        e.printStackTrace();
     86        HelpAwareOptionPane.showMessageDialogInEDT(
     87                Main.parent,
     88                tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
     89                tr("Error"),
     90                JOptionPane.ERROR_MESSAGE, null
     91        );
     92    }
     93   
     94    private static void displayCancel(final Throwable t) {
     95        GuiHelper.runInEDTAndWait(new Runnable() {
     96            @Override
     97            public void run() {
     98                Notification note = new Notification(t.getMessage());
     99                note.setIcon(JOptionPane.INFORMATION_MESSAGE);
     100                note.setDuration(Notification.TIME_SHORT);
     101                note.show();
     102            }
     103        });
     104    }
     105   
    79106    public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
    80107        try {
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r6316 r6621  
    8484    }
    8585
     86    protected void throwException(String msg, Throwable th) throws XMLStreamException {
     87        throw new OsmParsingException(msg, parser.getLocation(), th);
     88    }
     89
    8690    protected void throwException(String msg) throws XMLStreamException {
    8791        throw new OsmParsingException(msg, parser.getLocation());
     
    135139            if (cancel) {
    136140                cancel = false;
    137                 throwException(tr("Reading was canceled"));
     141                throw new OsmParsingCanceledException(tr("Reading was canceled"), parser.getLocation());
    138142            }
    139143
     
    300304            id = Long.parseLong(value);
    301305        } catch(NumberFormatException e) {
    302             throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(r.getUniqueId()),value));
     306            throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(r.getUniqueId()),value), e);
    303307        }
    304308        value = parser.getAttributeValue(null, "type");
     
    309313            type = OsmPrimitiveType.fromApiTypeName(value);
    310314        } catch(IllegalArgumentException e) {
    311             throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(id), Long.toString(r.getUniqueId()), value));
     315            throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(id), Long.toString(r.getUniqueId()), value), e);
    312316        }
    313317        value = parser.getAttributeValue(null, "role");
     
    404408            return User.createOsmUser(id, name);
    405409        } catch(NumberFormatException e) {
    406             throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
     410            throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid), e);
    407411        }
    408412        return null;
     
    441445                version = Integer.parseInt(versionString);
    442446            } catch(NumberFormatException e) {
    443                 throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
     447                throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString), e);
    444448            }
    445449            if (ds.getVersion().equals("0.6")){
     
    502506                } else {
    503507                    // for an existing primitive this is a problem
    504                     throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
     508                    throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v), e);
    505509                }
    506510            }
     
    526530            return Long.parseLong(value);
    527531        } catch(NumberFormatException e) {
    528             throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value));
     532            throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value), e);
    529533        }
    530534        return 0; // should not happen
     
    532536
    533537    private static class OsmParsingException extends XMLStreamException {
    534         public OsmParsingException() {
    535             super();
    536         }
    537 
    538         public OsmParsingException(String msg) {
    539             super(msg);
    540         }
    541538
    542539        public OsmParsingException(String msg, Location location) {
     
    548545            super(msg, th);
    549546            this.location = location;
    550         }
    551 
    552         public OsmParsingException(String msg, Throwable th) {
    553             super(msg, th);
    554         }
    555 
    556         public OsmParsingException(Throwable th) {
    557             super(th);
    558547        }
    559548
     
    568557            msg = msg + " " + tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber());
    569558            return msg;
     559        }
     560    }
     561
     562    /**
     563     * Exception thrown after user cancelation.
     564     */
     565    private static final class OsmParsingCanceledException extends OsmParsingException implements ImportCancelException {
     566        /**
     567         * Constructs a new {@code OsmParsingCanceledException}.
     568         * @param msg The error message
     569         * @param location The parser location
     570         */
     571        public OsmParsingCanceledException(String msg, Location location) {
     572            super(msg, location);
    570573        }
    571574    }
Note: See TracChangeset for help on using the changeset viewer.