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;
010
011import javax.swing.AbstractAction;
012import javax.swing.JLabel;
013import javax.swing.JOptionPane;
014import javax.swing.JPanel;
015
016import org.openstreetmap.josm.data.osm.DataSet;
017import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
018import org.openstreetmap.josm.gui.MainApplication;
019import org.openstreetmap.josm.gui.layer.Layer;
020import org.openstreetmap.josm.gui.layer.OsmDataLayer;
021import org.openstreetmap.josm.gui.widgets.UrlLabel;
022import org.openstreetmap.josm.spi.preferences.Config;
023import org.openstreetmap.josm.tools.GBC;
024import org.openstreetmap.josm.tools.ImageProvider;
025
026/**
027 * An abstract action for a conversion from a {@code T} {@link Layer} to a {@link OsmDataLayer}.
028 * @param <T> the source layer class
029 */
030public abstract class ConvertToDataLayerAction<T extends Layer> extends AbstractAction {
031    /** source layer */
032    protected final transient T layer;
033
034    /**
035     * Constructs a new {@code ConvertToDataLayerAction}
036     * @param layer source layer
037     */
038    protected ConvertToDataLayerAction(final T layer) {
039        super(tr("Convert to data layer"));
040        new ImageProvider("converttoosm").getResource().attachImageIcon(this, true);
041        this.layer = layer;
042        putValue("help", ht("/Action/ConvertToDataLayer"));
043    }
044
045    /**
046     * Performs the conversion to a {@link DataSet}.
047     * @return the resulting dataset
048     */
049    public abstract DataSet convert();
050
051    @Override
052    public void actionPerformed(ActionEvent e) {
053        JPanel msg = new JPanel(new GridBagLayout());
054        msg.add(new JLabel(
055                tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>"
056                        + "If you want to upload traces, look here:</html>")),
057                GBC.eol());
058        msg.add(new UrlLabel(Config.getUrls().getOSMWebsite() + "/traces", 2), GBC.eop());
059        if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", MainApplication.getMainFrame(), msg, tr("Warning"),
060                JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
061            return;
062        }
063        final DataSet ds = convert();
064        if (ds != null) {
065            final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), null);
066            if (layer.getAssociatedFile() != null) {
067                osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(),
068                        layer.getAssociatedFile().getName() + ".osm"));
069            }
070            osmLayer.setUploadDiscouraged(true);
071            MainApplication.getLayerManager().addLayer(osmLayer, false);
072            MainApplication.getLayerManager().removeLayer(layer);
073        }
074    }
075}