| 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.gui.HelpAwareOptionPane;
|
|---|
| 17 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 18 | import org.openstreetmap.josm.gui.io.importexport.ImageImporter;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 20 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|---|
| 21 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
|---|
| 22 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
|---|
| 23 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 24 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 25 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * An action that imports images along a GPX path
|
|---|
| 29 | */
|
|---|
| 30 | public class ImportImagesAction extends AbstractAction {
|
|---|
| 31 | private final transient GpxLayer layer;
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Create a new {@link ImportImagesAction}
|
|---|
| 35 | * @param layer The layer this action should be for
|
|---|
| 36 | */
|
|---|
| 37 | public ImportImagesAction(final GpxLayer layer) {
|
|---|
| 38 | super(tr("Import images"));
|
|---|
| 39 | new ImageProvider("dialogs/geoimage").getResource().attachImageIcon(this, true);
|
|---|
| 40 | this.layer = layer;
|
|---|
| 41 | putValue("help", ht("/Action/ImportImages"));
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | private static void warnCantImportIntoServerLayer(GpxLayer layer) {
|
|---|
| 45 | String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>"+
|
|---|
| 46 | "Because its way points do not include a timestamp we cannot correlate them with images.</html>",
|
|---|
| 47 | Utils.escapeReservedCharactersHTML(layer.getName()));
|
|---|
| 48 | HelpAwareOptionPane.showOptionDialog(MainApplication.getMainFrame(), msg, tr("Import not possible"),
|
|---|
| 49 | JOptionPane.WARNING_MESSAGE, ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer"));
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private static void addRecursiveFiles(List<File> files, File... sel) {
|
|---|
| 53 | if (sel == null) { // listFiles might return null
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 | for (File f : sel) {
|
|---|
| 57 | if (f.isDirectory()) {
|
|---|
| 58 | addRecursiveFiles(files, f.listFiles());
|
|---|
| 59 | } else if (Utils.hasExtension(f, "jpg")) {
|
|---|
| 60 | files.add(f);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | @Override
|
|---|
| 66 | public void actionPerformed(ActionEvent e) {
|
|---|
| 67 | if (layer.data.fromServer) {
|
|---|
| 68 | warnCantImportIntoServerLayer(layer);
|
|---|
| 69 | return;
|
|---|
| 70 | }
|
|---|
| 71 | ImageImporter importer = new ImageImporter(layer);
|
|---|
| 72 | AbstractFileChooser fc = new FileChooserManager(true, "geoimage.lastdirectory", Config.getPref().get("lastDirectory")).
|
|---|
| 73 | createFileChooser(true, null, importer.filter, JFileChooser.FILES_AND_DIRECTORIES).openFileChooser();
|
|---|
| 74 | if (fc != null) {
|
|---|
| 75 | File[] sel = fc.getSelectedFiles();
|
|---|
| 76 | if (sel != null && sel.length > 0) {
|
|---|
| 77 | List<File> files = new LinkedList<>();
|
|---|
| 78 | addRecursiveFiles(files, sel);
|
|---|
| 79 | importer.importDataHandleExceptions(files, NullProgressMonitor.INSTANCE);
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | }
|
|---|