Changeset 2309 in josm
- Timestamp:
- 2009-10-25T12:18:26+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/osm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r2305 r2309 5 5 import java.util.Arrays; 6 6 import java.util.Collection; 7 import java.util.Collections; 7 8 import java.util.Comparator; 8 9 import java.util.HashMap; 10 import java.util.LinkedHashSet; 9 11 import java.util.HashSet; 10 12 import java.util.Iterator; … … 14 16 15 17 import org.openstreetmap.josm.data.SelectionChangedListener; 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 16 19 17 20 /** … … 156 159 relations.remove(primitive); 157 160 } 161 selectedPrimitives.remove(primitive); 158 162 } 159 163 … … 163 167 164 168 public Collection<OsmPrimitive> getSelectedNodesAndWays() { 165 Collection<OsmPrimitive> sel = getSelected(nodes); 166 sel.addAll(getSelected(ways)); 169 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(); 170 for (OsmPrimitive osm : selectedPrimitives) { 171 if (osm instanceof Way || 172 osm instanceof Node) 173 sel.add(osm); 174 } 167 175 return sel; 168 176 } … … 174 182 */ 175 183 public Collection<OsmPrimitive> getSelected() { 176 Collection<OsmPrimitive> sel = getSelected(nodes); 177 sel.addAll(getSelected(ways)); 178 sel.addAll(getSelected(relations)); 179 return sel; 184 // It would be nice to have this be a copy-on-write list 185 // or an Collections.unmodifiableList(). It would be 186 // much faster for large selections. May users just 187 // call this, and only check the .size(). 188 return new ArrayList<OsmPrimitive>(selectedPrimitives); 180 189 } 181 190 … … 233 242 } 234 243 235 public boolean addSelected(OsmPrimitive osm) { 236 osm.setSelected(true); 244 LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>(); 245 246 public boolean toggleSelected(OsmPrimitive osm) { 247 if (!selectedPrimitives.remove(osm)) 248 selectedPrimitives.add(osm); 237 249 return true; 238 250 } 239 240 public boolean toggleSelected(OsmPrimitive osm) {241 osm.setSelected(!osm.isSelected());242 return true;243 }244 251 public boolean isSelected(OsmPrimitive osm) { 245 return osm.isSelected();252 return selectedPrimitives.contains(osm); 246 253 } 247 254 … … 268 275 */ 269 276 public void setSelected(Collection<? extends OsmPrimitive> selection, boolean fireSelectionChangeEvent) { 270 clearSelection(nodes); 271 clearSelection(ways); 272 clearSelection(relations); 273 for (OsmPrimitive osm : selection) { 274 osm.setSelected(true); 275 } 277 selectedPrimitives = new LinkedHashSet<OsmPrimitive>(selection); 276 278 if (fireSelectionChangeEvent) { 277 279 fireSelectionChanged(selection); … … 299 301 } 300 302 303 public void addSelected(OsmPrimitive... osm) { 304 addSelected(Arrays.asList(osm)); 305 } 306 301 307 /** 302 308 * Adds the primitives in <code>selection</code> to the current selection. … … 307 313 */ 308 314 public void addSelected(Collection<? extends OsmPrimitive> selection, boolean fireSelectionChangeEvent) { 309 for (OsmPrimitive osm : selection) { 310 osm.setSelected(true); 311 } 315 selectedPrimitives.addAll(selection); 312 316 if (fireSelectionChangeEvent) { 313 317 fireSelectionChanged(selection); … … 321 325 return; 322 326 } 323 clearSelection(nodes); 324 clearSelection(ways); 325 clearSelection(relations); 326 for (OsmPrimitive o : osm) 327 if (o != null) { 328 o.setSelected(true); 329 } 330 fireSelectionChanged(Arrays.asList(osm)); 327 List<OsmPrimitive> list = Arrays.asList(osm); 328 setSelected(list); 329 fireSelectionChanged(list); 331 330 } 332 331 … … 364 363 if (list == null) 365 364 return; 366 for (OsmPrimitive osm : list) { 367 osm.setSelected(false); 368 } 365 selectedPrimitives.removeAll(list); 369 366 } 370 367 … … 374 371 */ 375 372 private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) { 376 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();377 373 if (list == null) 378 return sel; 379 for (OsmPrimitive osm : list) 380 if (osm.isSelected() && !osm.isDeleted()) { 381 sel.add(osm); 382 } 374 return new LinkedList<OsmPrimitive>(); 375 // getSelected() is called with large lists, so 376 // creating the return list from the selection 377 // should be faster most of the time. 378 Collection<OsmPrimitive> sel = new LinkedHashSet<OsmPrimitive>(selectedPrimitives); 379 sel.retainAll(list); 383 380 return sel; 384 381 } -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r2305 r2309 47 47 private static final int FLAG_DELETED = 1 << 3; 48 48 private static final int FLAG_FILTERED = 1 << 4; 49 private static final int FLAG_SELECTED = 1 << 5; 50 private static final int FLAG_HAS_DIRECTIONS = 1 << 6; 51 private static final int FLAG_TAGGED = 1 << 7; 49 private static final int FLAG_HAS_DIRECTIONS = 1 << 5; 50 private static final int FLAG_TAGGED = 1 << 6; 52 51 53 52 /** … … 189 188 * Sets whether this primitive is disabled or not. 190 189 * 191 * @param selectedtrue, if this primitive is disabled; false, otherwise190 * @param disabled true, if this primitive is disabled; false, otherwise 192 191 */ 193 192 public void setDisabled(boolean disabled) { … … 211 210 * Sets whether this primitive is filtered out or not. 212 211 * 213 * @param selectedtrue, if this primitive is filtered out; false, otherwise212 * @param filtered true, if this primitive is filtered out; false, otherwise 214 213 */ 215 214 public void setFiltered(boolean filtered) { … … 227 226 public boolean isFiltered() { 228 227 return (flags & FLAG_FILTERED) != 0; 229 }230 231 /**232 * Sets whether this primitive is selected or not.233 *234 * @param selected true, if this primitive is selected; false, otherwise235 * @since 1899236 */237 @Deprecated public void setSelected(boolean selected) {238 if (selected) {239 flags |= FLAG_SELECTED;240 } else {241 flags &= ~FLAG_SELECTED;242 }243 }244 /**245 * Replies true, if this primitive is selected.246 *247 * @return true, if this primitive is selected248 * @since 1899249 */250 @Deprecated public boolean isSelected() {251 return (flags & FLAG_SELECTED) != 0;252 228 } 253 229 … … 475 451 * Sets whether this primitive is deleted or not. 476 452 * 477 * Also marks this primitive as modified if deleted is true and sets selected to false.453 * Also marks this primitive as modified if deleted is true. 478 454 * 479 455 * @param deleted true, if this primitive is deleted; false, otherwise … … 486 462 } 487 463 setModified(deleted); 488 setSelected(false);489 464 } 490 465
Note:
See TracChangeset
for help on using the changeset viewer.