1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.LinkedList;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * DataSet is the data behind the application. It can consists of only a few
|
---|
12 | * points up to the whole osm database. DataSet's can be merged together,
|
---|
13 | * saved, (up/down/disk)loaded etc.
|
---|
14 | *
|
---|
15 | * Note, that DataSet is not an osm-primitive and so has no key association
|
---|
16 | * but a few members to store some information.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public class DataSet {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * All nodes goes here, even when included in other data (ways etc).
|
---|
24 | * This enables the instant conversion of the whole DataSet by iterating over
|
---|
25 | * this data structure.
|
---|
26 | */
|
---|
27 | public Collection<Node> nodes = new LinkedList<Node>();
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * All segments goes here, even when they are in a way.
|
---|
31 | */
|
---|
32 | public Collection<Segment> segments = new LinkedList<Segment>();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * All ways (Streets etc.) in the DataSet.
|
---|
36 | *
|
---|
37 | * The nodes of the way segments of this way must be objects from
|
---|
38 | * the nodes list, however the way segments are stored only in the
|
---|
39 | * way list.
|
---|
40 | */
|
---|
41 | public Collection<Way> ways = new LinkedList<Way>();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * A list of listeners to selection changed events.
|
---|
45 | */
|
---|
46 | transient Collection<SelectionChangedListener> listeners = new LinkedList<SelectionChangedListener>();
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @return A collection containing all primitives (except keys) of the
|
---|
50 | * dataset.
|
---|
51 | */
|
---|
52 | public Collection<OsmPrimitive> allPrimitives() {
|
---|
53 | Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
54 | o.addAll(nodes);
|
---|
55 | o.addAll(segments);
|
---|
56 | o.addAll(ways);
|
---|
57 | return o;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * @return A collection containing all not-deleted primitives (except keys).
|
---|
62 | */
|
---|
63 | public Collection<OsmPrimitive> allNonDeletedPrimitives() {
|
---|
64 | Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
65 | for (OsmPrimitive osm : allPrimitives())
|
---|
66 | if (!osm.deleted)
|
---|
67 | o.add(osm);
|
---|
68 | return o;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Remove the selection of the whole dataset.
|
---|
73 | */
|
---|
74 | public void clearSelection() {
|
---|
75 | clearSelection(nodes);
|
---|
76 | clearSelection(segments);
|
---|
77 | clearSelection(ways);
|
---|
78 | Collection<OsmPrimitive> sel = Collections.emptyList();
|
---|
79 | fireSelectionChanged(sel);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Return a list of all selected objects. Even keys are returned.
|
---|
84 | * @return List of all selected objects.
|
---|
85 | */
|
---|
86 | public Collection<OsmPrimitive> getSelected() {
|
---|
87 | Collection<OsmPrimitive> sel = getSelected(nodes);
|
---|
88 | sel.addAll(getSelected(segments));
|
---|
89 | sel.addAll(getSelected(ways));
|
---|
90 | return sel;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void setSelected(Collection<OsmPrimitive> selection) {
|
---|
94 | clearSelection();
|
---|
95 | for (OsmPrimitive osm : selection)
|
---|
96 | osm.selected = true;
|
---|
97 | fireSelectionChanged(selection);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Remove the selection from every value in the collection.
|
---|
102 | * @param list The collection to remove the selection from.
|
---|
103 | */
|
---|
104 | private void clearSelection(Collection<? extends OsmPrimitive> list) {
|
---|
105 | if (list == null)
|
---|
106 | return;
|
---|
107 | for (OsmPrimitive osm : list)
|
---|
108 | osm.selected = false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Return all selected items in the collection.
|
---|
113 | * @param list The collection from which the selected items are returned.
|
---|
114 | */
|
---|
115 | private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) {
|
---|
116 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
117 | if (list == null)
|
---|
118 | return sel;
|
---|
119 | for (OsmPrimitive osm : list)
|
---|
120 | if (osm.selected && !osm.deleted)
|
---|
121 | sel.add(osm);
|
---|
122 | return sel;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Remember to fire an selection changed event. A call to this will not fire
|
---|
127 | * the event immediately. For more, @see SelectionChangedListener
|
---|
128 | */
|
---|
129 | private void fireSelectionChanged(Collection<OsmPrimitive> sel) {
|
---|
130 | for (SelectionChangedListener l : listeners)
|
---|
131 | l.selectionChanged(sel);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Add a listener to the selection changed listener list. If <code>null</code>
|
---|
136 | * is passed, nothing happens.
|
---|
137 | * @param listener The listener to add to the list.
|
---|
138 | */
|
---|
139 | public void addSelectionChangedListener(SelectionChangedListener listener) {
|
---|
140 | if (listener != null)
|
---|
141 | listeners.add(listener);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Remove a listener from the selection changed listener list.
|
---|
146 | * If <code>null</code> is passed, nothing happens.
|
---|
147 | * @param listener The listener to remove from the list.
|
---|
148 | */
|
---|
149 | public void removeSelectionChangedListener(SelectionChangedListener listener) {
|
---|
150 | if (listener != null)
|
---|
151 | listeners.remove(listener);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void addAllSelectionListener(DataSet ds) {
|
---|
155 | listeners.addAll(ds.listeners);
|
---|
156 | }
|
---|
157 | }
|
---|