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

Last change on this file since 13849 was 13849, checked in by Don-vip, 6 years ago

SonarQube - fix minor code issues

  • Property svn:eol-style set to native
File size: 2.2 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.Collection;
6import java.util.Objects;
7import java.util.Set;
8import java.util.TreeSet;
9import java.util.function.IntFunction;
10import java.util.function.Supplier;
11
12import javax.swing.AbstractAction;
13import javax.swing.JTable;
14
15import org.openstreetmap.josm.data.osm.Tagged;
16import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
17import org.openstreetmap.josm.tools.Utils;
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 @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}
Note: See TracBrowser for help on using the repository browser.