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