1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.function.IntFunction;
|
---|
11 | import java.util.function.Supplier;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.ListSelectionModel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
17 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.util.TableHelper;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Obtains the selected key and values from a table and restores those properties on the specified primitive.
|
---|
24 | *
|
---|
25 | * @since 16593
|
---|
26 | */
|
---|
27 | public class RestorePropertyAction extends AbstractAction {
|
---|
28 |
|
---|
29 | private final IntFunction<String> keyFn;
|
---|
30 | private final IntFunction<String> valueFn;
|
---|
31 | private final Supplier<OsmPrimitive> objectSp;
|
---|
32 | private final ListSelectionModel selectionModel;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code RestorePropertyAction}.
|
---|
36 | *
|
---|
37 | * @param keyFn a function which returns the selected key for a given row index
|
---|
38 | * @param valueFn a function which returns the selected value for a given row index
|
---|
39 | * @param objectSp a supplier which returns the selected tagged object
|
---|
40 | * @param selectionModel selection model
|
---|
41 | */
|
---|
42 | public RestorePropertyAction(IntFunction<String> keyFn, IntFunction<String> valueFn,
|
---|
43 | Supplier<OsmPrimitive> objectSp, ListSelectionModel selectionModel) {
|
---|
44 | super(tr("Restore selected tags"));
|
---|
45 | this.keyFn = keyFn;
|
---|
46 | this.valueFn = valueFn;
|
---|
47 | this.objectSp = objectSp;
|
---|
48 | this.selectionModel = selectionModel;
|
---|
49 | new ImageProvider("undo").getResource().attachImageIcon(this, true);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | OsmPrimitive primitive = objectSp.get();
|
---|
55 | if (primitive == null) return;
|
---|
56 |
|
---|
57 | Map<String, String> changes = TableHelper.selectedIndices(selectionModel).boxed()
|
---|
58 | .collect(HashMap::new, (m, i) -> m.put(keyFn.apply(i), valueFn.apply(i)), HashMap::putAll);
|
---|
59 | ChangePropertyCommand command = new ChangePropertyCommand(Collections.singleton(primitive), changes);
|
---|
60 | UndoRedoHandler.getInstance().add(command);
|
---|
61 | }
|
---|
62 | }
|
---|