1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Iterator;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
23 |
|
---|
24 | public class ReorderAction extends JosmAction {
|
---|
25 |
|
---|
26 | public ReorderAction() {
|
---|
27 | super(tr("Reorder Segments"), "reorder", tr("Try to reorder segments of a way so that they are in a line. May try to flip segments around to match a line."), KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK, true);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This method first sorts all the segments in a way, then makes sure that all
|
---|
32 | * the segments are facing the same direction as the first one.
|
---|
33 | */
|
---|
34 | public void actionPerformed(ActionEvent e) {
|
---|
35 | Collection<Way> ways = new LinkedList<Way>();
|
---|
36 | for (OsmPrimitive osm : Main.ds.getSelected())
|
---|
37 | if (osm instanceof Way)
|
---|
38 | ways.add((Way)osm);
|
---|
39 |
|
---|
40 | if (ways.size() < 1) {
|
---|
41 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way."));
|
---|
42 | return;
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (ways.size() > 1) {
|
---|
46 | int answer = JOptionPane.showConfirmDialog(Main.parent,
|
---|
47 | trn(null, "You selected more than one way. Reorder the segments of {0} ways?", ways.size(), ways.size()),
|
---|
48 | tr("Reorder segments"), JOptionPane.OK_CANCEL_OPTION);
|
---|
49 | if (answer != JOptionPane.OK_OPTION)
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | boolean doneSomething = false;
|
---|
53 | for (Way way : ways) {
|
---|
54 | if (!way.isIncomplete() && way.segments.size() > 1)
|
---|
55 | {
|
---|
56 | doneSomething = true;
|
---|
57 | Command c = reorderWay(way);
|
---|
58 |
|
---|
59 | if( c != null )
|
---|
60 | Main.main.editLayer().add( c );
|
---|
61 | }
|
---|
62 | }
|
---|
63 | if (!doneSomething) {
|
---|
64 | JOptionPane.showMessageDialog(Main.parent,
|
---|
65 | trn("The selected way is incomplete or has only one segment.",
|
---|
66 | "None of the selected ways are complete and have more than one segment.",
|
---|
67 | ways.size()));
|
---|
68 | }
|
---|
69 | Main.map.repaint();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * This method first sorts all the segments in a way, then makes sure that all
|
---|
74 | * the segments are facing the same direction as the first one.
|
---|
75 | * @param way The way to reorder
|
---|
76 | * @return The command needed to reorder the way
|
---|
77 | */
|
---|
78 | public static Command reorderWay(Way way) {
|
---|
79 | final LinkedList<Segment> sel = new LinkedList<Segment>(sortSegments(new LinkedList<Segment>(way.segments), false));
|
---|
80 |
|
---|
81 | Collection<Command> c = new LinkedList<Command>();
|
---|
82 |
|
---|
83 | boolean direction = false;
|
---|
84 | // work out the "average" direction of the way, we use this to direct the rest of the segments
|
---|
85 | int dirCounter = 0;
|
---|
86 | for(int i = 0; i < sel.size() - 1; i++)
|
---|
87 | {
|
---|
88 | Segment firstSegment = sel.get(i);
|
---|
89 | Segment secondSegment = sel.get(i+1);
|
---|
90 | if ( firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to ) // direction = true when 'from' is the first node in the Way
|
---|
91 | dirCounter++;
|
---|
92 | else
|
---|
93 | dirCounter--;
|
---|
94 | }
|
---|
95 | if ( dirCounter <= 0 )
|
---|
96 | direction = false;
|
---|
97 | else
|
---|
98 | direction = true;
|
---|
99 |
|
---|
100 | Node lastNode = null;
|
---|
101 |
|
---|
102 | // we need to calculate what the first node in the way is, we work from there
|
---|
103 | Segment firstSegment = sel.getFirst();
|
---|
104 | Segment secondSegment = sel.get(1);
|
---|
105 | if (firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to)
|
---|
106 | lastNode = firstSegment.from;
|
---|
107 | else
|
---|
108 | lastNode = firstSegment.to;
|
---|
109 |
|
---|
110 | // go through each segment and flip them if required
|
---|
111 | for (Segment s : sel) {
|
---|
112 | Segment snew = new Segment(s);
|
---|
113 | boolean segDirection = s.from == lastNode;
|
---|
114 | // segDirection = true when the 'from' node occurs before the 'to' node in the Way
|
---|
115 | if (direction != segDirection)
|
---|
116 | {
|
---|
117 | // reverse the segment's direction
|
---|
118 | Node n = snew.from;
|
---|
119 | snew.from = snew.to;
|
---|
120 | snew.to = n;
|
---|
121 | c.add(new ChangeCommand(s, snew));
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (direction) // if its facing forwards,
|
---|
125 | lastNode = snew.to; // our next node is the 'to' one
|
---|
126 | else
|
---|
127 | lastNode = snew.from; // otherwise its the 'from' one
|
---|
128 | }
|
---|
129 |
|
---|
130 | LinkedList<Segment> segments = new LinkedList<Segment>();
|
---|
131 |
|
---|
132 | // Now we recreate the segment list, in the correct order of the direction
|
---|
133 | for (Segment s : sel)
|
---|
134 | if (!direction)
|
---|
135 | segments.addFirst(s);
|
---|
136 | else
|
---|
137 | segments.addLast(s);
|
---|
138 |
|
---|
139 | // Check if the new segment list is actually different from the old one
|
---|
140 | // before we go and add a change command for it
|
---|
141 | for(int i = 0; i < segments.size(); i++)
|
---|
142 | if (way.segments.get(i) != segments.get(i))
|
---|
143 | {
|
---|
144 | Way newWay = new Way(way);
|
---|
145 | newWay.segments.clear();
|
---|
146 | newWay.segments.addAll(segments);
|
---|
147 | c.add(new ChangeCommand(way, newWay));
|
---|
148 | break;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // Check we've got some change commands before we add a sequence command
|
---|
152 | if (c.size() != 0) {
|
---|
153 | NameVisitor v = new NameVisitor();
|
---|
154 | way.visit(v);
|
---|
155 | return new SequenceCommand(tr("Reorder segments for way {0}",v.name), c);
|
---|
156 | }
|
---|
157 | return null;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * This sort is based on the sort in the old ReorderAction, but it can work
|
---|
162 | * irresepective of the direction of the segments. This produces a sort
|
---|
163 | * that can be useful even if the segments are facing the wrong direction.
|
---|
164 | *
|
---|
165 | * @param segments list of segments to be sorted
|
---|
166 | * @param strict true if segment direction should be observed, false if not
|
---|
167 | */
|
---|
168 | public static LinkedList<Segment> sortSegments(LinkedList<Segment> segments, boolean strict) {
|
---|
169 |
|
---|
170 | LinkedList<Segment> sortedSegments = new LinkedList<Segment>();
|
---|
171 |
|
---|
172 | while (!segments.isEmpty()) {
|
---|
173 | LinkedList<Segment> pivotList = new LinkedList<Segment>();
|
---|
174 | pivotList.add(segments.getFirst());
|
---|
175 | segments.removeFirst();
|
---|
176 | boolean found;
|
---|
177 | do {
|
---|
178 | found = false;
|
---|
179 | for (Iterator<Segment> it = segments.iterator(); it.hasNext();) {
|
---|
180 | Segment ls = it.next();
|
---|
181 | if (ls.incomplete)
|
---|
182 | continue; // incomplete segments are never added to a new way
|
---|
183 | if (ls.from == pivotList.getLast().to || (!strict && (ls.to == pivotList.getLast().to || ls.from == pivotList.getLast().from || ls.to == pivotList.getLast().from))) {
|
---|
184 | pivotList.addLast(ls);
|
---|
185 | it.remove();
|
---|
186 | found = true;
|
---|
187 | } else if (ls.to == pivotList.getFirst().from || (!strict && (ls.from == pivotList.getFirst().from || ls.to == pivotList.getFirst().to || ls.from == pivotList.getFirst().to))) {
|
---|
188 | pivotList.addFirst(ls);
|
---|
189 | it.remove();
|
---|
190 | found = true;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | } while (found);
|
---|
194 | sortedSegments.addAll(pivotList);
|
---|
195 | }
|
---|
196 | return sortedSegments;
|
---|
197 | }
|
---|
198 | }
|
---|