source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/layer/DuplicateAction.java

Last change on this file was 18233, checked in by Don-vip, 3 years ago

see #21144 - fix #21330 - fix #21335 - proper handling of correlation support layers in "duplicate" and "gpx export" actions

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7
8import javax.swing.AbstractAction;
9
10import org.openstreetmap.josm.gui.MainApplication;
11import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
12import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
13import org.openstreetmap.josm.gui.help.HelpUtil;
14import org.openstreetmap.josm.gui.layer.Layer;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17import org.openstreetmap.josm.tools.ImageProvider;
18
19/**
20 * The action to duplicate the given selected layer into another layer.
21 */
22public final class DuplicateAction extends AbstractAction implements IEnabledStateUpdating {
23 private transient Layer layer;
24 private final LayerListModel model;
25
26 /**
27 * Constructs a new {@code DuplicateAction}.
28 * @param layer the layer
29 * @param model layer list model
30 * @throws IllegalArgumentException if {@code layer} is null
31 */
32 public DuplicateAction(Layer layer, LayerListModel model) {
33 this(model);
34 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
35 this.layer = layer;
36 updateEnabledState();
37 }
38
39 /**
40 * Constructs a new {@code DuplicateAction}.
41 * @param model layer list model
42 */
43 public DuplicateAction(LayerListModel model) {
44 this.model = model;
45 putValue(NAME, tr("Duplicate"));
46 new ImageProvider("dialogs", "duplicatelayer").getResource().attachImageIcon(this, true);
47 putValue(SHORT_DESCRIPTION, tr("Duplicate this layer"));
48 putValue("help", HelpUtil.ht("/Dialog/LayerList#DuplicateLayer"));
49 updateEnabledState();
50 }
51
52 private static void duplicate(Layer layer) {
53 if (layer instanceof OsmDataLayer) {
54 String newName = LayerListTransferHandler.suggestNewLayerName(layer.getName(), MainApplication.getLayerManager().getLayers());
55 MainApplication.getLayerManager().addLayer(((OsmDataLayer) layer).duplicate(newName));
56 }
57 }
58
59 @Override
60 public void actionPerformed(ActionEvent e) {
61 if (layer != null) {
62 duplicate(layer);
63 } else {
64 duplicate(model.getSelectedLayers().get(0));
65 }
66 }
67
68 @Override
69 public void updateEnabledState() {
70 if (layer == null) {
71 if (model != null && model.getSelectedLayers().size() == 1) {
72 setEnabled(model.getSelectedLayers().get(0) instanceof OsmDataLayer);
73 } else {
74 setEnabled(false);
75 }
76 } else {
77 setEnabled(layer instanceof OsmDataLayer);
78 }
79 }
80}
Note: See TracBrowser for help on using the repository browser.