1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.io.File;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.JFileChooser;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
18 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
19 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
---|
22 | import org.openstreetmap.josm.io.JpgImporter;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 |
|
---|
26 | public class ImportImagesAction extends AbstractAction {
|
---|
27 | private final transient GpxLayer layer;
|
---|
28 |
|
---|
29 | public ImportImagesAction(final GpxLayer layer) {
|
---|
30 | super(tr("Import images"), ImageProvider.get("dialogs/geoimage"));
|
---|
31 | this.layer = layer;
|
---|
32 | putValue("help", ht("/Action/ImportImages"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static void warnCantImportIntoServerLayer(GpxLayer layer) {
|
---|
36 | String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>"+
|
---|
37 | "Because its way points do not include a timestamp we cannot correlate them with images.</html>",
|
---|
38 | layer.getName());
|
---|
39 | HelpAwareOptionPane.showOptionDialog(Main.parent, msg, tr("Import not possible"),
|
---|
40 | JOptionPane.WARNING_MESSAGE, ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer"));
|
---|
41 | }
|
---|
42 |
|
---|
43 | private static void addRecursiveFiles(List<File> files, File[] sel) {
|
---|
44 | if (sel == null) { // listFiles might return null
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | for (File f : sel) {
|
---|
48 | if (f.isDirectory()) {
|
---|
49 | addRecursiveFiles(files, f.listFiles());
|
---|
50 | } else if (Utils.hasExtension(f, "jpg")) {
|
---|
51 | files.add(f);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void actionPerformed(ActionEvent e) {
|
---|
58 | if (layer.data.fromServer) {
|
---|
59 | warnCantImportIntoServerLayer(layer);
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | JpgImporter importer = new JpgImporter(layer);
|
---|
63 | AbstractFileChooser fc = new FileChooserManager(true, "geoimage.lastdirectory", Main.pref.get("lastDirectory")).
|
---|
64 | createFileChooser(true, null, importer.filter, JFileChooser.FILES_AND_DIRECTORIES).openFileChooser();
|
---|
65 | if (fc != null) {
|
---|
66 | File[] sel = fc.getSelectedFiles();
|
---|
67 | if (sel != null && sel.length > 0) {
|
---|
68 | List<File> files = new LinkedList<>();
|
---|
69 | addRecursiveFiles(files, sel);
|
---|
70 | importer.importDataHandleExceptions(files, NullProgressMonitor.INSTANCE);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|