Changeset 2309 in josm


Ignore:
Timestamp:
2009-10-25T12:18:26+01:00 (13 years ago)
Author:
stoecker
Message:

applied #3696 - patch by Dave - removed primitive based selection

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  
    55import java.util.Arrays;
    66import java.util.Collection;
     7import java.util.Collections;
    78import java.util.Comparator;
    89import java.util.HashMap;
     10import java.util.LinkedHashSet;
    911import java.util.HashSet;
    1012import java.util.Iterator;
     
    1416
    1517import org.openstreetmap.josm.data.SelectionChangedListener;
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1619
    1720/**
     
    156159            relations.remove(primitive);
    157160        }
     161        selectedPrimitives.remove(primitive);
    158162    }
    159163
     
    163167
    164168    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        }
    167175        return sel;
    168176    }
     
    174182     */
    175183    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);
    180189    }
    181190
     
    233242    }
    234243
    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);
    237249        return true;
    238250    }
    239 
    240     public boolean toggleSelected(OsmPrimitive osm) {
    241         osm.setSelected(!osm.isSelected());
    242         return true;
    243     }
    244251    public boolean isSelected(OsmPrimitive osm) {
    245         return osm.isSelected();
     252        return selectedPrimitives.contains(osm);
    246253    }
    247254
     
    268275     */
    269276    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);
    276278        if (fireSelectionChangeEvent) {
    277279            fireSelectionChanged(selection);
     
    299301    }
    300302
     303    public void addSelected(OsmPrimitive... osm) {
     304        addSelected(Arrays.asList(osm));
     305    }
     306
    301307    /**
    302308     * Adds the primitives in <code>selection</code> to the current selection.
     
    307313     */
    308314    public void addSelected(Collection<? extends OsmPrimitive> selection, boolean fireSelectionChangeEvent) {
    309         for (OsmPrimitive osm : selection) {
    310             osm.setSelected(true);
    311         }
     315        selectedPrimitives.addAll(selection);
    312316        if (fireSelectionChangeEvent) {
    313317            fireSelectionChanged(selection);
     
    321325            return;
    322326        }
    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);
    331330    }
    332331
     
    364363        if (list == null)
    365364            return;
    366         for (OsmPrimitive osm : list) {
    367             osm.setSelected(false);
    368         }
     365        selectedPrimitives.removeAll(list);
    369366    }
    370367
     
    374371     */
    375372    private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) {
    376         Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
    377373        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);
    383380        return sel;
    384381    }
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r2305 r2309  
    4747    private static final int FLAG_DELETED  = 1 << 3;
    4848    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;
    5251
    5352    /**
     
    189188     * Sets whether this primitive is disabled or not.
    190189     *
    191      * @param selected true, if this primitive is disabled; false, otherwise
     190     * @param disabled true, if this primitive is disabled; false, otherwise
    192191     */
    193192    public void setDisabled(boolean disabled) {
     
    211210     * Sets whether this primitive is filtered out or not.
    212211     *
    213      * @param selected true, if this primitive is filtered out; false, otherwise
     212     * @param filtered true, if this primitive is filtered out; false, otherwise
    214213     */
    215214    public void setFiltered(boolean filtered) {
     
    227226    public boolean isFiltered() {
    228227        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, otherwise
    235      * @since 1899
    236      */
    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 selected
    248      * @since 1899
    249      */
    250     @Deprecated public boolean isSelected() {
    251         return (flags & FLAG_SELECTED) != 0;
    252228    }
    253229
     
    475451     * Sets whether this primitive is deleted or not.
    476452     *
    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.
    478454     *
    479455     * @param deleted  true, if this primitive is deleted; false, otherwise
     
    486462        }
    487463        setModified(deleted);
    488         setSelected(false);
    489464    }
    490465
Note: See TracChangeset for help on using the changeset viewer.