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

Last change on this file since 17379 was 17034, checked in by Klumbumbus, 4 years ago
  • see #19808 - Add icon for "Go to OSM Tag History" (self created, CC0 and PD licensed)
  • use undo icon for "Restore selected tags" in context menu of history viewer
  • use undo icon for "No, discard the changes and close" when trying to close the relation editor with unsaved changes (two times cancel icon was confusing)
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.function.IntFunction;
10import java.util.function.Supplier;
11
12import javax.swing.AbstractAction;
13import javax.swing.ListSelectionModel;
14
15import org.openstreetmap.josm.command.ChangePropertyCommand;
16import org.openstreetmap.josm.data.UndoRedoHandler;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.gui.util.TableHelper;
19import 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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.