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