1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
9 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
10 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
11 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Creates a new relation with a copy of the current editor state
|
---|
15 | * @since 5799
|
---|
16 | */
|
---|
17 | public class DuplicateRelationAction extends AbstractRelationAction {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Constructs a new {@code DuplicateRelationAction}.
|
---|
21 | */
|
---|
22 | public DuplicateRelationAction() {
|
---|
23 | putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
|
---|
24 | new ImageProvider("duplicate").getResource().attachImageIcon(this, true);
|
---|
25 | putValue(NAME, tr("Duplicate"));
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Duplicates the given relation and launches the relation editor for the created copy.
|
---|
30 | * @param original The relation to duplicate
|
---|
31 | */
|
---|
32 | public static void duplicateRelationAndLaunchEditor(Relation original) {
|
---|
33 | Relation copy = new Relation(original, true);
|
---|
34 | copy.setModified(true);
|
---|
35 | RelationEditor editor = RelationEditor.getEditor(
|
---|
36 | MainApplication.getLayerManager().getEditLayer(),
|
---|
37 | copy,
|
---|
38 | null /* no selected members */
|
---|
39 | );
|
---|
40 | editor.setVisible(true);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | if (!isEnabled() || relations.isEmpty())
|
---|
46 | return;
|
---|
47 | Relation r = relations.iterator().next();
|
---|
48 | duplicateRelationAndLaunchEditor(r);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected void updateEnabledState() {
|
---|
53 | // only one selected relation can be edited
|
---|
54 | setEnabled(relations.size() == 1 && !relations.iterator().next().getDataSet().isLocked());
|
---|
55 | }
|
---|
56 | }
|
---|