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