Changeset 12069 in josm


Ignore:
Timestamp:
2017-05-06T14:10:40+02:00 (8 years ago)
Author:
michael2402
Message:

Fix #14737: Preserve selection order and add comments that it is ordered. Add unit tests.

Location:
trunk
Files:
3 edited

Legend:

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

    r12058 r12069  
    44import java.util.Collections;
    55import java.util.HashSet;
     6import java.util.LinkedHashSet;
    67import java.util.Set;
    78import java.util.stream.Collectors;
     
    3940
    4041        /**
    41          * Gets the new selection
     42         * Gets the new selection. New elements are added to the end of the collection.
    4243         * <p>
    4344         * This collection cannot be modified and will not change.
     
    175176                this.current = this.getOldSelection();
    176177            } else {
    177                 this.current = new HashSet<>(old);
     178                this.current = new LinkedHashSet<>(old);
    178179                this.current.addAll(add);
    179180            }
     
    217218                this.current = this.getOldSelection();
    218219            } else {
    219                 HashSet<OsmPrimitive> currentSet = new HashSet<>(old);
     220                HashSet<OsmPrimitive> currentSet = new LinkedHashSet<>(old);
    220221                currentSet.removeAll(remove);
    221222                current = Collections.unmodifiableSet(currentSet);
     
    257258        public SelectionToggleEvent(DataSet source, Set<OsmPrimitive> old, Stream<OsmPrimitive> toToggle) {
    258259            super(source, old);
    259             HashSet<OsmPrimitive> currentSet = new HashSet<>(old);
    260             HashSet<OsmPrimitive> removeSet = new HashSet<>();
    261             HashSet<OsmPrimitive> addSet = new HashSet<>();
     260            HashSet<OsmPrimitive> currentSet = new LinkedHashSet<>(old);
     261            HashSet<OsmPrimitive> removeSet = new LinkedHashSet<>();
     262            HashSet<OsmPrimitive> addSet = new LinkedHashSet<>();
    262263            toToggle.forEach(p -> {
    263264                if (currentSet.remove(p)) {
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r12065 r12069  
    177177    /**
    178178     * The current selected primitives. This is always a unmodifiable set.
     179     *
     180     * The set should be ordered in the order in which the primitives have been added to the selection.
    179181     */
    180182    private Set<OsmPrimitive> currentSelectedPrimitives = Collections.emptySet();
     
    705707     * in this dataset, except deleted ones. May be empty, but not null.
    706708     *
     709     * When iterating through the set it is ordered by the order in which the primitives were added to the selection.
     710     *
    707711     * @return unmodifiable collection of primitives
    708712     */
     
    714718     * Replies an unmodifiable collection of primitives currently selected
    715719     * in this dataset, including deleted ones. May be empty, but not null.
     720     *
     721     * When iterating through the set it is ordered by the order in which the primitives were added to the selection.
    716722     *
    717723     * @return unmodifiable collection of primitives
  • trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetTest.java

    r12036 r12069  
    55import static org.junit.Assert.assertTrue;
    66
     7import java.util.ArrayList;
    78import java.util.Arrays;
    89import java.util.HashSet;
     
    124125        ds.unlinkNodeFromWays(n2);
    125126    }
     127
     128    /**
     129     * Test the selection order.
     130     * See <a href="https://josm.openstreetmap.de/ticket/14737">#14737</a>
     131     * @since 12069
     132     */
     133    @Test
     134    public void testSelectionOrderPreserved() {
     135        final DataSet ds = new DataSet();
     136        Node n1 = new Node(1);
     137        Node n2 = new Node(2);
     138        Node n3 = new Node(3);
     139        ds.addPrimitive(n1);
     140        ds.addPrimitive(n2);
     141        ds.addPrimitive(n3);
     142
     143        assertEquals(Arrays.asList(), new ArrayList<>(ds.getSelected()));
     144
     145        ds.setSelected(n1.getPrimitiveId(), n2.getPrimitiveId());
     146        assertEquals(Arrays.asList(n1, n2), new ArrayList<>(ds.getSelected()));
     147
     148        ds.clearSelection();
     149        assertEquals(Arrays.asList(), new ArrayList<>(ds.getSelected()));
     150
     151        ds.addSelected(n3.getPrimitiveId());
     152        ds.addSelected(n1.getPrimitiveId(), n2.getPrimitiveId());
     153        assertEquals(Arrays.asList(n3, n1, n2), new ArrayList<>(ds.getSelected()));
     154
     155        ds.addSelected(n3.getPrimitiveId());
     156        assertEquals(Arrays.asList(n3, n1, n2), new ArrayList<>(ds.getSelected()));
     157
     158        ds.clearSelection(n1.getPrimitiveId());
     159        assertEquals(Arrays.asList(n3, n2), new ArrayList<>(ds.getSelected()));
     160
     161        ds.toggleSelected(n1.getPrimitiveId());
     162        assertEquals(Arrays.asList(n3, n2, n1), new ArrayList<>(ds.getSelected()));
     163
     164        ds.toggleSelected(n2.getPrimitiveId());
     165        assertEquals(Arrays.asList(n3, n1), new ArrayList<>(ds.getSelected()));
     166
     167    }
    126168}
Note: See TracChangeset for help on using the changeset viewer.