Ignore:
Timestamp:
2010-01-13T20:20:07+01:00 (12 years ago)
Author:
jttt
Message:

Added progress monitor to JpgImporter, allow to cancel importing

Location:
trunk/src/org/openstreetmap/josm/io
Files:
8 edited

Legend:

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

    r2719 r2851  
    1212import org.openstreetmap.josm.Main;
    1313import org.openstreetmap.josm.actions.ExtensionFileFilter;
     14import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1415
    1516public abstract class FileImporter implements Comparable<FileImporter> {
    1617
    17     public ExtensionFileFilter filter;
     18    public final ExtensionFileFilter filter;
    1819
    1920    public FileImporter(ExtensionFileFilter filter) {
     
    3435    /**
    3536     * Needs to be implemented if isBatchImporter() returns false.
     37     * @throws IllegalDataException
    3638     */
    37     public void importData(File file) throws IOException, IllegalDataException {
     39    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    3840        throw new IOException(tr("Could not import ''{0}''.", file.getName()));
    3941    }
     
    4143    /**
    4244     * Needs to be implemented if isBatchImporter() returns true.
     45     * @throws IllegalDataException
    4346     */
    44     public void importData(List<File> files) throws IOException, IllegalDataException {
     47    public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    4548        throw new IOException(tr("Could not import files."));
    4649    }
     
    4952     * Wrapper to give meaningful output if things go wrong.
    5053     */
    51     public void importDataHandleExceptions(File f) {
     54    public void importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
    5255        try {
    5356            System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
    54             importData(f);
     57            importData(f, progressMonitor);
    5558        } catch (Exception e) {
    5659            e.printStackTrace();
     
    6366        }
    6467    }
    65     public void importDataHandleExceptions(List<File> files) {
     68    public void importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
    6669        try {
    6770            System.out.println("Open "+files.size()+" files");
    68             importData(files);
     71            importData(files, progressMonitor);
    6972        } catch (Exception e) {
    7073            e.printStackTrace();
     
    9093        return (new Double(this.getPriority())).compareTo(other.getPriority());
    9194    }
     95
    9296}
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r2795 r2851  
    1818import org.openstreetmap.josm.gui.layer.GpxLayer;
    1919import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
     20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2021import org.xml.sax.SAXException;
    2122
     
    2627    }
    2728
    28     @Override public void importData(final File file) throws IOException {
     29    @Override public void importData(final File file, ProgressMonitor progressMonitor) throws IOException {
    2930        final String fn = file.getName();
    3031
  • trunk/src/org/openstreetmap/josm/io/IllegalDataException.java

    r2512 r2851  
    66    public IllegalDataException() {
    77        super();
    8         // TODO Auto-generated constructor stub
    98    }
    109
    1110    public IllegalDataException(String message, Throwable cause) {
    1211        super(message, cause);
    13         // TODO Auto-generated constructor stub
    1412    }
    1513
    1614    public IllegalDataException(String message) {
    1715        super(message);
    18         // TODO Auto-generated constructor stub
    1916    }
    2017
    2118    public IllegalDataException(Throwable cause) {
    2219        super(cause);
    23         // TODO Auto-generated constructor stub
    2420    }
    2521
  • trunk/src/org/openstreetmap/josm/io/JpgImporter.java

    r2799 r2851  
    66import java.io.File;
    77import java.io.IOException;
     8import java.util.ArrayList;
    89import java.util.Arrays;
    9 import java.util.LinkedList;
     10import java.util.HashSet;
    1011import java.util.List;
     12import java.util.Set;
    1113
    1214import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1315import org.openstreetmap.josm.gui.layer.GpxLayer;
    1416import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
     17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1518
    1619public class JpgImporter extends FileImporter {
     
    3235
    3336    @Override
    34     public void importData(List<File> sel) throws IOException, IllegalDataException {
    35         if (sel == null || sel.size() == 0)
    36             return;
    37         LinkedList<File> files = new LinkedList<File>();
    38         addRecursiveFiles(files, sel);
    39         if(files.isEmpty()) throw new IOException(tr("No image files found."));
    40         GeoImageLayer.create(files, gpx);
     37    public void importData(List<File> sel, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
     38        progressMonitor.beginTask(tr("Looking for image files"), 1);
     39        try {
     40            List<File> files = new ArrayList<File>();
     41            Set<String> visitedDirs = new HashSet<String>();
     42            addRecursiveFiles(files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true));
     43
     44            if (progressMonitor.isCancelled())
     45                return;
     46
     47            if (files.isEmpty())
     48                throw new IOException(tr("No image files found."));
     49
     50            GeoImageLayer.create(files, gpx);
     51        } finally {
     52            progressMonitor.finishTask();
     53        }
    4154    }
    4255
    43     private void addRecursiveFiles(LinkedList<File> files, List<File> sel) {
    44         for (File f : sel) {
    45             if (f.isDirectory()) {
    46                 addRecursiveFiles(files, Arrays.asList(f.listFiles()));
    47             } else if (f.getName().toLowerCase().endsWith(".jpg")) {
    48                 files.add(f);
     56    private void addRecursiveFiles(List<File> files, Set<String> visitedDirs, List<File> sel, ProgressMonitor progressMonitor) throws IOException {
     57
     58        if (progressMonitor.isCancelled())
     59            return;
     60
     61        progressMonitor.beginTask(null, sel.size());
     62        try {
     63            for (File f : sel) {
     64                if (f.isDirectory()) {
     65                    if (visitedDirs.add(f.getCanonicalPath())) { // Do not loop over symlinks
     66                        File[] dirFiles = f.listFiles(); // Can be null for some strange directories (like lost+found)
     67                        if (dirFiles != null) {
     68                            addRecursiveFiles(files, visitedDirs, Arrays.asList(dirFiles), progressMonitor.createSubTaskMonitor(1, true));
     69                        }
     70                    } else {
     71                        progressMonitor.worked(1);
     72                    }
     73                } else {
     74                    if (f.getName().toLowerCase().endsWith(".jpg")) {
     75                        files.add(f);
     76                    }
     77                    progressMonitor.worked(1);
     78                }
    4979            }
     80        } finally {
     81            progressMonitor.finishTask();
    5082        }
    5183    }
  • trunk/src/org/openstreetmap/josm/io/NMEAImporter.java

    r2290 r2851  
    1414import org.openstreetmap.josm.gui.layer.GpxLayer;
    1515import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
     16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1617
    1718public class NMEAImporter extends FileImporter {
     
    2324    }
    2425
    25     @Override public void importData(File file) throws IOException {
     26    @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
    2627        String fn = file.getName();
    2728        NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
  • trunk/src/org/openstreetmap/josm/io/OsmBzip2Importer.java

    r2070 r2851  
    1111import org.apache.tools.bzip2.CBZip2InputStream;
    1212import org.openstreetmap.josm.actions.ExtensionFileFilter;
     13import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1314
    1415public class OsmBzip2Importer extends OsmImporter {
     
    2021
    2122    @Override
    22     public void importData(File file) throws IOException, IllegalDataException {
     23    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    2324        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    2425        int b = bis.read();
  • trunk/src/org/openstreetmap/josm/io/OsmGzipImporter.java

    r2070 r2851  
    1010
    1111import org.openstreetmap.josm.actions.ExtensionFileFilter;
     12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1213
    1314public class OsmGzipImporter extends OsmImporter {
     
    1819
    1920    @Override
    20     public void importData(File file) throws IOException, IllegalDataException {
     21    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    2122        GZIPInputStream in = new GZIPInputStream(new FileInputStream(file));
    2223        importData(in, file);
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r2181 r2851  
    1717import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1818import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1920
    2021public class OsmImporter extends FileImporter {
     
    2829    }
    2930
    30     @Override public void importData(File file) throws IOException, IllegalDataException {
     31    @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    3132        try {
    3233            FileInputStream in = new FileInputStream(file);
Note: See TracChangeset for help on using the changeset viewer.