Changeset 3414 in josm for trunk


Ignore:
Timestamp:
2010-08-04T08:29:53+02:00 (14 years ago)
Author:
jttt
Message:

Fix #5089 Error at upload

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r3408 r3414  
    8181
    8282    private final ReadWriteLock lock = new ReentrantReadWriteLock();
     83    private final Object selectionLock = new Object();
    8384
    8485    public Lock getReadLock() {
     
    301302                relations.remove(primitive);
    302303            }
    303             selectedPrimitives.remove(primitive);
     304            synchronized (selectionLock) {
     305                selectedPrimitives.remove(primitive);
     306                selectionSnapshot = null;
     307            }
    304308            allPrimitives.remove(primitive);
    305309            primitive.setDataset(null);
     
    329333    public void fireSelectionChanged(){
    330334        synchronized (selListeners) {
    331             List<? extends OsmPrimitive> currentSelection = Collections.unmodifiableList(new ArrayList<OsmPrimitive>(selectedPrimitives));
     335            Collection<? extends OsmPrimitive> currentSelection = getSelected();
    332336            for (SelectionChangedListener l : selListeners) {
    333337                l.selectionChanged(currentSelection);
     
    336340    }
    337341
    338     LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
     342    private LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
     343    private Collection<OsmPrimitive> selectionSnapshot;
    339344
    340345    public Collection<OsmPrimitive> getSelectedNodesAndWays() {
    341         Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
    342         for (OsmPrimitive osm : selectedPrimitives) {
    343             if (osm instanceof Way ||
    344                     osm instanceof Node) {
    345                 sel.add(osm);
    346             }
    347         }
    348         return sel;
     346        return new DatasetCollection<OsmPrimitive>(getSelected(), new Predicate<OsmPrimitive>() {
     347            @Override
     348            public boolean evaluate(OsmPrimitive primitive) {
     349                return primitive instanceof Node || primitive instanceof Way;
     350            }
     351        });
    349352    }
    350353
     
    356359     */
    357360    public Collection<OsmPrimitive> getSelected() {
    358         return Collections.unmodifiableSet(selectedPrimitives);
     361        Collection<OsmPrimitive> currentList;
     362        synchronized (selectionLock) {
     363            if (selectionSnapshot == null) {
     364                selectionSnapshot = Collections.unmodifiableList(new ArrayList<OsmPrimitive>(selectedPrimitives));
     365            }
     366            currentList = selectionSnapshot;
     367        }
     368        return currentList;
    359369    }
    360370
     
    363373     */
    364374    public Collection<Node> getSelectedNodes() {
    365         List<Node> result = new ArrayList<Node>(selectedPrimitives.size());
    366         for (OsmPrimitive primitive:selectedPrimitives) {
    367             if (primitive instanceof Node) {
    368                 result.add((Node)primitive);
    369             }
    370         }
    371         return result;
     375        return new DatasetCollection<Node>(getSelected(), OsmPrimitive.nodePredicate);
    372376    }
    373377
     
    376380     */
    377381    public Collection<Way> getSelectedWays() {
    378         List<Way> result = new ArrayList<Way>(selectedPrimitives.size());
    379         for (OsmPrimitive primitive:selectedPrimitives) {
    380             if (primitive instanceof Way) {
    381                 result.add((Way)primitive);
    382             }
    383         }
    384         return result;
     382        return new DatasetCollection<Way>(getSelected(), OsmPrimitive.wayPredicate);
    385383    }
    386384
     
    389387     */
    390388    public Collection<Relation> getSelectedRelations() {
    391         List<Relation> result = new ArrayList<Relation>(selectedPrimitives.size() / 10);
    392         for (OsmPrimitive primitive:selectedPrimitives) {
    393             if (primitive instanceof Relation) {
    394                 result.add((Relation)primitive);
    395             }
    396         }
    397         return result;
     389        return new DatasetCollection<Relation>(getSelected(), OsmPrimitive.relationPredicate);
    398390    }
    399391
     
    404396    public void toggleSelected(Collection<? extends PrimitiveId> osm) {
    405397        boolean changed = false;
    406         for (PrimitiveId o : osm) {
    407             changed = changed | this.__toggleSelected(o);
     398        synchronized (selectionLock) {
     399            for (PrimitiveId o : osm) {
     400                changed = changed | this.__toggleSelected(o);
     401            }
     402            if (changed) {
     403                selectionSnapshot = null;
     404            }
    408405        }
    409406        if (changed) {
     
    421418            selectedPrimitives.add(primitive);
    422419        }
     420        selectionSnapshot = null;
    423421        return true;
    424422    }
     
    432430     */
    433431    public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
    434         boolean wasEmpty = selectedPrimitives.isEmpty();
    435         selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
    436         addSelected(selection, fireSelectionChangeEvent);
     432        boolean wasEmpty;
     433        synchronized (selectionLock) {
     434            wasEmpty = selectedPrimitives.isEmpty();
     435            selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
     436            addSelected(selection, fireSelectionChangeEvent);
     437            if (!wasEmpty && selectedPrimitives.isEmpty()) {
     438                selectionSnapshot = null;
     439            }
     440        }
     441
    437442        if (!wasEmpty && selectedPrimitives.isEmpty() && fireSelectionChangeEvent) {
     443            // If selection is not empty then event was already fired in addSelecteds
    438444            fireSelectionChanged();
    439445        }
     
    482488    public void addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
    483489        boolean changed = false;
    484         for (PrimitiveId id: selection) {
    485             OsmPrimitive primitive = getPrimitiveByIdChecked(id);
    486             if (primitive != null) {
    487                 changed = changed | selectedPrimitives.add(primitive);
     490        synchronized (selectionLock) {
     491            for (PrimitiveId id: selection) {
     492                OsmPrimitive primitive = getPrimitiveByIdChecked(id);
     493                if (primitive != null) {
     494                    changed = changed | selectedPrimitives.add(primitive);
     495                }
     496            }
     497            if (changed) {
     498                selectionSnapshot = null;
    488499            }
    489500        }
     
    502513    public void clearSelection(Collection<? extends PrimitiveId> list) {
    503514        boolean changed = false;
    504         for (PrimitiveId id:list) {
    505             OsmPrimitive primitive = getPrimitiveById(id);
    506             if (primitive != null) {
    507                 changed = changed | selectedPrimitives.remove(primitive);
     515        synchronized (selectionLock) {
     516            for (PrimitiveId id:list) {
     517                OsmPrimitive primitive = getPrimitiveById(id);
     518                if (primitive != null) {
     519                    changed = changed | selectedPrimitives.remove(primitive);
     520                }
     521            }
     522            if (changed) {
     523                selectionSnapshot = null;
    508524            }
    509525        }
     
    514530    public void clearSelection() {
    515531        if (!selectedPrimitives.isEmpty()) {
    516             selectedPrimitives.clear();
     532            synchronized (selectionLock) {
     533                selectedPrimitives.clear();
     534                selectionSnapshot = null;
     535            }
    517536            fireSelectionChanged();
    518537        }
     
    887906    private boolean cleanupDeleted(Iterator<? extends OsmPrimitive> it) {
    888907        boolean changed = false;
    889         while (it.hasNext()) {
    890             OsmPrimitive primitive = it.next();
    891             if (primitive.isDeleted()) {
    892                 selectedPrimitives.remove(primitive);
    893                 allPrimitives.remove(primitive);
    894                 primitive.setDataset(null);
    895                 changed = true;
    896                 it.remove();
     908        synchronized (selectionLock) {
     909            while (it.hasNext()) {
     910                OsmPrimitive primitive = it.next();
     911                if (primitive.isDeleted()) {
     912                    selectedPrimitives.remove(primitive);
     913                    selectionSnapshot = null;
     914                    allPrimitives.remove(primitive);
     915                    primitive.setDataset(null);
     916                    changed = true;
     917                    it.remove();
     918                }
     919            }
     920            if (changed) {
     921                selectionSnapshot = null;
    897922            }
    898923        }
Note: See TracChangeset for help on using the changeset viewer.