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.Arrays;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Objects;
|
---|
8 | import java.util.function.IntFunction;
|
---|
9 | import java.util.function.Supplier;
|
---|
10 | import java.util.stream.Collectors;
|
---|
11 | import java.util.stream.Stream;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.JTable;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
17 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
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 IntFunction<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, IntFunction<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 | protected Stream<String> valueStream() {
|
---|
44 | int[] rows = tagTable.getSelectedRows();
|
---|
45 | Collection<? extends Tagged> sel = objectSupplier.get();
|
---|
46 | if (rows.length == 0 || sel == null || sel.isEmpty()) return Stream.empty();
|
---|
47 | return Arrays.stream(rows)
|
---|
48 | .mapToObj(keySupplier)
|
---|
49 | .flatMap(key -> sel.stream().map(p -> getString(p, key)))
|
---|
50 | .filter(Objects::nonNull)
|
---|
51 | .flatMap(Collection::stream)
|
---|
52 | .distinct();
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void actionPerformed(ActionEvent ae) {
|
---|
57 | final String values = valueStream()
|
---|
58 | .sorted()
|
---|
59 | .collect(Collectors.joining("\n"));
|
---|
60 | if (!values.isEmpty()) {
|
---|
61 | ClipboardUtils.copyString(values);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|