source: josm/trunk/src/org/openstreetmap/josm/actions/RestorePropertyAction.java

Last change on this file was 18051, checked in by Don-vip, 3 years ago

upgrade to checkstyle 8.43, fix violations

File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.Map;
10import java.util.function.IntFunction;
11import java.util.function.Supplier;
12
13import javax.swing.AbstractAction;
14import javax.swing.ListSelectionModel;
15
16import org.openstreetmap.josm.command.ChangePropertyCommand;
17import org.openstreetmap.josm.data.UndoRedoHandler;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.gui.util.TableHelper;
20import 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 */
27public 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}
Note: See TracBrowser for help on using the repository browser.