[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;
|
---|
[12671] | 17 | import org.openstreetmap.josm.gui.io.importexport.FileExporter;
|
---|
| 18 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
|
---|
[582] | 19 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
[283] | 20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[444] | 21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[2842] | 22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12620] | 23 | import org.openstreetmap.josm.tools.Logging;
|
---|
[1084] | 24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[283] | 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Exports data to gpx.
|
---|
[7414] | 28 | * @since 78
|
---|
[283] | 29 | */
|
---|
[1820] | 30 | public class GpxExportAction extends DiskAccessAction {
|
---|
[283] | 31 |
|
---|
[7414] | 32 | /**
|
---|
| 33 | * Constructs a new {@code GpxExportAction}.
|
---|
| 34 | */
|
---|
[1808] | 35 | public GpxExportAction() {
|
---|
[1212] | 36 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."),
|
---|
[4982] | 37 | Shortcut.registerShortcut("file:exportgpx", tr("Export to GPX..."), KeyEvent.VK_E, Shortcut.CTRL));
|
---|
[2323] | 38 | putValue("help", ht("/Action/GpxExport"));
|
---|
[1169] | 39 | }
|
---|
[283] | 40 |
|
---|
[5477] | 41 | /**
|
---|
[13210] | 42 | * Deferring constructor for child classes.
|
---|
| 43 | *
|
---|
| 44 | * @param name see {@code DiskAccessAction}
|
---|
| 45 | * @param iconName see {@code DiskAccessAction}
|
---|
| 46 | * @param tooltip see {@code DiskAccessAction}
|
---|
| 47 | * @param shortcut see {@code DiskAccessAction}
|
---|
| 48 | * @param register see {@code DiskAccessAction}
|
---|
| 49 | * @param toolbarId see {@code DiskAccessAction}
|
---|
| 50 | * @param installAdapters see {@code DiskAccessAction}
|
---|
| 51 | *
|
---|
| 52 | * @since 13210
|
---|
| 53 | */
|
---|
| 54 | protected GpxExportAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
| 55 | boolean register, String toolbarId, boolean installAdapters) {
|
---|
| 56 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
[5477] | 60 | * Get the layer to export.
|
---|
| 61 | * @return The layer to export, either a {@link GpxLayer} or {@link OsmDataLayer}.
|
---|
| 62 | */
|
---|
| 63 | protected Layer getLayer() {
|
---|
[12636] | 64 | Layer layer = getLayerManager().getActiveLayer();
|
---|
[5477] | 65 | return (layer instanceof GpxLayer || layer instanceof OsmDataLayer) ? layer : null;
|
---|
[1808] | 66 | }
|
---|
| 67 |
|
---|
[6084] | 68 | @Override
|
---|
[1169] | 69 | public void actionPerformed(ActionEvent e) {
|
---|
[1808] | 70 | if (!isEnabled())
|
---|
| 71 | return;
|
---|
[5477] | 72 | Layer layer = getLayer();
|
---|
[1808] | 73 | if (layer == null) {
|
---|
[2017] | 74 | JOptionPane.showMessageDialog(
|
---|
[1847] | 75 | Main.parent,
|
---|
| 76 | tr("Nothing to export. Get some data first."),
|
---|
| 77 | tr("Information"),
|
---|
| 78 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 79 | );
|
---|
[1169] | 80 | return;
|
---|
| 81 | }
|
---|
[1808] | 82 | export(layer);
|
---|
| 83 | }
|
---|
[283] | 84 |
|
---|
[1808] | 85 | /**
|
---|
| 86 | * Exports a layer to a file. Launches a file chooser to request the user to enter a file name.
|
---|
[1949] | 87 | *
|
---|
[1808] | 88 | * <code>layer</code> must not be null. <code>layer</code> must be an instance of
|
---|
[5266] | 89 | * {@link OsmDataLayer} or {@link GpxLayer}.
|
---|
[2512] | 90 | *
|
---|
[1808] | 91 | * @param layer the layer
|
---|
[8291] | 92 | * @throws IllegalArgumentException if layer is null
|
---|
| 93 | * @throws IllegalArgumentException if layer is neither an instance of {@link OsmDataLayer}
|
---|
[5266] | 94 | * nor of {@link GpxLayer}
|
---|
[1808] | 95 | */
|
---|
| 96 | public void export(Layer layer) {
|
---|
[2842] | 97 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
[8443] | 98 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
[8509] | 99 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.",
|
---|
| 100 | layer.getClass().getName()));
|
---|
[1808] | 101 |
|
---|
[9461] | 102 | File file = createAndOpenSaveFileChooser(tr("Export GPX file"), GpxImporter.getFileFilter());
|
---|
[1169] | 103 | if (file == null)
|
---|
| 104 | return;
|
---|
[283] | 105 |
|
---|
[10407] | 106 | for (FileExporter exporter : ExtensionFileFilter.getExporters()) {
|
---|
[1949] | 107 | if (exporter.acceptFile(file, layer)) {
|
---|
| 108 | try {
|
---|
| 109 | exporter.exportData(file, layer);
|
---|
[6643] | 110 | } catch (IOException e) {
|
---|
[12620] | 111 | Logging.error(e);
|
---|
[1169] | 112 | }
|
---|
| 113 | }
|
---|
[1808] | 114 | }
|
---|
[1169] | 115 | }
|
---|
[1808] | 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Refreshes the enabled state
|
---|
| 119 | */
|
---|
[1820] | 120 | @Override
|
---|
| 121 | protected void updateEnabledState() {
|
---|
[5477] | 122 | setEnabled(getLayer() != null);
|
---|
[1808] | 123 | }
|
---|
[283] | 124 | }
|
---|