[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[283] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[1949] | 4 | import static org.openstreetmap.josm.actions.SaveActionBase.createAndOpenSaveFileChooser;
|
---|
[2323] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[283] | 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;
|
---|
[2842] | 12 | import java.text.MessageFormat;
|
---|
[283] | 13 |
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
[582] | 17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
[283] | 18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[444] | 19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[1949] | 20 | import org.openstreetmap.josm.io.FileExporter;
|
---|
[5456] | 21 | import org.openstreetmap.josm.io.GpxImporter;
|
---|
[2842] | 22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1084] | 23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[283] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Exports data to gpx.
|
---|
[7414] | 27 | * @since 78
|
---|
[283] | 28 | */
|
---|
[1820] | 29 | public class GpxExportAction extends DiskAccessAction {
|
---|
[283] | 30 |
|
---|
[7414] | 31 | /**
|
---|
| 32 | * Constructs a new {@code GpxExportAction}.
|
---|
| 33 | */
|
---|
[1808] | 34 | public GpxExportAction() {
|
---|
[1212] | 35 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."),
|
---|
[4982] | 36 | Shortcut.registerShortcut("file:exportgpx", tr("Export to GPX..."), KeyEvent.VK_E, Shortcut.CTRL));
|
---|
[2323] | 37 | putValue("help", ht("/Action/GpxExport"));
|
---|
[1169] | 38 | }
|
---|
[283] | 39 |
|
---|
[5477] | 40 | /**
|
---|
| 41 | * Get the layer to export.
|
---|
| 42 | * @return The layer to export, either a {@link GpxLayer} or {@link OsmDataLayer}.
|
---|
| 43 | */
|
---|
| 44 | protected Layer getLayer() {
|
---|
[8510] | 45 | if (!Main.isDisplayingMapView())
|
---|
[5477] | 46 | return null;
|
---|
[1808] | 47 | Layer layer = Main.map.mapView.getActiveLayer();
|
---|
[5477] | 48 | return (layer instanceof GpxLayer || layer instanceof OsmDataLayer) ? layer : null;
|
---|
[1808] | 49 | }
|
---|
| 50 |
|
---|
[6084] | 51 | @Override
|
---|
[1169] | 52 | public void actionPerformed(ActionEvent e) {
|
---|
[1808] | 53 | if (!isEnabled())
|
---|
| 54 | return;
|
---|
[5477] | 55 | Layer layer = getLayer();
|
---|
[1808] | 56 | if (layer == null) {
|
---|
[2017] | 57 | JOptionPane.showMessageDialog(
|
---|
[1847] | 58 | Main.parent,
|
---|
| 59 | tr("Nothing to export. Get some data first."),
|
---|
| 60 | tr("Information"),
|
---|
| 61 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 62 | );
|
---|
[1169] | 63 | return;
|
---|
| 64 | }
|
---|
[1808] | 65 | export(layer);
|
---|
| 66 | }
|
---|
[283] | 67 |
|
---|
[1808] | 68 | /**
|
---|
| 69 | * Exports a layer to a file. Launches a file chooser to request the user to enter a file name.
|
---|
[1949] | 70 | *
|
---|
[1808] | 71 | * <code>layer</code> must not be null. <code>layer</code> must be an instance of
|
---|
[5266] | 72 | * {@link OsmDataLayer} or {@link GpxLayer}.
|
---|
[2512] | 73 | *
|
---|
[1808] | 74 | * @param layer the layer
|
---|
[8291] | 75 | * @throws IllegalArgumentException if layer is null
|
---|
| 76 | * @throws IllegalArgumentException if layer is neither an instance of {@link OsmDataLayer}
|
---|
[5266] | 77 | * nor of {@link GpxLayer}
|
---|
[1808] | 78 | */
|
---|
| 79 | public void export(Layer layer) {
|
---|
[2842] | 80 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
[8443] | 81 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
[8509] | 82 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.",
|
---|
| 83 | layer.getClass().getName()));
|
---|
[1808] | 84 |
|
---|
[9461] | 85 | File file = createAndOpenSaveFileChooser(tr("Export GPX file"), GpxImporter.getFileFilter());
|
---|
[1169] | 86 | if (file == null)
|
---|
| 87 | return;
|
---|
[283] | 88 |
|
---|
[1949] | 89 | for (FileExporter exporter : ExtensionFileFilter.exporters) {
|
---|
| 90 | if (exporter.acceptFile(file, layer)) {
|
---|
| 91 | try {
|
---|
| 92 | exporter.exportData(file, layer);
|
---|
[6643] | 93 | } catch (IOException e) {
|
---|
| 94 | Main.error(e);
|
---|
[1169] | 95 | }
|
---|
| 96 | }
|
---|
[1808] | 97 | }
|
---|
[1169] | 98 | }
|
---|
[1808] | 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Refreshes the enabled state
|
---|
| 102 | */
|
---|
[1820] | 103 | @Override
|
---|
| 104 | protected void updateEnabledState() {
|
---|
[5477] | 105 | setEnabled(getLayer() != null);
|
---|
[1808] | 106 | }
|
---|
[283] | 107 | }
|
---|