1 | // License: GPL. For details, see LICENSE file.
|
---|
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.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.io.importexport.FileExporter;
|
---|
18 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
|
---|
19 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
23 | import org.openstreetmap.josm.tools.Logging;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Exports data to gpx.
|
---|
28 | * @since 78
|
---|
29 | */
|
---|
30 | public class GpxExportAction extends DiskAccessAction {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code GpxExportAction}.
|
---|
34 | */
|
---|
35 | public GpxExportAction() {
|
---|
36 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."),
|
---|
37 | Shortcut.registerShortcut("file:exportgpx", tr("Export to GPX..."), KeyEvent.VK_E, Shortcut.CTRL));
|
---|
38 | setHelpId(ht("/Action/GpxExport"));
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
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 | /**
|
---|
60 | * Get the layer to export.
|
---|
61 | * @return The layer to export, either a {@link GpxLayer} or {@link OsmDataLayer}.
|
---|
62 | */
|
---|
63 | protected Layer getLayer() {
|
---|
64 | Layer layer = getLayerManager().getActiveLayer();
|
---|
65 | return (layer instanceof GpxLayer || layer instanceof OsmDataLayer) ? layer : null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void actionPerformed(ActionEvent e) {
|
---|
70 | if (!isEnabled())
|
---|
71 | return;
|
---|
72 | Layer layer = getLayer();
|
---|
73 | if (layer == null) {
|
---|
74 | JOptionPane.showMessageDialog(
|
---|
75 | MainApplication.getMainFrame(),
|
---|
76 | tr("Nothing to export. Get some data first."),
|
---|
77 | tr("Information"),
|
---|
78 | JOptionPane.INFORMATION_MESSAGE
|
---|
79 | );
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | export(layer);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Exports a layer to a file. Launches a file chooser to request the user to enter a file name.
|
---|
87 | *
|
---|
88 | * <code>layer</code> must not be null. <code>layer</code> must be an instance of
|
---|
89 | * {@link OsmDataLayer} or {@link GpxLayer}.
|
---|
90 | *
|
---|
91 | * @param layer the layer
|
---|
92 | * @throws IllegalArgumentException if layer is null
|
---|
93 | * @throws IllegalArgumentException if layer is neither an instance of {@link OsmDataLayer}
|
---|
94 | * nor of {@link GpxLayer}
|
---|
95 | */
|
---|
96 | public void export(Layer layer) {
|
---|
97 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
98 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
99 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.",
|
---|
100 | layer.getClass().getName()));
|
---|
101 |
|
---|
102 | File file = createAndOpenSaveFileChooser(tr("Export GPX file"), GpxImporter.getFileFilter());
|
---|
103 | if (file == null)
|
---|
104 | return;
|
---|
105 |
|
---|
106 | for (FileExporter exporter : ExtensionFileFilter.getExporters()) {
|
---|
107 | if (exporter.acceptFile(file, layer)) {
|
---|
108 | try {
|
---|
109 | exporter.exportData(file, layer);
|
---|
110 | } catch (IOException e) {
|
---|
111 | Logging.error(e);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Refreshes the enabled state
|
---|
119 | */
|
---|
120 | @Override
|
---|
121 | protected void updateEnabledState() {
|
---|
122 | setEnabled(getLayer() != null);
|
---|
123 | }
|
---|
124 | }
|
---|