Changeset 4695 in josm for trunk


Ignore:
Timestamp:
2011-12-22T11:49:23+01:00 (12 years ago)
Author:
bastiK
Message:

session: GpxImporter refactoring

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java

    r4668 r4695  
    127127                cancel();
    128128                throw e;
    129             } catch (Throwable t) {
     129            } catch (Error e) {
    130130                cancel();
    131                 throw new RuntimeException(t);
     131                throw e;
    132132            }
    133133        }
  • trunk/src/org/openstreetmap/josm/gui/util/GuiHelper.java

    r3707 r4695  
    44import java.awt.Component;
    55import java.awt.Container;
     6
     7import javax.swing.SwingUtilities;
    68
    79/**
     
    2426    }
    2527
     28    public static void runInEDT(Runnable task) {
     29        if (SwingUtilities.isEventDispatchThread()) {
     30            task.run();
     31        } else {
     32            SwingUtilities.invokeLater(task);
     33        }
     34    }
     35
    2636}
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r4310 r4695  
    1919import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    2020import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     21import org.openstreetmap.josm.gui.util.GuiHelper;
    2122import org.xml.sax.SAXException;
    2223
    2324public class GpxImporter extends FileImporter {
     25
     26    protected GpxLayer gpxLayer;
     27    protected MarkerLayer markerLayer;
     28    protected Runnable postLayerTask;
    2429
    2530    public GpxImporter() {
     
    2732    }
    2833
    29     @Override public void importData(final File file, ProgressMonitor progressMonitor) throws IOException {
    30         final String fn = file.getName();
     34    @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
     35        InputStream is;
     36        if (file.getName().endsWith(".gpx.gz")) {
     37            is = new GZIPInputStream(new FileInputStream(file));
     38        } else {
     39            is = new FileInputStream(file);
     40        }
     41        String fileName = file.getName();
     42        loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor);
    3143
     44        // FIXME: remove UI stuff from the IO subsystem
     45        GuiHelper.runInEDT(new Runnable() {
     46            public void run() {
     47                if (markerLayer != null) {
     48                    Main.main.addLayer(markerLayer);
     49                }
     50                if (gpxLayer != null) {
     51                    Main.main.addLayer(gpxLayer);
     52                }
     53                postLayerTask.run();
     54            }
     55        });
     56    }
     57
     58    public void loadLayers(InputStream is, final File associatedFile,
     59                    final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
    3260        try {
    33             InputStream is;
    34             if (file.getName().endsWith(".gpx.gz")) {
    35                 is = new GZIPInputStream(new FileInputStream(file));
    36             } else {
    37                 is = new FileInputStream(file);
    38             }
    3961            final GpxReader r = new GpxReader(is);
    4062            final boolean parsedProperly = r.parse(true);
    41             r.data.storageFile = file;
    42             final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
    43 
    44             // FIXME: remove UI stuff from the IO subsystem
    45             //
    46             Runnable task = new Runnable() {
     63            r.data.storageFile = associatedFile;
     64            if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
     65                gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
     66            }
     67            if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
     68                markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, gpxLayer);
     69                if (markerLayer.data.size() == 0) {
     70                    markerLayer = null;
     71                }
     72            }
     73            postLayerTask = new Runnable() {
     74                @Override
    4775                public void run() {
    48                     if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
    49                         Main.main.addLayer(gpxLayer);
    50                     }
    51                     if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
    52                         MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
    53                         if (ml.data.size() > 0) {
    54                             Main.main.addLayer(ml);
     76                    if (!parsedProperly) {
     77                        String msg;
     78                        if (associatedFile == null) {
     79                            msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
     80                                    gpxLayerName);
     81                        } else {
     82                            msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
     83                                    associatedFile.getPath());
    5584                        }
    56                     }
    57                     if (!parsedProperly) {
    58                         JOptionPane.showMessageDialog(null, tr("Error occurred while parsing gpx file {0}. Only a part of the file will be available.", file.getName()));
     85                        JOptionPane.showMessageDialog(null, msg);
    5986                    }
    6087                }
    6188            };
    62             if (SwingUtilities.isEventDispatchThread()) {
    63                 task.run();
    64             } else {
    65                 SwingUtilities.invokeLater(task);
    66             }
    67         } catch (FileNotFoundException e) {
    68             e.printStackTrace();
    69             throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
    7089        } catch (SAXException e) {
    7190            e.printStackTrace();
    72             throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
     91            throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
    7392        }
    7493    }
     94
     95    public GpxLayer getGpxLayer() {
     96        return gpxLayer;
     97    }
     98
     99    public MarkerLayer getMarkerLayer() {
     100        return markerLayer;
     101    }
     102
     103    public Runnable getPostLayerTask() {
     104        return postLayerTask;
     105    }
    75106}
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r4687 r4695  
    1919import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2020import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     21import org.openstreetmap.josm.gui.util.GuiHelper;
    2122
    2223public class OsmImporter extends FileImporter {
    2324
    24     private OsmDataLayer layer;
    25     private Runnable postLayerTask;
     25    protected OsmDataLayer layer;
     26    protected Runnable postLayerTask;
    2627
    2728    public OsmImporter() {
     
    4748        loadLayer(in, associatedFile, associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), NullProgressMonitor.INSTANCE);
    4849        // FIXME: remove UI stuff from IO subsystem
    49         Runnable uiStuff = new Runnable() {
     50        GuiHelper.runInEDT(new Runnable() {
    5051            @Override
    5152            public void run() {
     
    5455                layer.onPostLoadFromFile();
    5556            }
    56         };
    57         if (SwingUtilities.isEventDispatchThread()) {
    58             uiStuff.run();
    59         } else {
    60             SwingUtilities.invokeLater(uiStuff);
    61         }
     57        });
    6258    }
    6359
Note: See TracChangeset for help on using the changeset viewer.