001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.relation;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007
008import org.openstreetmap.josm.data.osm.Relation;
009import org.openstreetmap.josm.gui.MainApplication;
010import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
011import org.openstreetmap.josm.tools.ImageProvider;
012
013/**
014 * Creates a new relation with a copy of the current editor state
015 * @since 5799
016 */
017public class DuplicateRelationAction extends AbstractRelationAction {
018
019    /**
020     * Constructs a new {@code DuplicateRelationAction}.
021     */
022    public DuplicateRelationAction() {
023        putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
024        new ImageProvider("duplicate").getResource().attachImageIcon(this, true);
025        putValue(NAME, tr("Duplicate"));
026    }
027
028    /**
029     * Duplicates the given relation and launches the relation editor for the created copy.
030     * @param original The relation to duplicate
031     */
032    public static void duplicateRelationAndLaunchEditor(Relation original) {
033        Relation copy = new Relation(original, true);
034        copy.setModified(true);
035        RelationEditor editor = RelationEditor.getEditor(
036                MainApplication.getLayerManager().getEditLayer(),
037                copy,
038                null /* no selected members */
039                );
040        editor.setVisible(true);
041    }
042
043    @Override
044    public void actionPerformed(ActionEvent e) {
045        if (!isEnabled() || relations.isEmpty())
046            return;
047        Relation r = relations.iterator().next();
048        duplicateRelationAndLaunchEditor(r);
049    }
050
051    @Override
052    protected void updateEnabledState() {
053        // only one selected relation can be edited
054        setEnabled(relations.size() == 1 && !relations.iterator().next().getDataSet().isLocked());
055    }
056}