### Eclipse Workspace Patch 1.0
#P josm
diff --git src/org/openstreetmap/josm/actions/AlignGridRotationAction.java src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
index 5f7459a..e279c86 100644
|
|
|
|
| 25 | 25 | * @since 19640 |
| 26 | 26 | */ |
| 27 | 27 | public class AlignGridRotationAction extends JosmAction { |
| | 28 | |
| | 29 | private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0]; |
| 28 | 30 | |
| 29 | 31 | /** |
| 30 | 32 | * Constructs a new {@code AlignGridRotationAction}. |
| … |
… |
|
| 43 | 45 | */ |
| 44 | 46 | static EastNorth[] getSelectedDirection(DataSet ds) { |
| 45 | 47 | if (ds == null) { |
| 46 | | return null; |
| | 48 | return EMPTY_DIRECTION; |
| 47 | 49 | } |
| 48 | | Collection<Way> ways = ds.getSelectedWays(); |
| 49 | | Collection<Node> nodes = ds.getSelectedNodes(); |
| | 50 | final Collection<Way> ways = ds.getSelectedWays(); |
| | 51 | final Collection<Node> nodes = ds.getSelectedNodes(); |
| | 52 | |
| | 53 | final int waysSize = ways.size(); |
| | 54 | final int nodesSize = nodes.size(); |
| | 55 | |
| 50 | 56 | Node a = null; |
| 51 | 57 | Node b = null; |
| 52 | | if (ways.size() == 1 && nodes.isEmpty()) { |
| 53 | | Way w = ways.iterator().next(); |
| | 58 | if (waysSize == 1 && nodesSize ==0) { |
| | 59 | final Way w = ways.iterator().next(); |
| 54 | 60 | if (w.getNodesCount() >= 2) { |
| 55 | 61 | a = w.firstNode(); |
| 56 | 62 | b = w.isClosed() ? w.getNode(1) : w.lastNode(); |
| 57 | 63 | } |
| 58 | | } else if (nodes.size() == 2 && ways.isEmpty()) { |
| 59 | | Iterator<Node> it = nodes.iterator(); |
| | 64 | } else if (nodesSize == 2 && waysSize ==0) { |
| | 65 | final Iterator<Node> it = nodes.iterator(); |
| 60 | 66 | a = it.next(); |
| 61 | 67 | b = it.next(); |
| 62 | 68 | } |
| 63 | 69 | if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) { |
| 64 | | return null; |
| | 70 | return EMPTY_DIRECTION; |
| 65 | 71 | } |
| 66 | | EastNorth[] result = {a.getEastNorth(), b.getEastNorth()}; |
| 67 | | return result[0].equalsEpsilon(result[1], 1e-9) ? null : result; |
| | 72 | |
| | 73 | final EastNorth[] result = {a.getEastNorth(), b.getEastNorth()}; |
| | 74 | return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result; |
| 68 | 75 | } |
| 69 | 76 | |
| 70 | 77 | /** |
| … |
… |
|
| 85 | 92 | |
| 86 | 93 | @Override |
| 87 | 94 | protected void updateEnabledState() { |
| 88 | | setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()) != null); |
| | 95 | setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length > 0); |
| 89 | 96 | } |
| 90 | 97 | |
| 91 | 98 | @Override |
| … |
… |
|
| 95 | 102 | |
| 96 | 103 | @Override |
| 97 | 104 | public void actionPerformed(ActionEvent e) { |
| 98 | | EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet()); |
| 99 | | if (direction == null) { |
| | 105 | final EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet()); |
| | 106 | if (direction.length == 0) { |
| 100 | 107 | return; |
| 101 | 108 | } |
| 102 | 109 | MapGridPaintable.ROTATION.put(rotationOf(direction[0], direction[1])); |
diff --git test/unit/org/openstreetmap/josm/actions/GridActionsTest.java test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
index 37e9d66..557abca 100644
|
|
|
|
| 1 | 1 | // License: GPL. For details, see LICENSE file. |
| 2 | 2 | package org.openstreetmap.josm.actions; |
| 3 | 3 | |
| | 4 | import static org.junit.Assert.assertArrayEquals; |
| 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | 6 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | 7 | import static org.junit.jupiter.api.Assertions.assertNull; |
| … |
… |
|
| 164 | 165 | ds.addPrimitive(w); |
| 165 | 166 | |
| 166 | 167 | ds.setSelected(); |
| 167 | | assertNull(AlignGridRotationAction.getSelectedDirection(ds)); |
| | 168 | assertArrayEquals(new EastNorth[0], AlignGridRotationAction.getSelectedDirection(ds)); |
| 168 | 169 | AlignGridRotationAction action = new AlignGridRotationAction(); |
| 169 | 170 | assertFalse(action.isEnabled()); |
| 170 | 171 | ds.setSelected(a); |