1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractAction;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
|
---|
15 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
---|
16 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The action to merge the currently selected layer into another layer.
|
---|
24 | */
|
---|
25 | public final class DuplicateAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
26 | private transient Layer layer;
|
---|
27 | private final LayerListModel model;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code DuplicateAction}.
|
---|
31 | * @param layer the layer
|
---|
32 | * @param model layer list model
|
---|
33 | * @throws IllegalArgumentException if {@code layer} is null
|
---|
34 | */
|
---|
35 | public DuplicateAction(Layer layer, LayerListModel model) {
|
---|
36 | this(model);
|
---|
37 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
38 | this.layer = layer;
|
---|
39 | updateEnabledState();
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new {@code DuplicateAction}.
|
---|
44 | * @param model layer list model
|
---|
45 | */
|
---|
46 | public DuplicateAction(LayerListModel model) {
|
---|
47 | this.model = model;
|
---|
48 | putValue(NAME, tr("Duplicate"));
|
---|
49 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "duplicatelayer"));
|
---|
50 | putValue(SHORT_DESCRIPTION, tr("Duplicate this layer"));
|
---|
51 | putValue("help", HelpUtil.ht("/Dialog/LayerList#DuplicateLayer"));
|
---|
52 | updateEnabledState();
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void duplicate(Layer layer) {
|
---|
56 | if (!Main.isDisplayingMapView())
|
---|
57 | return;
|
---|
58 |
|
---|
59 | List<String> layerNames = new ArrayList<>();
|
---|
60 | for (Layer l: Main.getLayerManager().getLayers()) {
|
---|
61 | layerNames.add(l.getName());
|
---|
62 | }
|
---|
63 | if (layer instanceof OsmDataLayer) {
|
---|
64 | OsmDataLayer oldLayer = (OsmDataLayer) layer;
|
---|
65 | // Translators: "Copy of {layer name}"
|
---|
66 | String newName = tr("Copy of {0}", oldLayer.getName());
|
---|
67 | int i = 2;
|
---|
68 | while (layerNames.contains(newName)) {
|
---|
69 | // Translators: "Copy {number} of {layer name}"
|
---|
70 | newName = tr("Copy {1} of {0}", oldLayer.getName(), i);
|
---|
71 | i++;
|
---|
72 | }
|
---|
73 | Main.getLayerManager().addLayer(new OsmDataLayer(new DataSet(oldLayer.data), newName, null));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 | if (layer != null) {
|
---|
80 | duplicate(layer);
|
---|
81 | } else {
|
---|
82 | duplicate(model.getSelectedLayers().get(0));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public void updateEnabledState() {
|
---|
88 | if (layer == null) {
|
---|
89 | if (model.getSelectedLayers().size() == 1) {
|
---|
90 | setEnabled(model.getSelectedLayers().get(0) instanceof OsmDataLayer);
|
---|
91 | } else {
|
---|
92 | setEnabled(false);
|
---|
93 | }
|
---|
94 | } else {
|
---|
95 | setEnabled(layer instanceof OsmDataLayer);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|