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.LinkedList;
009import java.util.List;
010import java.util.Map.Entry;
011import java.util.function.IntFunction;
012import java.util.function.Supplier;
013
014import javax.swing.JTable;
015
016import org.openstreetmap.josm.data.osm.Tag;
017import org.openstreetmap.josm.data.osm.Tagged;
018import org.openstreetmap.josm.gui.MainApplication;
019import org.openstreetmap.josm.tools.Shortcut;
020
021/**
022 * Copy the key and value of all the tags to clipboard.
023 * @since 13521
024 */
025public class CopyAllKeyValueAction extends AbstractCopyAction {
026
027    /**
028     * Constructs a new {@code CopyAllKeyValueAction}.
029     * @param tagTable the tag table
030     * @param keyFn a function which returns the selected key for a given row index
031     * @param objectSp a supplier which returns the selected tagged object(s)
032     */
033    public CopyAllKeyValueAction(JTable tagTable, IntFunction<String> keyFn, Supplier<Collection<? extends Tagged>> objectSp) {
034        super(tagTable, keyFn, objectSp);
035        putValue(NAME, tr("Copy all Keys/Values"));
036        putValue(SHORT_DESCRIPTION, tr("Copy the key and value of all the tags to clipboard"));
037        Shortcut sc = Shortcut.registerShortcut("system:copytags", tr("Edit: {0}", tr("Copy Tags")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
038        MainApplication.registerActionShortcut(this, sc);
039        sc.setAccelerator(this);
040    }
041
042    @Override
043    protected Collection<String> getString(Tagged p, String key) {
044        List<String> r = new LinkedList<>();
045        for (Entry<String, String> kv : p.getKeys().entrySet()) {
046            r.add(new Tag(kv.getKey(), kv.getValue()).toString());
047        }
048        return r;
049    }
050}