001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.properties;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.KeyEvent;
007import java.util.Collection;
008import java.util.function.IntFunction;
009import java.util.function.Supplier;
010import java.util.stream.Collectors;
011
012import javax.swing.JTable;
013
014import org.openstreetmap.josm.data.osm.Tag;
015import org.openstreetmap.josm.data.osm.Tagged;
016import org.openstreetmap.josm.gui.MainApplication;
017import org.openstreetmap.josm.tools.Shortcut;
018import org.openstreetmap.josm.tools.ImageProvider;
019
020/**
021 * Copy the key and value of all the tags to clipboard.
022 * @since 13521
023 */
024public class CopyAllKeyValueAction extends AbstractCopyAction {
025
026    /**
027     * Constructs a new {@code CopyAllKeyValueAction}.
028     * @param tagTable the tag table
029     * @param keyFn a function which returns the selected key for a given row index
030     * @param objectSp a supplier which returns the selected tagged object(s)
031     */
032    public CopyAllKeyValueAction(JTable tagTable, IntFunction<String> keyFn, Supplier<Collection<? extends Tagged>> objectSp) {
033        super(tagTable, keyFn, objectSp);
034        putValue(NAME, tr("Copy all Keys/Values"));
035        putValue(SHORT_DESCRIPTION, tr("Copy the key and value of all the tags to clipboard"));
036        new ImageProvider("copy").getResource().attachImageIcon(this, true);
037    }
038
039    /**
040     * Registers this action shortcut
041     * @return this instance, for easy chaining
042     */
043    CopyAllKeyValueAction registerShortcut() {
044        Shortcut sc = Shortcut.registerShortcut("system:copytags", tr("Edit: {0}", tr("Copy Tags")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
045        MainApplication.registerActionShortcut(this, sc);
046        sc.setAccelerator(this);
047        return this;
048    }
049
050    @Override
051    protected Collection<String> getString(Tagged p, String key) {
052        return p.getKeys().entrySet().stream()
053                .map(kv -> new Tag(kv.getKey(), kv.getValue()).toString())
054                .collect(Collectors.toList());
055    }
056}