Changeset 2401 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2009-11-07T17:10:51+01:00 (15 years ago)
Author:
jttt
Message:

Changes in selection handling:

  • selection is passed as PrimitiveId instead of OsmPrimitive
  • only primitives that exists in Dataset can be selected
  • selectionChanged event is fired only if selection was really changed
Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r2381 r2401  
    527527                newSelection.clear();
    528528                newSelection.add(wayToSelect);
    529                 ds.setSelected(way);
    530                 ds.fireSelectionChanged();
    531529            }
    532530        }
     
    544542                }
    545543            }
    546            
     544
    547545            newSelection.add(n);
    548546            ds.setSelected(n);
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r2399 r2401  
    4545    }
    4646
    47     /**
    48      * A list of listeners to selection changed events. The list is static, as listeners register
    49      * themselves for any dataset selection changes that occur, regardless of the current active
    50      * dataset. (However, the selection does only change in the active layer)
    51      */
    52     public static Collection<SelectionChangedListener> selListeners = new LinkedList<SelectionChangedListener>();
    53 
    54     /**
    55      * notifies all registered selection change listeners about the current selection of
    56      * primitives
    57      *
    58      * @param sel the current selection
    59      */
    60     private static void notifySelectionChangeListeners(Collection<? extends OsmPrimitive> sel) {
    61         for (SelectionChangedListener l : selListeners) {
    62             l.selectionChanged(sel);
    63         }
    64     }
    65 
    6647    private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash());
    6748    private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash());
     
    232213     * primitive are left unchanged.
    233214     *
    234      * @param primitive the primitive. Ignored if null.
     215     * @param primitive the primitive
    235216     */
    236217    public void removePrimitive(PrimitiveId primitiveId) {
    237         OsmPrimitive primitive = getPrimitiveById(primitiveId);
    238         if (primitive == null) {
    239             System.out.println("Warning: somebody is trying to remove nonexisting primitive from the Dataset. Action will be ignored. You can report this problem on http://josm.openstreetmap.de");
    240             new Exception().printStackTrace();
     218        OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
     219        if (primitive == null)
    241220            return;
    242         }
    243221        if (primitive instanceof Node) {
    244222            nodes.remove(primitive);
     
    252230    }
    253231
     232
     233    /*---------------------------------------------------
     234     *   SELECTION HANDLING
     235     *---------------------------------------------------*/
     236
     237    /**
     238     * A list of listeners to selection changed events. The list is static, as listeners register
     239     * themselves for any dataset selection changes that occur, regardless of the current active
     240     * dataset. (However, the selection does only change in the active layer)
     241     */
     242    public static Collection<SelectionChangedListener> selListeners = new LinkedList<SelectionChangedListener>();
     243
     244    /**
     245     * notifies all registered selection change listeners about the current selection of
     246     * primitives
     247     *
     248     * @param sel the current selection
     249     */
     250    private static void notifySelectionChangeListeners(Collection<? extends OsmPrimitive> sel) {
     251        for (SelectionChangedListener l : selListeners) {
     252            l.selectionChanged(sel);
     253        }
     254    }
     255
     256    /**
     257     * Notifies all registered {@see SelectionChangedListener} about the current selection in
     258     * this dataset.
     259     *
     260     */
     261    public void fireSelectionChanged(){
     262        notifySelectionChangeListeners(selectedPrimitives);
     263    }
     264
     265
     266    LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
     267
    254268    public Collection<OsmPrimitive> getSelectedNodesAndWays() {
    255269        Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
     
    295309    public Collection<OsmPrimitive> getSelectedRelations() {
    296310        return getSelected(relations);
    297     }
    298 
    299     public void setFiltered(Collection<? extends OsmPrimitive> selection) {
    300         clearFiltered(nodes);
    301         clearFiltered(ways);
    302         clearFiltered(relations);
    303         for (OsmPrimitive osm : selection) {
    304             osm.setFiltered(true);
    305         }
    306     }
    307 
    308     public void setFiltered(OsmPrimitive... osm) {
    309         if (osm.length == 1 && osm[0] == null) {
    310             setFiltered();
    311             return;
    312         }
    313         clearFiltered(nodes);
    314         clearFiltered(ways);
    315         clearFiltered(relations);
    316         for (OsmPrimitive o : osm)
    317             if (o != null) {
    318                 o.setFiltered(true);
    319             }
    320     }
    321 
    322     public void setDisabled(Collection<? extends OsmPrimitive> selection) {
    323         clearDisabled(nodes);
    324         clearDisabled(ways);
    325         clearDisabled(relations);
    326         for (OsmPrimitive osm : selection) {
    327             osm.setDisabled(true);
    328         }
    329     }
    330 
    331     LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
    332 
    333     public boolean toggleSelected(Collection<OsmPrimitive> osm) {
    334         for (OsmPrimitive o : osm) {
    335             this.__toggleSelected(o);
    336         }
    337         fireSelectionChanged();
    338         return true;
    339     }
    340     public boolean toggleSelected(OsmPrimitive... osm) {
    341         return this.toggleSelected(Arrays.asList(osm));
    342     }
    343     private boolean __toggleSelected(OsmPrimitive osm) {
    344         if (!selectedPrimitives.remove(osm)) {
    345             selectedPrimitives.add(osm);
    346         }
    347         return true;
    348     }
    349     public boolean isSelected(OsmPrimitive osm) {
    350         return selectedPrimitives.contains(osm);
    351     }
    352 
    353     public void setDisabled(OsmPrimitive... osm) {
    354         if (osm.length == 1 && osm[0] == null) {
    355             setDisabled();
    356             return;
    357         }
    358         clearDisabled(nodes);
    359         clearDisabled(ways);
    360         clearDisabled(relations);
    361         for (OsmPrimitive o : osm)
    362             if (o != null) {
    363                 o.setDisabled(true);
    364             }
    365     }
    366 
    367     /**
    368      * Sets the current selection to the primitives in <code>selection</code>.
    369      * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
    370      *
    371      * @param selection the selection
    372      * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
    373      */
    374     public void setSelected(Collection<? extends OsmPrimitive> selection, boolean fireSelectionChangeEvent) {
    375         selectedPrimitives = new LinkedHashSet<OsmPrimitive>(selection);
    376         if (fireSelectionChangeEvent) {
    377             fireSelectionChanged();
    378         }
    379     }
    380 
    381     /**
    382      * Sets the current selection to the primitives in <code>selection</code>
    383      * and notifies all {@see SelectionChangedListener}.
    384      *
    385      * @param selection the selection
    386      */
    387     public void setSelected(Collection<? extends OsmPrimitive> selection) {
    388         setSelected(selection, true /* fire selection change event */);
    389     }
    390 
    391     /**
    392      * Adds   the primitives in <code>selection</code> to the current selection
    393      * and notifies all {@see SelectionChangedListener}.
    394      *
    395      * @param selection the selection
    396      */
    397     public void addSelected(Collection<? extends OsmPrimitive> selection) {
    398         addSelected(selection, true /* fire selection change event */);
    399     }
    400 
    401     public void addSelected(OsmPrimitive... osm) {
    402         addSelected(Arrays.asList(osm));
    403     }
    404 
    405     /**
    406      * Adds the primitives in <code>selection</code> to the current selection.
    407      * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
    408      *
    409      * @param selection the selection
    410      * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
    411      */
    412     public void addSelected(Collection<? extends OsmPrimitive> selection, boolean fireSelectionChangeEvent) {
    413         selectedPrimitives.addAll(selection);
    414         if (fireSelectionChangeEvent) {
    415             fireSelectionChanged();
    416         }
    417     }
    418 
    419 
    420     public void setSelected(OsmPrimitive... osm) {
    421         if (osm.length == 1 && osm[0] == null) {
    422             setSelected();
    423             return;
    424         }
    425         List<OsmPrimitive> list = Arrays.asList(osm);
    426         setSelected(list);
    427         fireSelectionChanged();
    428     }
    429 
    430     /**
    431      * Remove the filtered parameter from every value in the collection.
    432      * @param list The collection to remove the filtered parameter from.
    433      */
    434     private void clearFiltered(Collection<? extends OsmPrimitive> list) {
    435         if (list == null)
    436             return;
    437         for (OsmPrimitive osm : list) {
    438             osm.setFiltered(false);
    439         }
    440     }
    441     /**
    442      * Remove the disabled parameter from every value in the collection.
    443      * @param list The collection to remove the disabled parameter from.
    444      */
    445     private void clearDisabled(Collection<? extends OsmPrimitive> list) {
    446         if (list == null)
    447             return;
    448         for (OsmPrimitive osm : list) {
    449             osm.setDisabled(false);
    450         }
    451     }
    452 
    453     /**
    454      * Remove the selection from every value in the collection.
    455      * @param list The collection to remove the selection from.
    456      */
    457     public void clearSelection(OsmPrimitive... osm) {
    458         clearSelection(Arrays.asList(osm));
    459     }
    460     public void clearSelection(Collection<? extends OsmPrimitive> list) {
    461         if (list == null)
    462             return;
    463         selectedPrimitives.removeAll(list);
    464     }
    465     public void clearSelection() {
    466         selectedPrimitives.clear();
    467311    }
    468312
     
    482326    }
    483327
    484     /**
    485      * Notifies all registered {@see SelectionChangedListener} about the current selection in
    486      * this dataset.
    487      *
    488      */
    489     public void fireSelectionChanged(){
    490         notifySelectionChangeListeners(selectedPrimitives);
     328    public boolean isSelected(OsmPrimitive osm) {
     329        return selectedPrimitives.contains(osm);
     330    }
     331
     332
     333    public void toggleSelected(Collection<? extends PrimitiveId> osm) {
     334        boolean changed = false;
     335        for (PrimitiveId o : osm) {
     336            changed = changed | this.__toggleSelected(o);
     337        }
     338        if (changed) {
     339            fireSelectionChanged();
     340        }
     341    }
     342    public void toggleSelected(PrimitiveId... osm) {
     343        toggleSelected(Arrays.asList(osm));
     344    }
     345    private boolean __toggleSelected(PrimitiveId primitiveId) {
     346        OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
     347        if (primitive == null)
     348            return false;
     349        if (!selectedPrimitives.remove(primitive)) {
     350            selectedPrimitives.add(primitive);
     351        }
     352        return true;
     353    }
     354
     355    /**
     356     * Sets the current selection to the primitives in <code>selection</code>.
     357     * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
     358     *
     359     * @param selection the selection
     360     * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
     361     */
     362    public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
     363        boolean wasEmpty = selectedPrimitives.isEmpty();
     364        selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
     365        addSelected(selection, fireSelectionChangeEvent);
     366        if (!wasEmpty && selectedPrimitives.isEmpty() && fireSelectionChangeEvent) {
     367            fireSelectionChanged();
     368        }
     369    }
     370
     371    /**
     372     * Sets the current selection to the primitives in <code>selection</code>
     373     * and notifies all {@see SelectionChangedListener}.
     374     *
     375     * @param selection the selection
     376     */
     377    public void setSelected(Collection<? extends PrimitiveId> selection) {
     378        setSelected(selection, true /* fire selection change event */);
     379    }
     380
     381    public void setSelected(PrimitiveId... osm) {
     382        if (osm.length == 1 && osm[0] == null) {
     383            setSelected();
     384            return;
     385        }
     386        List<PrimitiveId> list = Arrays.asList(osm);
     387        setSelected(list);
     388    }
     389
     390    /**
     391     * Adds   the primitives in <code>selection</code> to the current selection
     392     * and notifies all {@see SelectionChangedListener}.
     393     *
     394     * @param selection the selection
     395     */
     396    public void addSelected(Collection<? extends PrimitiveId> selection) {
     397        addSelected(selection, true /* fire selection change event */);
     398    }
     399
     400    public void addSelected(PrimitiveId... osm) {
     401        addSelected(Arrays.asList(osm));
     402    }
     403
     404    /**
     405     * Adds the primitives in <code>selection</code> to the current selection.
     406     * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
     407     *
     408     * @param selection the selection
     409     * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
     410     */
     411    public void addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
     412        boolean changed = false;
     413        for (PrimitiveId id: selection) {
     414            OsmPrimitive primitive = getPrimitiveByIdChecked(id);
     415            if (primitive != null) {
     416                changed = changed | selectedPrimitives.add(primitive);
     417            }
     418        }
     419        if (fireSelectionChangeEvent && changed) {
     420            fireSelectionChanged();
     421        }
     422    }
     423
     424    /**
     425     * Remove the selection from every value in the collection.
     426     * @param list The collection to remove the selection from.
     427     */
     428    public void clearSelection(PrimitiveId... osm) {
     429        clearSelection(Arrays.asList(osm));
     430    }
     431    public void clearSelection(Collection<? extends PrimitiveId> list) {
     432        boolean changed = false;
     433        for (PrimitiveId id:list) {
     434            OsmPrimitive primitive = getPrimitiveById(id);
     435            if (primitive != null) {
     436                changed = changed | selectedPrimitives.remove(primitive);
     437            }
     438        }
     439        if (changed) {
     440            fireSelectionChanged();
     441        }
     442    }
     443    public void clearSelection() {
     444        if (!selectedPrimitives.isEmpty()) {
     445            selectedPrimitives.clear();
     446            fireSelectionChanged();
     447        }
     448    }
     449
     450
     451    /*------------------------------------------------------
     452     * FILTERED / DISABLED HANDLING
     453     *-----------------------------------------------------*/
     454
     455    public void setDisabled(OsmPrimitive... osm) {
     456        if (osm.length == 1 && osm[0] == null) {
     457            setDisabled();
     458            return;
     459        }
     460        clearDisabled(nodes);
     461        clearDisabled(ways);
     462        clearDisabled(relations);
     463        for (OsmPrimitive o : osm)
     464            if (o != null) {
     465                o.setDisabled(true);
     466            }
     467    }
     468
     469    public void setFiltered(Collection<? extends OsmPrimitive> selection) {
     470        clearFiltered(nodes);
     471        clearFiltered(ways);
     472        clearFiltered(relations);
     473        for (OsmPrimitive osm : selection) {
     474            osm.setFiltered(true);
     475        }
     476    }
     477
     478    public void setFiltered(OsmPrimitive... osm) {
     479        if (osm.length == 1 && osm[0] == null) {
     480            setFiltered();
     481            return;
     482        }
     483        clearFiltered(nodes);
     484        clearFiltered(ways);
     485        clearFiltered(relations);
     486        for (OsmPrimitive o : osm)
     487            if (o != null) {
     488                o.setFiltered(true);
     489            }
     490    }
     491
     492    public void setDisabled(Collection<? extends OsmPrimitive> selection) {
     493        clearDisabled(nodes);
     494        clearDisabled(ways);
     495        clearDisabled(relations);
     496        for (OsmPrimitive osm : selection) {
     497            osm.setDisabled(true);
     498        }
     499    }
     500
     501
     502    /**
     503     * Remove the filtered parameter from every value in the collection.
     504     * @param list The collection to remove the filtered parameter from.
     505     */
     506    private void clearFiltered(Collection<? extends OsmPrimitive> list) {
     507        if (list == null)
     508            return;
     509        for (OsmPrimitive osm : list) {
     510            osm.setFiltered(false);
     511        }
     512    }
     513    /**
     514     * Remove the disabled parameter from every value in the collection.
     515     * @param list The collection to remove the disabled parameter from.
     516     */
     517    private void clearDisabled(Collection<? extends OsmPrimitive> list) {
     518        if (list == null)
     519            return;
     520        for (OsmPrimitive osm : list) {
     521            osm.setDisabled(false);
     522        }
    491523    }
    492524
     
    585617    }
    586618
     619    /**
     620     * Show message and stack trace in log in case primitive is not found
     621     * @param primitiveId
     622     * @return Primitive by id.
     623     */
     624    private OsmPrimitive getPrimitiveByIdChecked(PrimitiveId primitiveId) {
     625        OsmPrimitive result = getPrimitiveById(primitiveId);
     626        if (result == null) {
     627            System.out.println(tr("JOSM expected to find primitive [{0} {1}] in dataset but it's not there. Please report this "
     628                    + " at http://josm.openstreetmap.de . This is not a critical error, it should be safe to continue in your work.",
     629                    primitiveId.getType(), Long.toString(primitiveId.getUniqueId())));
     630            new Exception().printStackTrace();
     631        }
     632
     633        return result;
     634    }
     635
    587636    public Set<Long> getPrimitiveIds() {
    588637        HashSet<Long> ret = new HashSet<Long>();
     
    753802     */
    754803    public void clear() {
    755         if (!selectedPrimitives.isEmpty()) {
    756             selectedPrimitives.clear();
    757             fireSelectionChanged();
    758         }
     804        clearSelection();
    759805        nodes.clear();
    760806        ways.clear();
    761807        relations.clear();
     808        allPrimitives.clear();
    762809    }
    763810}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/ParentRelationLoadingTask.java

    r2381 r2401  
    1414import org.openstreetmap.josm.data.osm.DataSet;
    1515import org.openstreetmap.josm.data.osm.DataSource;
    16 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1716import org.openstreetmap.josm.data.osm.Relation;
    1817import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
     
    151150        parents.clear();
    152151        for (Relation parent : referrers.getRelations()) {
    153             parents.add((Relation) getLayer().data.getPrimitiveById(parent.getId(), OsmPrimitiveType.RELATION));
     152            parents.add((Relation) getLayer().data.getPrimitiveById(parent));
    154153        }
    155154        if (continuation != null) {
Note: See TracChangeset for help on using the changeset viewer.