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