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