001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.gpx;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.GridBagLayout;
008import java.awt.event.ActionEvent;
009import java.io.File;
010import java.util.ArrayList;
011import java.util.List;
012
013import javax.swing.AbstractAction;
014import javax.swing.JLabel;
015import javax.swing.JOptionPane;
016import javax.swing.JPanel;
017
018import org.openstreetmap.josm.actions.SimplifyWayAction;
019import org.openstreetmap.josm.data.osm.DataSet;
020import org.openstreetmap.josm.data.osm.Way;
021import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
022import org.openstreetmap.josm.gui.MainApplication;
023import org.openstreetmap.josm.gui.layer.Layer;
024import org.openstreetmap.josm.gui.layer.OsmDataLayer;
025import org.openstreetmap.josm.gui.widgets.UrlLabel;
026import org.openstreetmap.josm.spi.preferences.Config;
027import org.openstreetmap.josm.tools.GBC;
028import org.openstreetmap.josm.tools.ImageProvider;
029
030/**
031 * An abstract action for a conversion from a {@code T} {@link Layer} to a {@link OsmDataLayer}.
032 * @param <T> the source layer class
033 */
034public abstract class ConvertToDataLayerAction<T extends Layer> extends AbstractAction {
035    /** source layer */
036    protected final transient T layer;
037
038    /**
039     * Constructs a new {@code ConvertToDataLayerAction}
040     * @param layer source layer
041     */
042    protected ConvertToDataLayerAction(final T layer) {
043        super(tr("Convert to data layer"));
044        new ImageProvider("converttoosm").getResource().attachImageIcon(this, true);
045        this.layer = layer;
046        putValue("help", ht("/Action/ConvertToDataLayer"));
047    }
048
049    /**
050     * Performs the conversion to a {@link DataSet}.
051     * @return the resulting dataset
052     */
053    public abstract DataSet convert();
054
055    @Override
056    public void actionPerformed(ActionEvent e) {
057        JPanel msg = new JPanel(new GridBagLayout());
058        msg.add(new JLabel(
059                tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>"
060                        + "If you want to upload traces, look here:</html>")),
061                GBC.eol());
062        msg.add(new UrlLabel(Config.getUrls().getOSMWebsite() + "/traces", 2), GBC.eop());
063        if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", MainApplication.getMainFrame(), msg, tr("Warning"),
064                JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
065            return;
066        }
067        final DataSet ds = convert();
068        if (ds != null) {
069            List<Way> ways = new ArrayList<>(ds.getWays());
070            double err = SimplifyWayAction.askSimplifyWays(ways, tr("Would you like to simplify the ways in the converted layer?"), true);
071            if (err > 0) {
072                SimplifyWayAction.simplifyWays(ways, err);
073            }
074            final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), null);
075            if (layer.getAssociatedFile() != null) {
076                osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(),
077                        layer.getAssociatedFile().getName() + ".osm"));
078            }
079            osmLayer.setUploadDiscouraged(true);
080            MainApplication.getLayerManager().addLayer(osmLayer, false);
081            MainApplication.getLayerManager().removeLayer(layer);
082        }
083    }
084}