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 | /**
|
---|
20 | * File importer allowing to import geottaged images (*.jpg files).
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class JpgImporter extends FileImporter {
|
---|
24 | private GpxLayer gpx;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The default file filter (only *.jpg files).
|
---|
28 | */
|
---|
29 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
30 | "jpg,jpeg", "jpg", tr("Image Files") + " (*.jpg)");
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * An alternate file filter that also includes folders.
|
---|
34 | * @since 5438
|
---|
35 | */
|
---|
36 | public static final ExtensionFileFilter FILE_FILTER_WITH_FOLDERS = new ExtensionFileFilter(
|
---|
37 | "jpg,jpeg", "jpg", tr("Image Files") + " (*.jpg, "+ tr("folder")+")");
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new {@code JpgImporter}.
|
---|
41 | */
|
---|
42 | public JpgImporter() {
|
---|
43 | this(false);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new {@code JpgImporter} with folders selection, if wanted.
|
---|
48 | * @param includeFolders If true, includes folders in the file filter
|
---|
49 | * @since 5438
|
---|
50 | */
|
---|
51 | public JpgImporter(boolean includeFolders) {
|
---|
52 | super(includeFolders ? FILE_FILTER_WITH_FOLDERS : FILE_FILTER);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Constructs a new {@code JpgImporter} for the given GPX layer. Folders selection is allowed.
|
---|
57 | * @param gpx The GPX layer
|
---|
58 | */
|
---|
59 | public JpgImporter(GpxLayer gpx) {
|
---|
60 | this(true);
|
---|
61 | this.gpx = gpx;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public boolean acceptFile(File pathname) {
|
---|
66 | return super.acceptFile(pathname) || pathname.isDirectory();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void importData(List<File> sel, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
71 | progressMonitor.beginTask(tr("Looking for image files"), 1);
|
---|
72 | try {
|
---|
73 | List<File> files = new ArrayList<>();
|
---|
74 | Set<String> visitedDirs = new HashSet<>();
|
---|
75 | addRecursiveFiles(files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true));
|
---|
76 |
|
---|
77 | if (progressMonitor.isCanceled())
|
---|
78 | return;
|
---|
79 |
|
---|
80 | if (files.isEmpty())
|
---|
81 | throw new IOException(tr("No image files found."));
|
---|
82 |
|
---|
83 | GeoImageLayer.create(files, gpx);
|
---|
84 | } finally {
|
---|
85 | progressMonitor.finishTask();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void addRecursiveFiles(List<File> files, Set<String> visitedDirs, List<File> sel, ProgressMonitor progressMonitor) throws IOException {
|
---|
90 |
|
---|
91 | if (progressMonitor.isCanceled())
|
---|
92 | return;
|
---|
93 |
|
---|
94 | progressMonitor.beginTask(null, sel.size());
|
---|
95 | try {
|
---|
96 | for (File f : sel) {
|
---|
97 | if (f.isDirectory()) {
|
---|
98 | if (visitedDirs.add(f.getCanonicalPath())) { // Do not loop over symlinks
|
---|
99 | File[] dirFiles = f.listFiles(); // Can be null for some strange directories (like lost+found)
|
---|
100 | if (dirFiles != null) {
|
---|
101 | addRecursiveFiles(files, visitedDirs, Arrays.asList(dirFiles), progressMonitor.createSubTaskMonitor(1, true));
|
---|
102 | }
|
---|
103 | } else {
|
---|
104 | progressMonitor.worked(1);
|
---|
105 | }
|
---|
106 | } else {
|
---|
107 | if (f.getName().toLowerCase().endsWith(".jpg")) {
|
---|
108 | files.add(f);
|
---|
109 | }
|
---|
110 | progressMonitor.worked(1);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | } finally {
|
---|
114 | progressMonitor.finishTask();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public boolean isBatchImporter() {
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Needs to be the last, to avoid problems.
|
---|
125 | */
|
---|
126 | @Override
|
---|
127 | public double getPriority() {
|
---|
128 | return -1000;
|
---|
129 | }
|
---|
130 | }
|
---|