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