Ticket #5457: SelectAction.java.r3529.patch

File SelectAction.java.r3529.patch, 5.4 KB (added by cmuelle8, 16 years ago)

patches SelectionAction to enable the user to more easily select multiple primitives under the cursor

  • src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

     
    1515import java.util.Collection;
    1616import java.util.Collections;
    1717import java.util.HashSet;
     18import java.util.Iterator;
    1819import java.util.LinkedList;
    1920import java.util.List;
    2021import java.util.Set;
     
    372373            // selected object (this would break moving of selected groups).
    373374            // We'll do that later in mouseReleased if the user didn't try to
    374375            // move.
     376            System.out.println("SelectAction:mousePressed(): id=" +
     377                    osmColl.iterator().next().getId() + " in sel? " +
     378                    getCurrentDataSet().getSelected().containsAll(osmColl));
    375379            selectPrims(osmColl,
    376380                    shift || getCurrentDataSet().getSelected().containsAll(osmColl),
    377381                    ctrl, false, false);
     
    422426            boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
    423427            if (!didMove) {
    424428                selectPrims(
    425                         Main.map.mapView.getNearestCollection(e.getPoint(), OsmPrimitive.isSelectablePredicate),
     429                        Main.map.mapView.getAllNearest(e.getPoint(), OsmPrimitive.isSelectablePredicate),
    426430                        shift, ctrl, true, false);
    427431
    428432                // If the user double-clicked a node, change to draw mode
     
    500504        selectPrims(selectionManager.getObjectsInRectangle(r, alt), shift, ctrl, true, true);
    501505    }
    502506
     507    private boolean lookForMore = false;
     508    private OsmPrimitive ctrlStart = null;
     509
    503510    public void selectPrims(Collection<OsmPrimitive> selectionList, boolean shift,
    504511            boolean ctrl, boolean released, boolean area) {
    505512        DataSet ds = getCurrentDataSet();
    506513        if ((shift && ctrl) || (ctrl && !released))
    507514            return; // not allowed together
    508515
     516        // toggle through possible objects on mouse release
     517        if (released && !area) {
     518            if (selectionList.size() > 1) {
     519                Collection<OsmPrimitive> coll = ds.getSelected();
     520                OsmPrimitive o = selectionList.iterator().next(), first = o, fsel = null;
     521
     522                for (Iterator<OsmPrimitive> i = selectionList.iterator(); i.hasNext(); ) {
     523                    if (lookForMore && shift) {
     524                        if (!coll.contains(o = i.next())) {
     525                            break; // take first not in dsSel (or last)
     526                        }
     527                    } else {
     528                        if (coll.contains(o = i.next())) {
     529                            fsel = o;
     530                            if (lookForMore || ctrl) {
     531                                ds.clearSelection(o);
     532                                o = i.hasNext() ? i.next() : first;
     533                            }
     534                            break; // take first found
     535                        }
     536                    }
     537                }
     538
     539                if (ctrl) {
     540                    if (fsel != null) {
     541                        // fsel was found in dsSel, succ(fsel) == o holds
     542                        if (ctrlStart == null) {
     543                            ctrlStart = fsel;
     544                        }
     545                        if (o.equals(ctrlStart)) {
     546                            // user clicked his way through all possible completions
     547                            // now add o, so we also get a deselect step in rotation
     548                            ds.addSelected(o);
     549                            ctrlStart = null;
     550                        }
     551                    } else {
     552                        // selList and dsSel were disjunct, take o (first in rotation)
     553                        ctrlStart = o;
     554                    }
     555                } else {
     556                    ctrlStart = null;
     557                }
     558
     559                selectionList.clear();
     560                selectionList.add(o);
     561                System.out.println("SelectAction:selectPrims(): truncating selList to id=" + o.getId());
     562            }
     563        }
     564
    509565        if (ctrl) {
    510566            // Ctrl on an item toggles its selection status,
    511567            // but Ctrl on an *area* just clears those items
    512568            // out of the selection.
    513569            if (area) {
    514570                ds.clearSelection(selectionList);
     571                System.out.println("SelectAction:selectPrims(): clearing selList from dsSel");
    515572            } else {
    516573                ds.toggleSelected(selectionList);
     574                System.out.println("SelectAction:selectPrims(): toggling selList in dsSel");
    517575            }
    518576        } else {
     577            // hard-wiring to false due to performance reasons, should do w/out
     578            lookForMore = (released || area) ? false : ds.getSelected().containsAll(selectionList);
     579
    519580            // plain clicks with no modifiers
    520581            if (!shift) {
    521582                ds.setSelected(selectionList);
     583                System.out.println("SelectAction:selectPrims(): replacing dsSel w/ selList");
    522584            } else {
    523585                // add things to an
    524586                // existing selection.
    525587                ds.addSelected(selectionList);
     588                System.out.println("SelectAction:selectPrims(): adding selList to dsSel");
    526589            }
    527590        }
    528591    }