1 | package org.openstreetmap.josm.actions.mapmode;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.util.LinkedList;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.command.AddCommand;
|
---|
13 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Add a new way from all selected segments.
|
---|
20 | *
|
---|
21 | * If there is a selection when the mode is entered, all segments in this
|
---|
22 | * selection form a new way, except the user holds down Shift.
|
---|
23 | *
|
---|
24 | * The user can click on a segment. If he holds down Shift, no way is
|
---|
25 | * created yet. If he holds down Alt, the whole way is considered instead of
|
---|
26 | * the clicked segment. If the user holds down Ctrl, no way is created
|
---|
27 | * and the clicked segment get removed from the list.
|
---|
28 | *
|
---|
29 | * Also, the user may select a rectangle as in selection mode. No node, area or
|
---|
30 | * way can be selected this way.
|
---|
31 | *
|
---|
32 | * @author imi
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class AddWayAction extends MapMode {
|
---|
36 |
|
---|
37 | private MapMode followMode;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create a new AddWayAction.
|
---|
41 | * @param mapFrame The MapFrame this action belongs to.
|
---|
42 | * @param followMode The mode to go into when finished creating a way.
|
---|
43 | */
|
---|
44 | public AddWayAction(MapFrame mapFrame, MapMode followMode) {
|
---|
45 | super("Add Way", "addway", "Combine selected segments to a new way.", "W", KeyEvent.VK_W, mapFrame);
|
---|
46 | this.followMode = followMode;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public void actionPerformed(ActionEvent e) {
|
---|
50 | makeWay();
|
---|
51 | super.actionPerformed(e);
|
---|
52 | mapFrame.selectMapMode(followMode);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Just make a way of all selected items.
|
---|
57 | */
|
---|
58 | private void makeWay() {
|
---|
59 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
60 | if (selection.isEmpty())
|
---|
61 | return;
|
---|
62 |
|
---|
63 | // form a new way
|
---|
64 | LinkedList<Segment> segments = new LinkedList<Segment>();
|
---|
65 | int numberOfSelectedWays = 0;
|
---|
66 | for (OsmPrimitive osm : selection) {
|
---|
67 | if (osm instanceof Way)
|
---|
68 | numberOfSelectedWays++;
|
---|
69 | else if (osm instanceof Segment)
|
---|
70 | segments.add((Segment)osm);
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (numberOfSelectedWays > 0) {
|
---|
74 | String ways = "way" + (numberOfSelectedWays==1?" has":"s have");
|
---|
75 | int answer = JOptionPane.showConfirmDialog(Main.main, numberOfSelectedWays+" "+ways+" been selected.\n" +
|
---|
76 | "Do you wish to select all segments belonging to the "+ways+" instead?");
|
---|
77 | if (answer == JOptionPane.CANCEL_OPTION)
|
---|
78 | return;
|
---|
79 | if (answer == JOptionPane.YES_OPTION) {
|
---|
80 | for (OsmPrimitive osm : selection)
|
---|
81 | if (osm instanceof Way)
|
---|
82 | segments.addAll(((Way)osm).segments);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (segments.isEmpty())
|
---|
87 | return;
|
---|
88 |
|
---|
89 | // sort the segments in best possible order. This is done by:
|
---|
90 | // 0 if no elements in list, quit
|
---|
91 | // 1 taking the first ls as pivot, remove it from list
|
---|
92 | // 2 searching for a connection at from or to of pivot
|
---|
93 | // 3 if found, attach it, remove it from list, goto 2
|
---|
94 | // 4 if not found, save the pivot-string and goto 0
|
---|
95 | LinkedList<Segment> sortedSegments = new LinkedList<Segment>();
|
---|
96 | while (!segments.isEmpty()) {
|
---|
97 | LinkedList<Segment> pivotList = new LinkedList<Segment>();
|
---|
98 | pivotList.add(segments.getFirst());
|
---|
99 | segments.removeFirst();
|
---|
100 | for (boolean found = true; found;) {
|
---|
101 | found = false;
|
---|
102 | for (Iterator<Segment> it = segments.iterator(); it.hasNext();) {
|
---|
103 | Segment ls = it.next();
|
---|
104 | if (ls.incomplete)
|
---|
105 | continue; // incomplete segments are never added to a new way
|
---|
106 | if (ls.from == pivotList.getLast().to) {
|
---|
107 | pivotList.addLast(ls);
|
---|
108 | it.remove();
|
---|
109 | found = true;
|
---|
110 | } else if (ls.to == pivotList.getFirst().from) {
|
---|
111 | pivotList.addFirst(ls);
|
---|
112 | it.remove();
|
---|
113 | found = true;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | sortedSegments.addAll(pivotList);
|
---|
118 | }
|
---|
119 |
|
---|
120 | Way w = new Way();
|
---|
121 | w.segments.addAll(sortedSegments);
|
---|
122 | mv.editLayer().add(new AddCommand(w));
|
---|
123 | Main.ds.clearSelection();
|
---|
124 | mv.repaint();
|
---|
125 | }
|
---|
126 | }
|
---|