| | 1 | // License: GPL v2 or later. Copyright 2010 by Kalle Lampila and others |
| | 2 | // See LICENSE file for details. |
| | 3 | package org.openstreetmap.josm.actions; |
| | 4 | |
| | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
| | 6 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 7 | import static org.openstreetmap.josm.tools.I18n.trn; |
| | 8 | |
| | 9 | import java.awt.event.KeyEvent; |
| | 10 | import java.awt.event.ActionEvent; |
| | 11 | |
| | 12 | import java.util.Collection; |
| | 13 | import java.util.Collections; |
| | 14 | import java.util.LinkedList; |
| | 15 | import java.util.List; |
| | 16 | |
| | 17 | import org.openstreetmap.josm.Main; |
| | 18 | import org.openstreetmap.josm.tools.Shortcut; |
| | 19 | import org.openstreetmap.josm.command.AddCommand; |
| | 20 | import org.openstreetmap.josm.command.ChangeCommand; |
| | 21 | import org.openstreetmap.josm.command.Command; |
| | 22 | import org.openstreetmap.josm.command.SequenceCommand; |
| | 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 24 | import org.openstreetmap.josm.data.osm.Relation; |
| | 25 | import org.openstreetmap.josm.data.osm.RelationMember; |
| | 26 | |
| | 27 | /** |
| | 28 | * Duplicate nodes, ways and relations that are used by multiple relations. |
| | 29 | * |
| | 30 | * Resulting nodes, ways and relations are identical as the orginals. |
| | 31 | * |
| | 32 | * @author Kalle Lampila |
| | 33 | * |
| | 34 | */ |
| | 35 | public class UnGlueRelationAction extends JosmAction { |
| | 36 | |
| | 37 | /** |
| | 38 | * Create a new UnGlueRelationAction. |
| | 39 | */ |
| | 40 | public UnGlueRelationAction() { |
| | 41 | super(tr("UnGlue Relation"), "ungluerelations", tr("Duplicate nodes, ways and relations that are used by multiple relations."), |
| | 42 | Shortcut.registerShortcut("tools:ungluerelation", tr("Tool: {0}", tr("UnGlue Relations")), KeyEvent.VK_G, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT ), true); |
| | 43 | putValue("help", ht("/Action/UnGlueRelation")); |
| | 44 | } |
| | 45 | |
| | 46 | /** |
| | 47 | * Called when the action is executed. |
| | 48 | */ |
| | 49 | public void actionPerformed(ActionEvent e) { |
| | 50 | |
| | 51 | LinkedList<Command> cmds = new LinkedList<Command>(); |
| | 52 | List<OsmPrimitive> newPrimitives = new LinkedList<OsmPrimitive>(); |
| | 53 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); |
| | 54 | |
| | 55 | for (OsmPrimitive p : selection) { |
| | 56 | boolean first = true; |
| | 57 | for (Relation relation : OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class)) { |
| | 58 | if (relation.isDeleted()) { |
| | 59 | continue; |
| | 60 | } |
| | 61 | if (!first) { |
| | 62 | OsmPrimitive newp = p.makeClone(true); |
| | 63 | newPrimitives.add(newp); |
| | 64 | cmds.add(new AddCommand(newp)); |
| | 65 | cmds.add(new ChangeCommand(relation, changeRelationMember(relation, p, newp))); |
| | 66 | } else { |
| | 67 | first = false; |
| | 68 | } |
| | 69 | } |
| | 70 | } |
| | 71 | |
| | 72 | if (newPrimitives.isEmpty() ) { |
| | 73 | // error message nothing to do |
| | 74 | } |
| | 75 | else { |
| | 76 | Main.main.undoRedo.add(new SequenceCommand(tr("Unglued Relations"), cmds)); |
| | 77 | //Set selection all primiteves (new and old) |
| | 78 | newPrimitives.addAll(selection); |
| | 79 | getCurrentDataSet().setSelected(newPrimitives); |
| | 80 | Main.map.mapView.repaint(); |
| | 81 | } |
| | 82 | } |
| | 83 | |
| | 84 | /** |
| | 85 | * Change member in relation to another one |
| | 86 | * @param relation |
| | 87 | * @param orginalMember member to change |
| | 88 | * @param newMember |
| | 89 | * @return new relation were change is made |
| | 90 | */ |
| | 91 | private Relation changeRelationMember(Relation relation, OsmPrimitive orginalMember, OsmPrimitive newMember) { |
| | 92 | LinkedList<RelationMember> newrms = new LinkedList<RelationMember>(); |
| | 93 | for (RelationMember rm : relation.getMembers()) { |
| | 94 | if (rm.getMember() == orginalMember) { |
| | 95 | newrms.add(new RelationMember(rm.getRole(),newMember)); |
| | 96 | } else { |
| | 97 | newrms.add(rm); |
| | 98 | } |
| | 99 | } |
| | 100 | Relation newRelation = new Relation(relation); |
| | 101 | newRelation.setMembers(newrms); |
| | 102 | return newRelation; |
| | 103 | } |
| | 104 | |
| | 105 | @Override |
| | 106 | protected void updateEnabledState() { |
| | 107 | if (getCurrentDataSet() == null) { |
| | 108 | setEnabled(false); |
| | 109 | } else { |
| | 110 | updateEnabledState(getCurrentDataSet().getSelected()); |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | @Override |
| | 115 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| | 116 | setEnabled(selection != null && !selection.isEmpty()); |
| | 117 | } |
| | 118 | |
| | 119 | } |