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.Main;
009import org.openstreetmap.josm.data.osm.Relation;
010import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
011import org.openstreetmap.josm.tools.ImageProvider;
012
013
014/**
015 * Creates a new relation with a copy of the current editor state
016 * @since 5799
017 */
018public class DuplicateRelationAction extends AbstractRelationAction {
019    
020    /**
021     * Constructs a new {@code DuplicateRelationAction}. 
022     */
023    public DuplicateRelationAction() {
024        putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
025        putValue(SMALL_ICON, ImageProvider.get("duplicate"));
026        putValue(NAME, tr("Duplicate"));
027    }
028
029    public static void duplicateRelationAndLaunchEditor(Relation original) {
030        Relation copy = new Relation(original, true);
031        copy.setModified(true);
032        RelationEditor editor = RelationEditor.getEditor(
033                Main.main.getEditLayer(),
034                copy,
035                null /* no selected members */
036                );
037        editor.setVisible(true);
038    }
039
040    @Override
041    public void actionPerformed(ActionEvent e) {
042        if (!isEnabled() || relations.isEmpty())
043            return;
044        Relation r = relations.iterator().next();
045        duplicateRelationAndLaunchEditor(r);
046    }
047
048    @Override
049    protected void updateEnabledState() {
050        // only one selected relation can be edited
051        setEnabled( relations.size()==1 );
052    }        
053}