source: josm/trunk/src/org/openstreetmap/josm/data/osm/DataSet.java@ 627

Last change on this file since 627 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.LinkedList;
9import java.util.List;
10
11import 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 */
23public 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 ways (Streets etc.) in the DataSet.
34 *
35 * The way nodes are stored only in the way list.
36 */
37 public Collection<Way> ways = new LinkedList<Way>();
38
39 /**
40 * All relations/relationships
41 */
42 public Collection<Relation> relations = new LinkedList<Relation>();
43
44 /**
45 * All data sources of this DataSet.
46 */
47 public Collection<DataSource> dataSources = new LinkedList<DataSource>();
48
49 /**
50 * A list of listeners to selection changed events. The list is static,
51 * as listeners register themself for any dataset selection changes that
52 * occour, regardless of the current active dataset. (However, the
53 * selection does only change in the active layer)
54 */
55 public static Collection<SelectionChangedListener> selListeners = new LinkedList<SelectionChangedListener>();
56
57 /**
58 * @return A collection containing all primitives of the dataset. The
59 * data is ordered after: first come nodes, then ways, then relations.
60 * Ordering in between the categories is not guaranteed.
61 */
62 public List<OsmPrimitive> allPrimitives() {
63 List<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
64 o.addAll(nodes);
65 o.addAll(ways);
66 o.addAll(relations);
67 return o;
68 }
69
70 /**
71 * @return A collection containing all not-deleted primitives (except keys).
72 */
73 public Collection<OsmPrimitive> allNonDeletedPrimitives() {
74 Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
75 for (OsmPrimitive osm : allPrimitives())
76 if (!osm.deleted)
77 o.add(osm);
78 return o;
79 }
80
81 public void addPrimitive(OsmPrimitive osm) {
82 if (osm instanceof Node) {
83 nodes.add((Node) osm);
84 } else if (osm instanceof Way) {
85 ways.add((Way) osm);
86 } else if (osm instanceof Relation) {
87 relations.add((Relation) osm);
88 }
89 }
90
91 /**
92 * Remove the selection of the whole dataset.
93 * @deprecated Use setSelected() instead.
94 */
95 @Deprecated
96 public void clearSelection() {
97 clearSelection(nodes);
98 clearSelection(ways);
99 clearSelection(relations);
100 Collection<OsmPrimitive> sel = Collections.emptyList();
101 fireSelectionChanged(sel);
102 }
103
104 /**
105 * Return a list of all selected objects. Even keys are returned.
106 * @return List of all selected objects.
107 */
108 public Collection<OsmPrimitive> getSelected() {
109 Collection<OsmPrimitive> sel = getSelected(nodes);
110 sel.addAll(getSelected(ways));
111 sel.addAll(getSelected(relations));
112 return sel;
113 }
114
115 /**
116 * Return selected nodes.
117 */
118 public Collection<OsmPrimitive> getSelectedNodes() {
119 return getSelected(nodes);
120 }
121
122 /**
123 * Return selected ways.
124 */
125 public Collection<OsmPrimitive> getSelectedWays() {
126 return getSelected(ways);
127 }
128
129 /**
130 * Return selected relations.
131 */
132 public Collection<OsmPrimitive> getSelectedRelations() {
133 return getSelected(relations);
134 }
135
136 public void setSelected(Collection<? extends OsmPrimitive> selection) {
137 clearSelection(nodes);
138 clearSelection(ways);
139 clearSelection(relations);
140 for (OsmPrimitive osm : selection)
141 osm.selected = true;
142 fireSelectionChanged(selection);
143 }
144
145 public void setSelected(OsmPrimitive... osm) {
146 if (osm.length == 1 && osm[0] == null) {
147 setSelected();
148 return;
149 }
150 clearSelection(nodes);
151 clearSelection(ways);
152 clearSelection(relations);
153 for (OsmPrimitive o : osm)
154 if (o != null)
155 o.selected = true;
156 fireSelectionChanged(Arrays.asList(osm));
157 }
158
159 /**
160 * Remove the selection from every value in the collection.
161 * @param list The collection to remove the selection from.
162 */
163 private void clearSelection(Collection<? extends OsmPrimitive> list) {
164 if (list == null)
165 return;
166 for (OsmPrimitive osm : list)
167 osm.selected = false;
168 }
169
170 /**
171 * Return all selected items in the collection.
172 * @param list The collection from which the selected items are returned.
173 */
174 private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) {
175 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
176 if (list == null)
177 return sel;
178 for (OsmPrimitive osm : list)
179 if (osm.selected && !osm.deleted)
180 sel.add(osm);
181 return sel;
182 }
183
184 /**
185 * Remember to fire an selection changed event. A call to this will not fire
186 * the event immediately. For more, @see SelectionChangedListener
187 */
188 public static void fireSelectionChanged(Collection<? extends OsmPrimitive> sel) {
189 for (SelectionChangedListener l : selListeners)
190 l.selectionChanged(sel);
191 }
192
193 @Override public DataSet clone() {
194 DataSet ds = new DataSet();
195 for (Node n : nodes)
196 ds.nodes.add(new Node(n));
197 for (Way w : ways)
198 ds.ways.add(new Way(w));
199 for (Relation e : relations)
200 ds.relations.add(new Relation(e));
201 for (DataSource source : dataSources)
202 ds.dataSources.add(new DataSource(source.bounds, source.origin));
203 return ds;
204 }
205}
Note: See TracBrowser for help on using the repository browser.