1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
15 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
|
---|
17 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
18 |
|
---|
19 | public class JpgImporter extends FileImporter {
|
---|
20 | private GpxLayer gpx;
|
---|
21 |
|
---|
22 | public JpgImporter() {
|
---|
23 | super(new ExtensionFileFilter("jpg", "jpg", tr("Image Files") + " (*.jpg, "+ tr("folder")+")"));
|
---|
24 | }
|
---|
25 |
|
---|
26 | public JpgImporter(GpxLayer gpx) {
|
---|
27 | this();
|
---|
28 | this.gpx = gpx;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public boolean acceptFile(File pathname) {
|
---|
33 | return super.acceptFile(pathname) || pathname.isDirectory();
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
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 | }
|
---|
54 | }
|
---|
55 |
|
---|
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 | }
|
---|
79 | }
|
---|
80 | } finally {
|
---|
81 | progressMonitor.finishTask();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public boolean isBatchImporter() {
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Needs to be the last, to avoid problems.
|
---|
92 | */
|
---|
93 | @Override
|
---|
94 | public double getPriority() {
|
---|
95 | return -1000;
|
---|
96 | }
|
---|
97 | }
|
---|