| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 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.text.MessageFormat; |
|---|
| 13 | |
|---|
| 14 | import javax.swing.JOptionPane; |
|---|
| 15 | |
|---|
| 16 | import org.openstreetmap.josm.Main; |
|---|
| 17 | import org.openstreetmap.josm.gui.layer.GpxLayer; |
|---|
| 18 | import org.openstreetmap.josm.gui.layer.Layer; |
|---|
| 19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
|---|
| 20 | import org.openstreetmap.josm.io.FileExporter; |
|---|
| 21 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 22 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Exports data to gpx. |
|---|
| 26 | */ |
|---|
| 27 | public class GpxExportAction extends DiskAccessAction { |
|---|
| 28 | |
|---|
| 29 | public GpxExportAction() { |
|---|
| 30 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."), |
|---|
| 31 | Shortcut.registerShortcut("file:exportgpx", tr("Export to GPX..."), KeyEvent.VK_E, Shortcut.CTRL)); |
|---|
| 32 | putValue("help", ht("/Action/GpxExport")); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | protected GpxLayer getLayer() { |
|---|
| 36 | if (!Main.isDisplayingMapView()) return null; |
|---|
| 37 | if (Main.map.mapView.getActiveLayer() == null) return null; |
|---|
| 38 | Layer layer = Main.map.mapView.getActiveLayer(); |
|---|
| 39 | if (! (layer instanceof GpxLayer)) return null; |
|---|
| 40 | return (GpxLayer)layer; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public void actionPerformed(ActionEvent e) { |
|---|
| 44 | if (!isEnabled()) |
|---|
| 45 | return; |
|---|
| 46 | GpxLayer layer = getLayer(); |
|---|
| 47 | if (layer == null) { |
|---|
| 48 | JOptionPane.showMessageDialog( |
|---|
| 49 | Main.parent, |
|---|
| 50 | tr("Nothing to export. Get some data first."), |
|---|
| 51 | tr("Information"), |
|---|
| 52 | JOptionPane.INFORMATION_MESSAGE |
|---|
| 53 | ); |
|---|
| 54 | return; |
|---|
| 55 | } |
|---|
| 56 | export(layer); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Exports a layer to a file. Launches a file chooser to request the user to enter a file name. |
|---|
| 61 | * |
|---|
| 62 | * <code>layer</code> must not be null. <code>layer</code> must be an instance of |
|---|
| 63 | * {@see OsmDataLayer} or {@see GpxLayer}. |
|---|
| 64 | * |
|---|
| 65 | * @param layer the layer |
|---|
| 66 | * @exception IllegalArgumentException thrown if layer is null |
|---|
| 67 | * @exception IllegalArgumentException thrown if layer is neither an instance of {@see OsmDataLayer} |
|---|
| 68 | * nor of {@see GpxLayer} |
|---|
| 69 | */ |
|---|
| 70 | public void export(Layer layer) { |
|---|
| 71 | CheckParameterUtil.ensureParameterNotNull(layer, "layer"); |
|---|
| 72 | if (! (layer instanceof OsmDataLayer) && ! (layer instanceof GpxLayer)) |
|---|
| 73 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer.getClass().getName())); |
|---|
| 74 | |
|---|
| 75 | File file = createAndOpenSaveFileChooser(tr("Export GPX file"), "gpx"); |
|---|
| 76 | if (file == null) |
|---|
| 77 | return; |
|---|
| 78 | |
|---|
| 79 | for (FileExporter exporter : ExtensionFileFilter.exporters) { |
|---|
| 80 | if (exporter.acceptFile(file, layer)) { |
|---|
| 81 | try { |
|---|
| 82 | exporter.exportData(file, layer); |
|---|
| 83 | } catch (IOException e1) { |
|---|
| 84 | e1.printStackTrace(); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Refreshes the enabled state |
|---|
| 92 | * |
|---|
| 93 | */ |
|---|
| 94 | @Override |
|---|
| 95 | protected void updateEnabledState() { |
|---|
| 96 | boolean check = |
|---|
| 97 | Main.isDisplayingMapView() |
|---|
| 98 | && Main.map.mapView.getActiveLayer() != null; |
|---|
| 99 | if(!check) { |
|---|
| 100 | setEnabled(false); |
|---|
| 101 | return; |
|---|
| 102 | } |
|---|
| 103 | Layer layer = Main.map.mapView.getActiveLayer(); |
|---|
| 104 | setEnabled(layer instanceof GpxLayer); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|