source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/UnGlueRelationAction.java

Last change on this file was 34812, checked in by gerdp, 4 months ago

see #17187:

  • Replace deprecated methods
  • Remove dead code
  • Make Shift+Z (Select last modified nodes) work again + Repated Shift+Z cycles through the commands available in the undo tree + Ignore changes in other layers
  • Make Alt+Shift+Z (Select last modified Ways) work again (same logic as with Shift+Z)
  • fix some javadoc errors


  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.LinkedList;
11import java.util.List;
12
13import org.openstreetmap.josm.actions.JosmAction;
14import org.openstreetmap.josm.command.AddCommand;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.SequenceCommand;
17import org.openstreetmap.josm.data.UndoRedoHandler;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Relation;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.plugins.utilsplugin2.command.ChangeRelationMemberCommand;
25import org.openstreetmap.josm.tools.Shortcut;
26import org.openstreetmap.josm.tools.Utils;
27
28/**
29 * Duplicate nodes, ways and relations that are used by multiple relations.
30 *
31 * Resulting nodes, ways and relations are identical as the orginals.
32 *
33 * @author Kalle Lampila
34 *
35 */
36public class UnGlueRelationAction extends JosmAction {
37
38    /**
39     * Create a new UnGlueRelationAction.
40     */
41    public UnGlueRelationAction() {
42        super(tr("UnGlue Relation"), "ungluerelations", tr("Duplicate nodes, ways and relations that are used by multiple relations."),
43                Shortcut.registerShortcut("tools:ungluerelation", tr("Tool: {0}", tr("UnGlue Relations")), KeyEvent.VK_G, Shortcut.ALT_SHIFT),
44                true);
45        putValue("help", ht("/Action/UnGlueRelation"));
46    }
47
48    /**
49     * Called when the action is executed.
50     */
51    @Override
52    public void actionPerformed(ActionEvent e) {
53
54        LinkedList<Command> cmds = new LinkedList<>();
55        List<OsmPrimitive> newPrimitives = new LinkedList<>();
56        DataSet ds = getLayerManager().getEditDataSet();
57        Collection<OsmPrimitive> selection = ds.getSelected();
58
59        for (OsmPrimitive p : selection) {
60            boolean first = true;
61            for (Relation relation : Utils.filteredCollection(p.getReferrers(), Relation.class)) {
62                if (relation.isDeleted()) {
63                    continue;
64                }
65                if (!first) {
66                    OsmPrimitive newp;
67                    switch(p.getType()) {
68                    case NODE: newp = new Node((Node) p, true); break;
69                    case WAY: newp = new Way((Way) p, true); break;
70                    case RELATION: newp = new Relation((Relation) p, true); break;
71                    default: throw new AssertionError();
72                    }
73                    newPrimitives.add(newp);
74                    cmds.add(new AddCommand(ds, newp));
75                    cmds.add(new ChangeRelationMemberCommand(ds, relation, p, newp));
76                } else {
77                    first = false;
78                }
79            }
80        }
81
82        if (newPrimitives.isEmpty()) {
83            // error message nothing to do
84        } else {
85            UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Unglued Relations"), cmds));
86            //Set selection all primiteves (new and old)
87            newPrimitives.addAll(selection);
88            ds.setSelected(newPrimitives);
89            MainApplication.getMap().mapView.repaint();
90        }
91    }
92
93    @Override
94    protected void updateEnabledState() {
95        updateEnabledStateOnCurrentSelection();
96    }
97
98    @Override
99    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
100        setEnabled(selection != null && !selection.isEmpty());
101    }
102}
Note: See TracBrowser for help on using the repository browser.