Ignore:
Timestamp:
2017-05-08T21:51:44+02:00 (7 years ago)
Author:
donvip
Message:

fix #josm14758 - NPE

Location:
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/SelectBoundaryAction.java

    r32410 r33297  
    1717import org.openstreetmap.josm.actions.JosmAction;
    1818import org.openstreetmap.josm.actions.SelectByInternalPointAction;
     19import org.openstreetmap.josm.data.osm.DataSet;
    1920import org.openstreetmap.josm.data.osm.Node;
    2021import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3940    @Override
    4041    public void actionPerformed(ActionEvent e) {
    41         Set<Way> selectedWays = OsmPrimitive.getFilteredSet(getLayerManager().getEditDataSet().getSelected(), Way.class);
    42         Set<Node> selectedNodes = OsmPrimitive.getFilteredSet(getLayerManager().getEditDataSet().getSelected(), Node.class);
     42        DataSet ds = getLayerManager().getEditDataSet();
     43        if (ds != null) {
     44            Collection<Way> selectedWays = ds.getSelectedWays();
     45            Collection<Node> selectedNodes = ds.getSelectedNodes();
    4346
    44         Set<Way> newWays = new HashSet<>();
     47            Set<Way> newWays = new HashSet<>();
    4548
    46         Way w = null;
     49            Way w = null;
    4750
    48         if (selectedWays.isEmpty()) {
    49             if (selectedNodes.size() == 1) {
    50                 for (OsmPrimitive p : selectedNodes.iterator().next().getReferrers()) {
    51                     if (p instanceof Way && p.isSelectable()) {
    52                         w = (Way) p;
    53                         break;
     51            if (selectedWays.isEmpty()) {
     52                if (selectedNodes.size() == 1) {
     53                    for (OsmPrimitive p : selectedNodes.iterator().next().getReferrers()) {
     54                        if (p instanceof Way && p.isSelectable()) {
     55                            w = (Way) p;
     56                            break;
     57                        }
    5458                    }
     59                } else {
     60                    Point p = Main.map.mapView.getMousePosition();
     61                    SelectByInternalPointAction.performSelection(Main.map.mapView.getEastNorth(p.x, p.y), false, false);
     62                    return;
    5563                }
     64            } else if (selectedWays.size() == 1) {
     65                w = selectedWays.iterator().next();
     66            } else if (selectedWays.contains(lastUsedStartingWay)) {
     67                w = lastUsedStartingWay; //repeated call for selected way
     68                lastUsedLeft = !lastUsedLeft;
     69            }
     70
     71            if (w == null) return; //no starting way found
     72            if (!w.isSelectable()) return;
     73            if (w.isClosed()) return;
     74            if (w.getNodesCount() < 2) return;
     75
     76            newWays.add(w);
     77            lastUsedStartingWay = w;
     78
     79            // try going left at each turn
     80            if (!NodeWayUtils.addAreaBoundary(w, newWays, lastUsedLeft)) {
     81                NodeWayUtils.addAreaBoundary(w, newWays, !lastUsedLeft); // try going right at each turn
     82            }
     83
     84            if (!newWays.isEmpty()) {
     85                ds.setSelected(newWays);
    5686            } else {
    57                 Point p = Main.map.mapView.getMousePosition();
    58                 SelectByInternalPointAction.performSelection(Main.map.mapView.getEastNorth(p.x, p.y), false, false);
    59                 return;
     87                new Notification(tr("Nothing found. Please select way that is a part of some polygon formed by connected ways"))
     88                .setIcon(JOptionPane.WARNING_MESSAGE).show();
    6089            }
    61         } else if (selectedWays.size() == 1) {
    62             w = selectedWays.iterator().next();
    63         } else if (selectedWays.contains(lastUsedStartingWay)) {
    64             w = lastUsedStartingWay; //repeated call for selected way
    65             lastUsedLeft = !lastUsedLeft;
    66         }
    67 
    68 
    69         if (w == null) return; //no starting way found
    70         if (!w.isSelectable()) return;
    71         if (w.isClosed()) return;
    72         if (w.getNodesCount() < 2) return;
    73 
    74         newWays.add(w);
    75         lastUsedStartingWay = w;
    76 
    77         // try going left at each turn
    78         if (!NodeWayUtils.addAreaBoundary(w, newWays, lastUsedLeft)) {
    79             NodeWayUtils.addAreaBoundary(w, newWays, !lastUsedLeft); // try going right at each turn
    80         }
    81 
    82         if (!newWays.isEmpty()) {
    83             getLayerManager().getEditDataSet().setSelected(newWays);
    84         } else {
    85             new Notification(tr("Nothing found. Please select way that is a part of some polygon formed by connected ways"))
    86             .setIcon(JOptionPane.WARNING_MESSAGE).show();
    8790        }
    8891    }
     
    9598    @Override
    9699    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    97         if (selection == null) {
    98             return;
    99         }
    100         setEnabled(true);
     100        setEnabled(selection != null && !selection.isEmpty());
    101101    }
    102102}
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/SelectModNodesAction.java

    r32410 r33297  
    3737    public void actionPerformed(ActionEvent e) {
    3838        DataSet ds = getLayerManager().getEditDataSet();
    39         Collection<OsmPrimitive> selection = ds.getSelected();
    40         Set<Node> selectedNodes = OsmPrimitive.getFilteredSet(selection, Node.class);
    41         ds.clearSelection(selectedNodes);
    42         Command cmd = null;
     39        if (ds != null) {
     40            Collection<OsmPrimitive> selection = ds.getSelected();
     41            ds.clearSelection(OsmPrimitive.getFilteredSet(selection, Node.class));
     42            Command cmd = null;
    4343
    44         if (Main.main.undoRedo.commands == null) return;
    45         int num = Main.main.undoRedo.commands.size();
    46         if (num == 0) return;
    47         int k = 0, idx;
    48         if (selection != null && !selection.isEmpty() && selection.hashCode() == lastHash) {
    49             // we are selecting next command in history if nothing is selected
    50             idx = Main.main.undoRedo.commands.indexOf(lastCmd);
    51         } else {
    52             idx = num;
     44            if (Main.main.undoRedo.commands == null) return;
     45            int num = Main.main.undoRedo.commands.size();
     46            if (num == 0) return;
     47            int k = 0, idx;
     48            if (selection != null && !selection.isEmpty() && selection.hashCode() == lastHash) {
     49                // we are selecting next command in history if nothing is selected
     50                idx = Main.main.undoRedo.commands.indexOf(lastCmd);
     51            } else {
     52                idx = num;
     53            }
     54
     55            Set<Node> nodes = new HashSet<>(10);
     56            do {  //  select next history element
     57                if (idx > 0) idx--; else idx = num-1;
     58                cmd = Main.main.undoRedo.commands.get(idx);
     59                Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
     60                nodes.clear();
     61                for (OsmPrimitive p : pp) {  // find all affected ways
     62                    if (p instanceof Node && !p.isDeleted()) nodes.add((Node) p);
     63                }
     64                if (!nodes.isEmpty()) {
     65                    ds.setSelected(nodes);
     66                    lastCmd = cmd; // remember last used command and last selection
     67                    lastHash = ds.getSelected().hashCode();
     68                    return;
     69                }
     70                k++;
     71            } while (k < num); // try to find previous command if this affects nothing
     72            lastCmd = null; lastHash = 0;
    5373        }
    54 
    55         Set<Node> nodes = new HashSet<>(10);
    56         do {  //  select next history element
    57             if (idx > 0) idx--; else idx = num-1;
    58             cmd = Main.main.undoRedo.commands.get(idx);
    59             Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
    60             nodes.clear();
    61             for (OsmPrimitive p : pp) {  // find all affected ways
    62                 if (p instanceof Node && !p.isDeleted()) nodes.add((Node) p);
    63             }
    64             if (!nodes.isEmpty()) {
    65                 ds.setSelected(nodes);
    66                 lastCmd = cmd; // remember last used command and last selection
    67                 lastHash = ds.getSelected().hashCode();
    68                 return;
    69             }
    70             k++;
    71             //System.out.println("no nodes found, previous...");
    72         } while (k < num); // try to find previous command if this affects nothing
    73         lastCmd = null; lastHash = 0;
    7474    }
    7575
    7676    @Override
    7777    protected void updateEnabledState() {
    78         updateEnabledStateOnCurrentSelection();
    79     }
    80 
    81     @Override
    82     protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    83         setEnabled(true);
     78        setEnabled(getLayerManager().getEditDataSet() != null);
    8479    }
    8580}
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/SelectModWaysAction.java

    r32410 r33297  
    3838    public void actionPerformed(ActionEvent e) {
    3939        DataSet ds = getLayerManager().getEditDataSet();
    40         Collection<OsmPrimitive> selection = ds.getSelected();
    41         Set<Node> selectedNodes = OsmPrimitive.getFilteredSet(selection, Node.class);
    42         ds.clearSelection(selectedNodes);
    43         Command cmd;
     40        if (ds != null) {
     41            Collection<OsmPrimitive> selection = ds.getSelected();
     42            ds.clearSelection(OsmPrimitive.getFilteredSet(selection, Node.class));
     43            Command cmd;
    4444
    45         if (Main.main.undoRedo.commands == null) return;
    46         int num = Main.main.undoRedo.commands.size();
    47         if (num == 0) return;
    48         int k = 0, idx;
    49         if (selection != null && !selection.isEmpty() && selection.hashCode() == lastHash) {
    50             // we are selecting next command in history if nothing is selected
    51             idx = Main.main.undoRedo.commands.indexOf(lastCmd);
    52         } else {
    53             idx = num;
     45            if (Main.main.undoRedo.commands == null) return;
     46            int num = Main.main.undoRedo.commands.size();
     47            if (num == 0) return;
     48            int k = 0, idx;
     49            if (selection != null && !selection.isEmpty() && selection.hashCode() == lastHash) {
     50                // we are selecting next command in history if nothing is selected
     51                idx = Main.main.undoRedo.commands.indexOf(lastCmd);
     52            } else {
     53                idx = num;
     54            }
     55
     56            Set<Way> ways = new HashSet<>(10);
     57            do {  //  select next history element
     58                if (idx > 0) idx--; else idx = num-1;
     59                cmd = Main.main.undoRedo.commands.get(idx);
     60                Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
     61                ways.clear();
     62                for (OsmPrimitive p : pp) {  // find all affected ways
     63                    if (p instanceof Way && !p.isDeleted()) ways.add((Way) p);
     64                }
     65                if (!ways.isEmpty() && !ds.getSelected().containsAll(ways)) {
     66                    ds.setSelected(ways);
     67                    lastCmd = cmd; // remember last used command and last selection
     68                    lastHash = ds.getSelected().hashCode();
     69                    return;
     70                }
     71                k++;
     72            } while (k < num); // try to find previous command if this affects nothing
     73            lastCmd = null;
     74            lastHash = 0;
    5475        }
    55 
    56         Set<Way> ways = new HashSet<>(10);
    57         do {  //  select next history element
    58             if (idx > 0) idx--; else idx = num-1;
    59             cmd = Main.main.undoRedo.commands.get(idx);
    60             Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
    61             ways.clear();
    62             for (OsmPrimitive p : pp) {  // find all affected ways
    63                 if (p instanceof Way && !p.isDeleted()) ways.add((Way) p);
    64             }
    65             if (!ways.isEmpty() && !ds.getSelected().containsAll(ways)) {
    66                 ds.setSelected(ways);
    67                 lastCmd = cmd; // remember last used command and last selection
    68                 lastHash = ds.getSelected().hashCode();
    69                 return;
    70             }
    71             k++;
    72         } while (k < num); // try to find previous command if this affects nothing
    73         lastCmd = null;
    74         lastHash = 0;
    7576    }
    7677
    7778    @Override
    7879    protected void updateEnabledState() {
    79         updateEnabledStateOnCurrentSelection();
    80     }
    81 
    82     @Override
    83     protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    84         setEnabled(true);
     80        setEnabled(getLayerManager().getEditDataSet() != null);
    8581    }
    8682}
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/UndoSelectionAction.java

    r32410 r33297  
    3636    public void actionPerformed(ActionEvent e) {
    3737        DataSet ds = getLayerManager().getEditDataSet();
    38         LinkedList<Collection<? extends OsmPrimitive>> history = ds.getSelectionHistory();
    39         if (history == null || history.isEmpty()) return; // empty history
    40         int num = history.size();
     38        if (ds != null) {
     39            LinkedList<Collection<? extends OsmPrimitive>> history = ds.getSelectionHistory();
     40            if (history == null || history.isEmpty()) return; // empty history
     41            int num = history.size();
    4142
    42         Collection<OsmPrimitive> selection = ds.getSelected();
     43            Collection<OsmPrimitive> selection = ds.getSelected();
    4344
    44         if (selection != null && selection.hashCode() != myAutomaticSelectionHash) {
    45             // manual selection or another pluging selection noticed
    46             index = history.indexOf(lastSel);
    47             // first is selected, next list is previous selection
     45            if (selection != null && selection.hashCode() != myAutomaticSelectionHash) {
     46                // manual selection or another pluging selection noticed
     47                index = history.indexOf(lastSel);
     48                // first is selected, next list is previous selection
     49            }
     50            int k = 0;
     51            Set<OsmPrimitive> newsel = new HashSet<>();
     52            do {
     53                if (index+1 < history.size()) index++; else index = 0;
     54                Collection<? extends OsmPrimitive> histsel = history.get(index);
     55                // remove deleted entities from selection
     56                newsel.clear();
     57                newsel.addAll(histsel);
     58                newsel.retainAll(ds.allNonDeletedPrimitives());
     59                if (!newsel.isEmpty()) break;
     60                k++;
     61            } while (k < num);
     62
     63            ds.setSelected(newsel);
     64            lastSel = ds.getSelected();
     65            myAutomaticSelectionHash = lastSel.hashCode();
     66            // remember last automatic selection
    4867        }
    49         int k = 0;
    50         Set<OsmPrimitive> newsel = new HashSet<>();
    51         do {
    52             if (index+1 < history.size()) index++; else index = 0;
    53             Collection<? extends OsmPrimitive> histsel = history.get(index);
    54             // remove deleted entities from selection
    55             newsel.clear();
    56             newsel.addAll(histsel);
    57             newsel.retainAll(ds.allNonDeletedPrimitives());
    58             if (newsel.size() > 0) break;
    59             k++;
    60         } while (k < num);
    61 
    62         ds.setSelected(newsel);
    63         lastSel = ds.getSelected();
    64         myAutomaticSelectionHash = lastSel.hashCode();
    65         // remeber last automatic selection
    6668    }
    6769
    6870    @Override
    6971    protected void updateEnabledState() {
    70         updateEnabledStateOnCurrentSelection();
    71     }
    72 
    73     @Override
    74     protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    75         setEnabled(true);
     72        setEnabled(getLayerManager().getEditDataSet() != null);
    7673    }
    7774}
Note: See TracChangeset for help on using the changeset viewer.