| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.actions.SaveActionBase.createAndOpenSaveFileChooser;
|
|---|
| 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 7 |
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.io.File;
|
|---|
| 11 | import java.io.IOException;
|
|---|
| 12 | import java.nio.file.InvalidPathException;
|
|---|
| 13 | import java.text.MessageFormat;
|
|---|
| 14 |
|
|---|
| 15 | import javax.swing.JOptionPane;
|
|---|
| 16 |
|
|---|
| 17 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 18 | import org.openstreetmap.josm.gui.io.importexport.FileExporter;
|
|---|
| 19 | import org.openstreetmap.josm.gui.io.importexport.GpxExporter;
|
|---|
| 20 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Exports data to gpx.
|
|---|
| 27 | * @since 78
|
|---|
| 28 | */
|
|---|
| 29 | public class GpxExportAction extends DiskAccessAction {
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Constructs a new {@code GpxExportAction}.
|
|---|
| 33 | */
|
|---|
| 34 | public GpxExportAction() {
|
|---|
| 35 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."),
|
|---|
| 36 | Shortcut.registerShortcut("file:exportgpx", tr("File: {0}", tr("Export to GPX...")), KeyEvent.VK_E, Shortcut.CTRL));
|
|---|
| 37 | setHelpId(ht("/Action/GpxExport"));
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Deferring constructor for child classes.
|
|---|
| 42 | *
|
|---|
| 43 | * @param name see {@code DiskAccessAction}
|
|---|
| 44 | * @param iconName see {@code DiskAccessAction}
|
|---|
| 45 | * @param tooltip see {@code DiskAccessAction}
|
|---|
| 46 | * @param shortcut see {@code DiskAccessAction}
|
|---|
| 47 | * @param register see {@code DiskAccessAction}
|
|---|
| 48 | * @param toolbarId see {@code DiskAccessAction}
|
|---|
| 49 | * @param installAdapters see {@code DiskAccessAction}
|
|---|
| 50 | *
|
|---|
| 51 | * @since 13210
|
|---|
| 52 | */
|
|---|
| 53 | protected GpxExportAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
|---|
| 54 | boolean register, String toolbarId, boolean installAdapters) {
|
|---|
| 55 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Get the layer to export.
|
|---|
| 60 | * @return The layer to export, if supported, otherwise {@code null}.
|
|---|
| 61 | */
|
|---|
| 62 | protected Layer getLayer() {
|
|---|
| 63 | Layer layer = getLayerManager().getActiveLayer();
|
|---|
| 64 | return GpxExporter.isSupportedLayer(layer) ? layer : null;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @Override
|
|---|
| 68 | public void actionPerformed(ActionEvent e) {
|
|---|
| 69 | if (!isEnabled())
|
|---|
| 70 | return;
|
|---|
| 71 | Layer layer = getLayer();
|
|---|
| 72 | if (layer == null) {
|
|---|
| 73 | JOptionPane.showMessageDialog(
|
|---|
| 74 | MainApplication.getMainFrame(),
|
|---|
| 75 | tr("Nothing to export. Get some data first."),
|
|---|
| 76 | tr("Information"),
|
|---|
| 77 | JOptionPane.INFORMATION_MESSAGE
|
|---|
| 78 | );
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 | export(layer);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Exports a layer to a file. Launches a file chooser to request the user to enter a file name.
|
|---|
| 86 | *
|
|---|
| 87 | * <code>layer</code> must not be null. <code>layer</code> must be of a supported type.
|
|---|
| 88 | *
|
|---|
| 89 | * @param layer the layer
|
|---|
| 90 | * @throws IllegalArgumentException if layer is null
|
|---|
| 91 | * @throws IllegalArgumentException if layer is not of a supported type.
|
|---|
| 92 | * @see GpxExporter#isSupportedLayer
|
|---|
| 93 | */
|
|---|
| 94 | public void export(Layer layer) {
|
|---|
| 95 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
|---|
| 96 | if (!GpxExporter.isSupportedLayer(layer))
|
|---|
| 97 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of {0}. Got ''{1}''.",
|
|---|
| 98 | GpxExporter.getSupportedLayers(), layer.getClass().getName()));
|
|---|
| 99 |
|
|---|
| 100 | File file = createAndOpenSaveFileChooser(tr("Export GPX file"), GpxImporter.getFileFilter());
|
|---|
| 101 | if (file == null)
|
|---|
| 102 | return;
|
|---|
| 103 |
|
|---|
| 104 | for (FileExporter exporter : ExtensionFileFilter.getExporters()) {
|
|---|
| 105 | if (exporter.acceptFile(file, layer)) {
|
|---|
| 106 | try {
|
|---|
| 107 | exporter.exportData(file, layer);
|
|---|
| 108 | } catch (IOException | InvalidPathException e) {
|
|---|
| 109 | SaveActionBase.showAndLogException(e);
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | @Override
|
|---|
| 116 | protected boolean listenToSelectionChange() {
|
|---|
| 117 | return false;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Refreshes the enabled state
|
|---|
| 122 | */
|
|---|
| 123 | @Override
|
|---|
| 124 | protected void updateEnabledState() {
|
|---|
| 125 | setEnabled(getLayer() != null);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|