source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/AbstractCopyAction.java@ 15813

Last change on this file since 15813 was 15813, checked in by simon04, 4 years ago

fix #18663 - AbstractCopyAction: copy distinct keys

Regression of r15717.

  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import java.awt.event.ActionEvent;
5import java.util.Arrays;
6import java.util.Collection;
7import java.util.Objects;
8import java.util.function.IntFunction;
9import java.util.function.Supplier;
10import java.util.stream.Collectors;
11import java.util.stream.Stream;
12
13import javax.swing.AbstractAction;
14import javax.swing.JTable;
15
16import org.openstreetmap.josm.data.osm.Tagged;
17import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
18
19/**
20 * Super class of all copy actions from tag table.
21 * @since 13521
22 */
23public 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}
Note: See TracBrowser for help on using the repository browser.