Index: /trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java	(revision 2595)
+++ /trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java	(revision 2596)
@@ -11,4 +11,5 @@
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -151,13 +152,44 @@
             }
             else  {
-                if (nodeList.size() == 2) {
-                    orthogonalize(wayDataList, nodeList);
-                }
-                else if (nodeList.isEmpty()) {
-                    orthogonalize(wayDataList, nodeList);
-                }
-                else {
+                if (nodeList.size() == 2 || nodeList.isEmpty()) {
+                    OrthogonalizeAction.rememberMovements.clear();
+                    final Collection<Command> commands = new LinkedList<Command>();
+
+                    if (nodeList.size() == 2) {  // fixed direction
+                        commands.addAll(orthogonalize(wayDataList, nodeList));
+                    }
+                    else if (nodeList.isEmpty()) {
+                        // collect groups of ways with common nodes and orthogonalize each group separately.
+                        ArrayList<ArrayList<WayData>> groups = new ArrayList<ArrayList<WayData>>();
+                        for (WayData w: wayDataList) {
+                            boolean add = false;
+                            for (ArrayList<WayData> g: groups) {
+                                for (WayData groupedWay: g) {
+                                    if (!Collections.disjoint(w.way.getNodes(), groupedWay.way.getNodes())) {
+                                        add = true;
+                                        break;
+                                    }
+                                }
+                                if (add) {
+                                    g.add(w);
+                                }
+                            }
+                            if (!add) {
+                                ArrayList<WayData> newGroup = new ArrayList<WayData>();
+                                newGroup.add(w);
+                                groups.add(newGroup);
+                            }
+                        }
+                        for (ArrayList<WayData> g: groups) {
+                            commands.addAll(orthogonalize(g, nodeList));
+                        }
+                    } else
+                        throw new IllegalStateException();
+                    
+                    Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), commands));
+                    Main.map.repaint();
+                    
+                } else
                     throw new InvalidUserInputException("usage");
-                }
             }
         } catch (InvalidUserInputException ex) {
@@ -199,5 +231,5 @@
      *
      **/
-    private static void orthogonalize(ArrayList<WayData> wayDataList, ArrayList<Node> headingNodes)
+    private static Collection<Command> orthogonalize(ArrayList<WayData> wayDataList, ArrayList<Node> headingNodes)
         throws InvalidUserInputException
     {
@@ -247,5 +279,5 @@
         final HashMap<Node, Double> nY = new HashMap<Node, Double>();
 
-        // caluclate the centroid of all nodes
+        // calculate the centroid of all nodes
         // it is used as rotation center
         EastNorth pivot = new EastNorth(0., 0.);
@@ -325,5 +357,5 @@
         // rotate back and log the change
         final Collection<Command> commands = new LinkedList<Command>();
-        OrthogonalizeAction.rememberMovements.clear();
+//        OrthogonalizeAction.rememberMovements.clear();
         for (Node n: allNodes) {
             EastNorth tmp = new EastNorth(nX.get(n), nY.get(n));
@@ -343,6 +375,5 @@
             }
         }
-        Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), commands));
-        Main.map.repaint();
+        return commands;
     }
 
