1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.io.File;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
22 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * An abstract action for a conversion from a {@code T} {@link Layer} to a {@link OsmDataLayer}.
|
---|
28 | * @param <T> the source layer class
|
---|
29 | */
|
---|
30 | public abstract class ConvertToDataLayerAction<T extends Layer> extends AbstractAction {
|
---|
31 | /** source layer */
|
---|
32 | protected final transient T layer;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code ConvertToDataLayerAction}
|
---|
36 | * @param layer source layer
|
---|
37 | */
|
---|
38 | protected ConvertToDataLayerAction(final T layer) {
|
---|
39 | super(tr("Convert to data layer"));
|
---|
40 | new ImageProvider("converttoosm").getResource().attachImageIcon(this, true);
|
---|
41 | this.layer = layer;
|
---|
42 | putValue("help", ht("/Action/ConvertToDataLayer"));
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Performs the conversion to a {@link DataSet}.
|
---|
47 | * @return the resulting dataset
|
---|
48 | */
|
---|
49 | public abstract DataSet convert();
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void actionPerformed(ActionEvent e) {
|
---|
53 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
54 | msg.add(new JLabel(
|
---|
55 | tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>"
|
---|
56 | + "If you want to upload traces, look here:</html>")),
|
---|
57 | GBC.eol());
|
---|
58 | msg.add(new UrlLabel(Config.getUrls().getOSMWebsite() + "/traces", 2), GBC.eop());
|
---|
59 | if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", MainApplication.getMainFrame(), msg, tr("Warning"),
|
---|
60 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | final DataSet ds = convert();
|
---|
64 | if (ds != null) {
|
---|
65 | final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), null);
|
---|
66 | if (layer.getAssociatedFile() != null) {
|
---|
67 | osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(),
|
---|
68 | layer.getAssociatedFile().getName() + ".osm"));
|
---|
69 | }
|
---|
70 | osmLayer.setUploadDiscouraged(true);
|
---|
71 | MainApplication.getLayerManager().addLayer(osmLayer, false);
|
---|
72 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|