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.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
18 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
19 | import org.openstreetmap.josm.command.Command;
|
---|
20 | import org.openstreetmap.josm.command.AddCommand;
|
---|
21 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
22 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
23 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
24 |
|
---|
25 | import javax.swing.AbstractAction;
|
---|
26 |
|
---|
27 | class MergePointsAction extends AbstractAction {
|
---|
28 | public MergePointsAction() {
|
---|
29 | super("Merge Points");
|
---|
30 | }
|
---|
31 | public void actionPerformed(ActionEvent e) {
|
---|
32 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
33 | Collection<OsmPrimitive> nodes = new ArrayList<OsmPrimitive>();
|
---|
34 | Node target = null;
|
---|
35 | for (OsmPrimitive osm : sel)
|
---|
36 | if (osm instanceof Node)
|
---|
37 | nodes.add((Node)osm);
|
---|
38 | if (nodes.size() < 2) {
|
---|
39 | javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Must select at least two nodes."));
|
---|
40 | return;
|
---|
41 | }
|
---|
42 | for ( OsmPrimitive o : nodes )
|
---|
43 | {
|
---|
44 | Node n = (Node)o;
|
---|
45 | if( target == null || target.id == 0 )
|
---|
46 | {
|
---|
47 | target = n;
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 | if( n.id == 0 )
|
---|
51 | continue;
|
---|
52 | if( n.id < target.id )
|
---|
53 | target = n;
|
---|
54 | }
|
---|
55 | // System.out.println( "Selected: "+target.toString() );
|
---|
56 | nodes.remove(target);
|
---|
57 |
|
---|
58 | // target is what we're merging into
|
---|
59 | // nodes is the list of nodes to be removed
|
---|
60 | // Since some segment may disappear, we need to track those too
|
---|
61 | Collection<OsmPrimitive> seglist = new ArrayList<OsmPrimitive>();
|
---|
62 |
|
---|
63 | // Now do the merging
|
---|
64 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
65 | for (final Segment s : Main.ds.segments)
|
---|
66 | {
|
---|
67 | if( s.deleted || s.incomplete )
|
---|
68 | continue;
|
---|
69 | if( !nodes.contains( s.from ) && !nodes.contains( s.to ) )
|
---|
70 | continue;
|
---|
71 |
|
---|
72 | Segment newseg = new Segment(s);
|
---|
73 | if( nodes.contains( s.from ) )
|
---|
74 | newseg.from = target;
|
---|
75 | if( nodes.contains( s.to ) )
|
---|
76 | newseg.to = target;
|
---|
77 |
|
---|
78 | // Is this node now a NULL node?
|
---|
79 | if( newseg.from == newseg.to )
|
---|
80 | seglist.add(s);
|
---|
81 | else
|
---|
82 | cmds.add(new ChangeCommand(s,newseg));
|
---|
83 | }
|
---|
84 | if( seglist.size() > 0 ) // Some segments to be deleted?
|
---|
85 | {
|
---|
86 | // We really want to delete this, but we must check if it is part of a way first
|
---|
87 | for (final Way w : Main.ds.ways)
|
---|
88 | {
|
---|
89 | Way newway = null;
|
---|
90 | if( w.deleted )
|
---|
91 | continue;
|
---|
92 | for (final OsmPrimitive o : seglist )
|
---|
93 | {
|
---|
94 | Segment s = (Segment)o;
|
---|
95 | if( w.segments.contains(s) )
|
---|
96 | {
|
---|
97 | if( newway == null )
|
---|
98 | newway = new Way(w);
|
---|
99 | newway.segments.remove(s);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | if( newway != null ) // Made changes?
|
---|
103 | {
|
---|
104 | // If no segments left, delete the way
|
---|
105 | if( newway.segments.size() == 0 )
|
---|
106 | cmds.add(makeDeleteCommand(w));
|
---|
107 | else
|
---|
108 | cmds.add(new ChangeCommand(w,newway));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | cmds.add(new DeleteCommand(seglist));
|
---|
112 | }
|
---|
113 |
|
---|
114 | cmds.add(new DeleteCommand(nodes));
|
---|
115 | Main.main.editLayer().add(new SequenceCommand(tr("Merge Nodes"), cmds));
|
---|
116 | Main.map.repaint();
|
---|
117 | }
|
---|
118 | private DeleteCommand makeDeleteCommand(OsmPrimitive obj)
|
---|
119 | {
|
---|
120 | return new DeleteCommand(Arrays.asList(new OsmPrimitive[]{obj}));
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|