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

Last change on this file since 729 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 5.6 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 themselves for any dataset selection changes that
52 * occur, 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 {@link #setSelected(Collection) setSelected}
94 * instead.
95 */
96 @Deprecated
97 public void clearSelection() {
98 clearSelection(nodes);
99 clearSelection(ways);
100 clearSelection(relations);
101 Collection<OsmPrimitive> sel = Collections.emptyList();
102 fireSelectionChanged(sel);
103 }
104
105 /**
106 * Return a list of all selected objects. Even keys are returned.
107 * @return List of all selected objects.
108 */
109 public Collection<OsmPrimitive> getSelected() {
110 Collection<OsmPrimitive> sel = getSelected(nodes);
111 sel.addAll(getSelected(ways));
112 sel.addAll(getSelected(relations));
113 return sel;
114 }
115
116 /**
117 * Return selected nodes.
118 */
119 public Collection<OsmPrimitive> getSelectedNodes() {
120 return getSelected(nodes);
121 }
122
123 /**
124 * Return selected ways.
125 */
126 public Collection<OsmPrimitive> getSelectedWays() {
127 return getSelected(ways);
128 }
129
130 /**
131 * Return selected relations.
132 */
133 public Collection<OsmPrimitive> getSelectedRelations() {
134 return getSelected(relations);
135 }
136
137 public void setSelected(Collection<? extends OsmPrimitive> selection) {
138 clearSelection(nodes);
139 clearSelection(ways);
140 clearSelection(relations);
141 for (OsmPrimitive osm : selection)
142 osm.selected = true;
143 fireSelectionChanged(selection);
144 }
145
146 public void setSelected(OsmPrimitive... osm) {
147 if (osm.length == 1 && osm[0] == null) {
148 setSelected();
149 return;
150 }
151 clearSelection(nodes);
152 clearSelection(ways);
153 clearSelection(relations);
154 for (OsmPrimitive o : osm)
155 if (o != null)
156 o.selected = true;
157 fireSelectionChanged(Arrays.asList(osm));
158 }
159
160 /**
161 * Remove the selection from every value in the collection.
162 * @param list The collection to remove the selection from.
163 */
164 private void clearSelection(Collection<? extends OsmPrimitive> list) {
165 if (list == null)
166 return;
167 for (OsmPrimitive osm : list)
168 osm.selected = false;
169 }
170
171 /**
172 * Return all selected items in the collection.
173 * @param list The collection from which the selected items are returned.
174 */
175 private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) {
176 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
177 if (list == null)
178 return sel;
179 for (OsmPrimitive osm : list)
180 if (osm.selected && !osm.deleted)
181 sel.add(osm);
182 return sel;
183 }
184
185 /**
186 * Remember to fire an selection changed event. A call to this will not fire
187 * the event immediately. For more, @see SelectionChangedListener
188 */
189 public static void fireSelectionChanged(Collection<? extends OsmPrimitive> sel) {
190 for (SelectionChangedListener l : selListeners)
191 l.selectionChanged(sel);
192 }
193
194 @Override public DataSet clone() {
195 DataSet ds = new DataSet();
196 for (Node n : nodes)
197 ds.nodes.add(new Node(n));
198 for (Way w : ways)
199 ds.ways.add(new Way(w));
200 for (Relation e : relations)
201 ds.relations.add(new Relation(e));
202 for (DataSource source : dataSources)
203 ds.dataSources.add(new DataSource(source.bounds, source.origin));
204 return ds;
205 }
206}
Note: See TracBrowser for help on using the repository browser.