1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashSet;
|
---|
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.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 |
|
---|
20 | public class ReorderAction extends JosmAction {
|
---|
21 |
|
---|
22 | public ReorderAction() {
|
---|
23 | 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);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void actionPerformed(ActionEvent e) {
|
---|
27 | Collection<Way> ways = new LinkedList<Way>();
|
---|
28 | for (OsmPrimitive osm : Main.ds.getSelected())
|
---|
29 | if (osm instanceof Way)
|
---|
30 | ways.add((Way)osm);
|
---|
31 | if (ways.size() < 1) {
|
---|
32 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way."));
|
---|
33 | return;
|
---|
34 | }
|
---|
35 | if (ways.size() > 1) {
|
---|
36 | int answer = JOptionPane.showConfirmDialog(Main.parent, tr("You selected more than one way. Reorder the segments of {0} ways?"), tr("Reorder segments"), JOptionPane.OK_CANCEL_OPTION);
|
---|
37 | if (answer != JOptionPane.OK_OPTION)
|
---|
38 | return;
|
---|
39 | }
|
---|
40 | for (Way way : ways) {
|
---|
41 | Way newWay = new Way(way);
|
---|
42 | newWay.segments.clear();
|
---|
43 | newWay.segments.addAll(sortSegments(new HashSet<Segment>(way.segments)));
|
---|
44 | Main.main.editLayer().add(new ChangeCommand(way, newWay));
|
---|
45 | Main.map.mapView.repaint();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * sort the segments in best possible order. This is done by:
|
---|
51 | * 0 if no elements in list, quit
|
---|
52 | * 1 taking the first ls as pivot, remove it from list
|
---|
53 | * 2 searching for a connection at from or to of pivot
|
---|
54 | * 3 if found, attach it, remove it from list, goto 2
|
---|
55 | * 4 if not found, save the pivot-string and goto 0
|
---|
56 | */
|
---|
57 | public static LinkedList<Segment> sortSegments(HashSet<Segment> segmentSet) {
|
---|
58 | LinkedList<Segment> sortedSegments = new LinkedList<Segment>();
|
---|
59 | LinkedList<Segment> segments = new LinkedList<Segment>(segmentSet);
|
---|
60 | while (!segments.isEmpty()) {
|
---|
61 | LinkedList<Segment> pivotList = new LinkedList<Segment>();
|
---|
62 | pivotList.add(segments.getFirst());
|
---|
63 | segments.removeFirst();
|
---|
64 | for (boolean found = true; found;) {
|
---|
65 | found = false;
|
---|
66 | for (Iterator<Segment> it = segments.iterator(); it.hasNext();) {
|
---|
67 | Segment ls = it.next();
|
---|
68 | if (ls.incomplete)
|
---|
69 | continue; // incomplete segments are never added to a new way
|
---|
70 | if (ls.from == pivotList.getLast().to) {
|
---|
71 | pivotList.addLast(ls);
|
---|
72 | it.remove();
|
---|
73 | found = true;
|
---|
74 | } else if (ls.to == pivotList.getFirst().from) {
|
---|
75 | pivotList.addFirst(ls);
|
---|
76 | it.remove();
|
---|
77 | found = true;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | sortedSegments.addAll(pivotList);
|
---|
82 | }
|
---|
83 | return sortedSegments;
|
---|
84 | }
|
---|
85 | }
|
---|