001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009import java.util.Collection;
010
011import org.openstreetmap.josm.data.osm.DataSet;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
014import org.openstreetmap.josm.tools.Shortcut;
015
016/**
017 * Action, to paste all tags from one primitive to another.
018 *
019 * It will take the primitive from the copy-paste buffer an apply all its tags
020 * to the selected primitive(s).
021 *
022 * @author David Earl
023 */
024public final class PasteTagsAction extends JosmAction {
025
026    private static final String help = ht("/Action/PasteTags");
027    private final OsmTransferHandler transferHandler = new OsmTransferHandler();
028
029    /**
030     * Constructs a new {@code PasteTagsAction}.
031     */
032    public PasteTagsAction() {
033        super(tr("Paste Tags"), "pastetags",
034                tr("Apply tags of contents of paste buffer to all selected items."),
035                Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")),
036                KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true);
037        putValue("help", help);
038    }
039
040    @Override
041    public void actionPerformed(ActionEvent e) {
042        Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
043
044        if (selection.isEmpty())
045            return;
046
047        transferHandler.pasteTags(selection);
048    }
049
050    @Override
051    protected void updateEnabledState() {
052        DataSet ds = getLayerManager().getEditDataSet();
053        if (ds == null) {
054            setEnabled(false);
055            return;
056        }
057        // buffer listening slows down the program and is not very good for arbitrary text in buffer
058        setEnabled(!ds.selectionEmpty());
059    }
060
061    @Override
062    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
063        setEnabled(selection != null && !selection.isEmpty());
064    }
065}