[5075] | 1 | package UtilsPlugin;
|
---|
| 2 |
|
---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.Arrays;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.Collection;
|
---|
| 9 |
|
---|
| 10 | import java.awt.event.ActionEvent;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.Main;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 14 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 16 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
| 17 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
| 18 | import org.openstreetmap.josm.command.Command;
|
---|
| 19 | import org.openstreetmap.josm.command.AddCommand;
|
---|
| 20 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
| 21 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 22 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 24 |
|
---|
| 25 | import javax.swing.AbstractAction;
|
---|
| 26 |
|
---|
| 27 | class MergeWaysAction extends AbstractAction {
|
---|
| 28 | public MergeWaysAction() {
|
---|
| 29 | super("Merge Ways");
|
---|
| 30 | }
|
---|
| 31 | public void actionPerformed(ActionEvent e) {
|
---|
| 32 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
| 33 | Collection<OsmPrimitive> ways = new ArrayList<OsmPrimitive>();
|
---|
| 34 | Way target = null;
|
---|
| 35 | for (OsmPrimitive osm : sel)
|
---|
| 36 | if (osm instanceof Way)
|
---|
| 37 | ways.add(osm);
|
---|
| 38 | if (ways.size() < 2) {
|
---|
| 39 | javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Must select at least two ways."));
|
---|
| 40 | return;
|
---|
| 41 | }
|
---|
| 42 | for ( OsmPrimitive o : ways )
|
---|
| 43 | {
|
---|
| 44 | Way w = (Way)o;
|
---|
| 45 | if( target == null || target.id == 0 )
|
---|
| 46 | {
|
---|
| 47 | target = w;
|
---|
| 48 | continue;
|
---|
| 49 | }
|
---|
| 50 | if( w.id == 0 )
|
---|
| 51 | continue;
|
---|
| 52 | if( w.id < target.id )
|
---|
| 53 | target = w;
|
---|
| 54 | }
|
---|
| 55 | // System.out.println( "Selected: "+target.toString() );
|
---|
| 56 | ways.remove(target);
|
---|
| 57 |
|
---|
| 58 | // target is what we're merging into
|
---|
| 59 | // ways is the list of ways to be removed
|
---|
| 60 |
|
---|
| 61 | // Now do the merging
|
---|
| 62 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
| 63 | Way newway = new Way(target);
|
---|
| 64 | for (final OsmPrimitive o : ways)
|
---|
| 65 | {
|
---|
| 66 | Way w = (Way)o;
|
---|
| 67 | if( w.deleted || w.isIncomplete() )
|
---|
| 68 | {
|
---|
| 69 | javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Ways must exist and be complete."));
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 | for ( String key : w.keySet() )
|
---|
| 73 | {
|
---|
| 74 | if( newway.keys.containsKey(key) && !newway.get(key).equals(w.get(key)) )
|
---|
| 75 | {
|
---|
| 76 | javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Ways have conflicting key: "+key+"["+newway.get(key)+","+w.get(key)+"]"));
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 | newway.put( key, w.get(key) );
|
---|
| 80 | }
|
---|
| 81 | for (final Segment s : w.segments)
|
---|
| 82 | {
|
---|
| 83 | if( !newway.segments.contains( s ) )
|
---|
| 84 | newway.segments.add( s );
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | cmds.add(new ChangeCommand(target, newway));
|
---|
| 89 | cmds.add(new DeleteCommand(ways));
|
---|
| 90 | Main.main.editLayer().add(new SequenceCommand(tr("Merge Ways"), cmds));
|
---|
| 91 | Main.map.repaint();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|