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