Ticket #5457: SelectAction.java.patch

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

fix GetNearestNodes() problem not returning all nearest nodes by using GetAllNearest() .. probably needs a new bug ticket for GetNearestNodes() problems !?

  • 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;
    19 import java.util.List;
    2020import java.util.Set;
    2121import java.util.TreeSet;
    2222
     
    337337    @Override public void mousePressed(MouseEvent e) {
    338338        if(!Main.map.mapView.isActiveLayerVisible())
    339339            return;
     340
    340341        // request focus in order to enable the expected keyboard shortcuts
    341         //
    342342        Main.map.mapView.requestFocus();
    343343
    344344        cancelDrawMode = false;
     
    346346        if (e.getButton() != MouseEvent.BUTTON1)
    347347            return;
    348348        boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
    349         boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
     349        /*boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;*/
    350350        boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
    351351
    352352        // We don't want to change to draw tool if the user tries to (de)select
     
    359359        didMove = false;
    360360        initialMoveThresholdExceeded = false;
    361361
    362         Collection<OsmPrimitive> osmColl = getNearestCollectionVirtual(e.getPoint(), alt);
     362        Collection<OsmPrimitive> osmColl = getNearestCollectionVirtual(e.getPoint(), false /*, alt*/);
    363363
    364364        if (ctrl && shift) {
    365365            if (getCurrentDataSet().getSelected().isEmpty()) {
     
    420420        if (mode == Mode.move) {
    421421            boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
    422422            boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
     423            boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0
     424            || Main.pref.getBoolean("selectaction.rotates", false);
    423425            if (!didMove) {
    424                 selectPrims(
    425                         Main.map.mapView.getNearestCollection(e.getPoint(), OsmPrimitive.isSelectablePredicate),
    426                         shift, ctrl, true, false);
     426                Collection<OsmPrimitive> c = Main.map.mapView.getNearestCollection(e.getPoint(), OsmPrimitive.isSelectablePredicate);
     427                if (!c.isEmpty() && alt) {
     428                    if (c.iterator().next() instanceof Node) {
     429                        // there is at least one node under the cursor:
     430                        //   - make sure (first element of new list) equals (result of getNearestCollection)
     431                        //   - do not consider ways at all, but all nearest nodes
     432                        c = new ArrayList<OsmPrimitive>();
     433                        for (OsmPrimitive p : Main.map.mapView.getAllNearest(e.getPoint(), OsmPrimitive.isSelectablePredicate)) {
     434                            if (p instanceof Node) {
     435                                c.add(p);
     436                            }
     437                        }
     438                    } else {
     439                        // consider all ways..
     440                        c = Main.map.mapView.getAllNearest(e.getPoint(), OsmPrimitive.isSelectablePredicate);
     441                    }
     442                }
     443                selectPrims(c, shift, ctrl, true, false);
    427444
    428445                // If the user double-clicked a node, change to draw mode
    429                 List<OsmPrimitive> sel = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
    430                 if(e.getClickCount() >=2 && sel.size() == 1 && sel.get(0) instanceof Node) {
     446                c = getCurrentDataSet().getSelected();
     447                if(e.getClickCount() >=2 && c.size() == 1 && c.iterator().next() instanceof Node) {
    431448                    // We need to do it like this as otherwise drawAction will see a double
    432449                    // click and switch back to SelectMode
    433450                    Main.worker.execute(new Runnable(){
     
    500517        selectPrims(selectionManager.getObjectsInRectangle(r, alt), shift, ctrl, true, true);
    501518    }
    502519
     520    private boolean selMorePrims = false;
     521    private OsmPrimitive selCycleStart = null;
     522
    503523    public void selectPrims(Collection<OsmPrimitive> selectionList, boolean shift,
    504524            boolean ctrl, boolean released, boolean area) {
    505525        DataSet ds = getCurrentDataSet();
    506526        if ((shift && ctrl) || (ctrl && !released))
    507527            return; // not allowed together
    508528
     529        // toggle through possible objects on mouse release
     530        if (released && !area) {
     531            if (selectionList.size() > 1) {
     532                Collection<OsmPrimitive> coll = ds.getSelected();
     533
     534                OsmPrimitive first, foundInDS, node, nxt;
     535                first = nxt = selectionList.iterator().next();
     536                foundInDS = node = null;
     537
     538                for (Iterator<OsmPrimitive> i = selectionList.iterator(); i.hasNext(); ) {
     539                    if (selMorePrims && shift) {
     540                        if (!coll.contains(nxt = i.next())) {
     541                            break; // take first primitive not in dsSel or last if all contained
     542                        }
     543                    } else {
     544                        if (coll.contains(nxt = i.next())) {
     545                            foundInDS = nxt;
     546                            if (selMorePrims || ctrl) {
     547                                ds.clearSelection(nxt);
     548                                nxt = i.hasNext() ? i.next() : first;
     549                            }
     550                            break; // take next primitive of selList
     551                        } else if (nxt instanceof Node && node == null) {
     552                            node = nxt;
     553                        }
     554                    }
     555                }
     556
     557                if (ctrl) {
     558                    if (foundInDS != null) {
     559                        // a member of selList was foundInDS
     560                        if (!selectionList.contains(selCycleStart)) {
     561                            selCycleStart = foundInDS;
     562                        }
     563                        // check if selCycleStart == prim (equals next(foundInDS))
     564                        if (selCycleStart.equals(nxt)) {
     565                            ds.addSelected(nxt);   // cycle complete, prim toggled below
     566                            selCycleStart = null;  // check: might do w/out ??
     567                        }
     568                    } else {
     569                        // no member of selList was foundInDS (sets were disjunct), setup for new cycle
     570                        selCycleStart = nxt = (node != null) ? node : first;
     571                    }
     572                }
     573
     574                selectionList = new ArrayList<OsmPrimitive>(1); // do not modify the passed object..
     575                selectionList.add(nxt);
     576                System.out.println("SelectAction:selectPrims(): truncated selList to id=" + nxt.getId());
     577            }
     578        }
     579
     580        // hard-wiring to false due to performance reasons, should do w/out
     581        selMorePrims = (released || area) ? false : ds.getSelected().containsAll(selectionList);
     582
    509583        if (ctrl) {
    510584            // Ctrl on an item toggles its selection status,
    511585            // but Ctrl on an *area* just clears those items