1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Objects;
|
---|
7 | import java.util.Set;
|
---|
8 | import java.util.TreeSet;
|
---|
9 | import java.util.function.Function;
|
---|
10 | import java.util.function.Supplier;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.JTable;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
16 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Super class of all copy actions from tag table.
|
---|
21 | * @since 13521
|
---|
22 | */
|
---|
23 | public abstract class AbstractCopyAction extends AbstractAction {
|
---|
24 |
|
---|
25 | private final JTable tagTable;
|
---|
26 | private final Function<Integer, String> keySupplier;
|
---|
27 | private final Supplier<Collection<? extends Tagged>> objectSupplier;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code AbstractCopyAction}.
|
---|
31 | * @param tagTable the tag table
|
---|
32 | * @param keySupplier a supplier which returns the selected key for a given row index
|
---|
33 | * @param objectSupplier a supplier which returns the selected tagged object(s)
|
---|
34 | */
|
---|
35 | public AbstractCopyAction(JTable tagTable, Function<Integer, String> keySupplier, Supplier<Collection<? extends Tagged>> objectSupplier) {
|
---|
36 | this.tagTable = Objects.requireNonNull(tagTable);
|
---|
37 | this.keySupplier = Objects.requireNonNull(keySupplier);
|
---|
38 | this.objectSupplier = Objects.requireNonNull(objectSupplier);
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected abstract Collection<String> getString(Tagged p, String key);
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent ae) {
|
---|
45 | int[] rows = tagTable.getSelectedRows();
|
---|
46 | Set<String> values = new TreeSet<>();
|
---|
47 | Collection<? extends Tagged> sel = objectSupplier.get();
|
---|
48 | if (rows.length == 0 || sel.isEmpty()) return;
|
---|
49 |
|
---|
50 | for (int row: rows) {
|
---|
51 | String key = keySupplier.apply(row);
|
---|
52 | if (sel.isEmpty())
|
---|
53 | return;
|
---|
54 | for (Tagged p : sel) {
|
---|
55 | Collection<String> s = getString(p, key);
|
---|
56 | if (s != null) {
|
---|
57 | values.addAll(s);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | if (!values.isEmpty()) {
|
---|
62 | ClipboardUtils.copyString(Utils.join("\n", values));
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|