source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/replacegeometry/ReplaceGeometryAction.java

Last change on this file was 34844, checked in by gerdp, 3 months ago

see #10225: Improve case when two new ways are selected

Also improves javadoc and simplifies exception handling

File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.replacegeometry;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.actions.JosmAction;
15import org.openstreetmap.josm.data.UndoRedoHandler;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.gui.Notification;
18import org.openstreetmap.josm.tools.Shortcut;
19
20/**
21 * Replaces already existing object (id>0) with a new object (id<0).
22 *
23 * @author Zverik
24 */
25public class ReplaceGeometryAction extends JosmAction {
26    private static final String TITLE = tr("Replace Geometry");
27
28    public ReplaceGeometryAction() {
29        super(TITLE, "dumbutils/replacegeometry", tr("Replace geometry of selected object with a new one"),
30                Shortcut.registerShortcut("tools:replacegeometry", tr("Tool: {0}", tr("Replace Geometry")), KeyEvent.VK_G, Shortcut.CTRL_SHIFT),
31                true);
32    }
33
34    @Override
35    public void actionPerformed(ActionEvent e) {
36        if (getLayerManager().getEditDataSet() == null) {
37            return;
38        }
39
40        // There must be two ways selected: one with id > 0 and one new.
41        List<OsmPrimitive> selection = new ArrayList<>(getLayerManager().getEditDataSet().getSelected());
42        if (selection.size() != 2) {
43            new Notification(
44                    tr("This tool replaces geometry of one object with another, and so requires exactly two objects to be selected.")
45                    ).setIcon(JOptionPane.WARNING_MESSAGE).show();
46            return;
47        }
48
49        OsmPrimitive firstObject = selection.get(0);
50        OsmPrimitive secondObject = selection.get(1);
51
52        try {
53            ReplaceGeometryCommand replaceCommand =
54                    ReplaceGeometryUtils.buildReplaceWithNewCommand(firstObject, secondObject);
55
56            // action was canceled
57            if (replaceCommand == null)
58                return;
59
60            UndoRedoHandler.getInstance().add(replaceCommand);
61        } catch (IllegalArgumentException | ReplaceGeometryException ex) {
62            new Notification(
63                    ex.getMessage()
64                    ).setIcon(JOptionPane.WARNING_MESSAGE).show();
65        }
66    }
67
68    @Override
69    protected void updateEnabledState() {
70        updateEnabledStateOnCurrentSelection();
71    }
72
73    @Override
74    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
75        setEnabled(selection != null && selection.size() >= 2);
76    }
77}
Note: See TracBrowser for help on using the repository browser.