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