1 | package org.openstreetmap.josm.actions.mapmode;
|
---|
2 |
|
---|
3 | import java.awt.Rectangle;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedList;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 | import org.openstreetmap.josm.data.osm.Track;
|
---|
11 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
12 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
13 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Add a new track from all selected line segments.
|
---|
17 | *
|
---|
18 | * If there is a selection when the mode is entered, all line segments in this
|
---|
19 | * selection form a new track, except the user holds down Shift.
|
---|
20 | *
|
---|
21 | * The user can click on a line segment. If he holds down Shift, no track is
|
---|
22 | * created yet. If he holds down Alt, the whole track is considered instead of
|
---|
23 | * the clicked line segment. If the user holds down Ctrl, no track is created
|
---|
24 | * and the clicked line segment get removed from the list.
|
---|
25 | *
|
---|
26 | * Also, the user may select a rectangle as in selection mode. No node, area or
|
---|
27 | * track can be selected this way.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | *
|
---|
31 | */
|
---|
32 | public class AddTrackAction extends MapMode implements SelectionEnded {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * The selection manager for this action, keeping track of all selections.
|
---|
36 | */
|
---|
37 | SelectionManager selectionManager;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create a new AddTrackAction.
|
---|
41 | * @param mapFrame The MapFrame this action belongs to.
|
---|
42 | */
|
---|
43 | public AddTrackAction(MapFrame mapFrame) {
|
---|
44 | super("Add Track", "addtrack", "Combine line segments to a new track.", KeyEvent.VK_T, mapFrame);
|
---|
45 | this.selectionManager = new SelectionManager(this, false, mv);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void registerListener() {
|
---|
50 | super.registerListener();
|
---|
51 | selectionManager.register(mv);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void unregisterListener() {
|
---|
56 | super.unregisterListener();
|
---|
57 | selectionManager.unregister(mv);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * If Shift is pressed, only add the selected line segments to the selection.
|
---|
62 | * If Ctrl is pressed, only remove the selected line segments from the selection.
|
---|
63 | * If both, Shift and Ctrl is pressed, do nothing.
|
---|
64 | *
|
---|
65 | * Else, form a new track out of all line segments in the selection and
|
---|
66 | * clear the selection afterwards.
|
---|
67 | *
|
---|
68 | * If Alt is pressed, consider all linesegments of all tracks a selected
|
---|
69 | * line segment is part of. Also consider all line segments that cross the
|
---|
70 | * selecting rectangle, instead only those that are fully within.
|
---|
71 | *
|
---|
72 | */
|
---|
73 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
74 | if (shift && ctrl)
|
---|
75 | return; // not allowed together
|
---|
76 |
|
---|
77 | if (!ctrl && !shift)
|
---|
78 | ds.clearSelection(); // new selection will replace the old.
|
---|
79 |
|
---|
80 | Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
|
---|
81 | for (OsmPrimitive osm : selectionList)
|
---|
82 | osm.setSelected(!ctrl, ds);
|
---|
83 |
|
---|
84 | mv.repaint(); // from now on, the map has to be repainted.
|
---|
85 |
|
---|
86 | if (ctrl || shift)
|
---|
87 | return; // no new track yet.
|
---|
88 |
|
---|
89 | Collection<OsmPrimitive> selection = ds.getSelected();
|
---|
90 | if (selection.isEmpty())
|
---|
91 | return;
|
---|
92 |
|
---|
93 | // form a new track
|
---|
94 | LinkedList<LineSegment> lineSegments = new LinkedList<LineSegment>();
|
---|
95 | for (OsmPrimitive osm : selection) {
|
---|
96 | if (osm instanceof Track)
|
---|
97 | lineSegments.addAll(((Track)osm).segments());
|
---|
98 | else if (osm instanceof LineSegment)
|
---|
99 | lineSegments.add((LineSegment)osm);
|
---|
100 | }
|
---|
101 | Track t = new Track();
|
---|
102 | for (LineSegment ls : lineSegments)
|
---|
103 | ds.assignPendingLineSegment(ls, t, true);
|
---|
104 | ds.addTrack(t);
|
---|
105 | ds.clearSelection();
|
---|
106 | }
|
---|
107 | }
|
---|