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 | import java.util.ArrayList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.actions.SimplifyWayAction;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
26 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * An abstract action for a conversion from a {@code T} {@link Layer} to a {@link OsmDataLayer}.
|
---|
32 | * @param <T> the source layer class
|
---|
33 | */
|
---|
34 | public abstract class ConvertToDataLayerAction<T extends Layer> extends AbstractAction {
|
---|
35 | /** source layer */
|
---|
36 | protected final transient T layer;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Constructs a new {@code ConvertToDataLayerAction}
|
---|
40 | * @param layer source layer
|
---|
41 | */
|
---|
42 | protected ConvertToDataLayerAction(final T layer) {
|
---|
43 | super(tr("Convert to data layer"));
|
---|
44 | new ImageProvider("converttoosm").getResource().attachImageIcon(this, true);
|
---|
45 | this.layer = layer;
|
---|
46 | putValue("help", ht("/Action/ConvertToDataLayer"));
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Performs the conversion to a {@link DataSet}.
|
---|
51 | * @return the resulting dataset
|
---|
52 | */
|
---|
53 | public abstract DataSet convert();
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void actionPerformed(ActionEvent e) {
|
---|
57 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
58 | msg.add(new JLabel(
|
---|
59 | tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>"
|
---|
60 | + "If you want to upload traces, look here:</html>")),
|
---|
61 | GBC.eol());
|
---|
62 | msg.add(new UrlLabel(Config.getUrls().getOSMWebsite() + "/traces", 2), GBC.eop());
|
---|
63 | if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", MainApplication.getMainFrame(), msg, tr("Warning"),
|
---|
64 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | final DataSet ds = convert();
|
---|
68 | if (ds != null) {
|
---|
69 | List<Way> ways = new ArrayList<>(ds.getWays());
|
---|
70 | double err = SimplifyWayAction.askSimplifyWays(ways, tr("Would you like to simplify the ways in the converted layer?"), true);
|
---|
71 | if (err > 0) {
|
---|
72 | SimplifyWayAction.simplifyWays(ways, err);
|
---|
73 | }
|
---|
74 | String name = layer.getName().replaceAll("^" + tr("Converted from: {0}", ""), "");
|
---|
75 | final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", name), null);
|
---|
76 | if (layer.getAssociatedFile() != null) {
|
---|
77 | osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(),
|
---|
78 | layer.getAssociatedFile().getName() + ".osm"));
|
---|
79 | }
|
---|
80 | osmLayer.setUploadDiscouraged(true);
|
---|
81 | MainApplication.getLayerManager().addLayer(osmLayer, false);
|
---|
82 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|