1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
14 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 |
|
---|
17 | public class DuplicateLayerAction extends JosmAction {
|
---|
18 |
|
---|
19 | public DuplicateLayerAction() {
|
---|
20 | super(tr("Duplicate Layer"), "dialogs/duplicatelayer", tr("Make a duplicate of the currently selected layer."),
|
---|
21 | Shortcut.registerShortcut("layer:duplicate", tr("Layer: {0}", tr("Duplicate")), KeyEvent.VK_N, Shortcut.GROUP_NONE), true);
|
---|
22 | putValue("help", ht("/Action/DuplicateLayer"));
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void actionPerformed(ActionEvent e) {
|
---|
26 | Layer sourceLayer = Main.main.getEditLayer();
|
---|
27 | if (sourceLayer == null)
|
---|
28 | return;
|
---|
29 | duplicate(sourceLayer);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void duplicate(Layer layer) {
|
---|
33 | if ((Main.map == null) || (Main.map.mapView == null))
|
---|
34 | return;
|
---|
35 | List<String> layerNames = new ArrayList<String>();
|
---|
36 | for (Layer l: Main.map.mapView.getAllLayers()) {
|
---|
37 | layerNames.add(l.getName());
|
---|
38 | }
|
---|
39 | if (layer instanceof OsmDataLayer) {
|
---|
40 | OsmDataLayer oldLayer = (OsmDataLayer)layer;
|
---|
41 | // Translators: "Copy of {layer name}"
|
---|
42 | String newName = tr("Copy of {0}", oldLayer.getName());
|
---|
43 | int i = 2;
|
---|
44 | while (layerNames.contains(newName)) {
|
---|
45 | // Translators: "Copy {number} of {layer name}"
|
---|
46 | newName = tr("Copy {1} of {0}", oldLayer.getName(), i);
|
---|
47 | i++;
|
---|
48 | }
|
---|
49 | Main.main.addLayer(new OsmDataLayer(oldLayer.data.clone(), newName, null));
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static boolean canDuplicate(Layer layer) {
|
---|
54 | if (layer instanceof OsmDataLayer)
|
---|
55 | return true;
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 | }
|
---|