Index: src/org/openstreetmap/josm/actions/DirectAllWaySegmentsAction.java
===================================================================
--- src/org/openstreetmap/josm/actions/DirectAllWaySegmentsAction.java	(revision 0)
+++ src/org/openstreetmap/josm/actions/DirectAllWaySegmentsAction.java	(revision 0)
@@ -0,0 +1,169 @@
+package org.openstreetmap.josm.actions;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import javax.swing.JOptionPane;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.SequenceCommand;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Segment;
+import org.openstreetmap.josm.data.osm.Way;
+
+public class DirectAllWaySegmentsAction extends JosmAction {
+	protected boolean reverseDirection = false;
+	
+	public DirectAllWaySegmentsAction() {
+		super(tr("Direct all way segments"), "reorder", tr("Try to direct all way segments so that they face the same direction."), KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK, true);
+    }
+	
+	protected DirectAllWaySegmentsAction(String name, String iconName, String tooltip, int shortCut, int modifier, boolean register) { 
+		super(name, iconName, tooltip, shortCut, modifier, register);
+	}
+	
+	/**
+	 * This method first sorts all the segments in a way, then makes sure that all 
+	 * the segments are facing the same direction as the first one.
+	 */
+	public void actionPerformed(ActionEvent e) {
+		Collection<Way> ways = new LinkedList<Way>();
+		for (OsmPrimitive osm : Main.ds.getSelected())
+			if (osm instanceof Way)
+				ways.add((Way)osm);
+		if (ways.size() < 1) {
+			JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way."));
+			return;
+		}
+		if (ways.size() > 1) {
+			int answer = JOptionPane.showConfirmDialog(Main.parent, tr("You selected more than one way. Reorder the segments of " + ways.size() + " ways?"), tr("Reorder segments"), JOptionPane.OK_CANCEL_OPTION);
+			if (answer != JOptionPane.OK_OPTION)
+				return;
+		}
+		for (Way way : ways) {
+			if ( !way.isIncomplete() && way.segments.size() > 1 )
+			{			
+				final LinkedList<Segment> sel = new LinkedList<Segment>(sortSegments(new LinkedList<Segment>(way.segments)));   	
+	
+		    	Collection<Command> c = new LinkedList<Command>();
+	
+		    	boolean direction = false;
+		    	// work out the "average" direction of the way, we use this to direct the rest of the segments
+		    	int dirCounter = 0;
+		    	for(int i = 0; i < sel.size() - 1; i++)
+		    	{
+		    		Segment firstSegment = sel.get(i);
+		    		Segment secondSegment = sel.get(i+1);
+		    		if ( firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to ) // direction = true when 'from' is the first node in the Way
+		    			dirCounter++;
+		    		else
+		    			dirCounter--;
+		    	}
+		    	if ( dirCounter <= 0 )
+		    		direction = false;
+		    	else
+		    		direction = true;
+		    	
+		    	if ( reverseDirection )
+		    		direction = !direction;
+	    	
+	    		Node lastNode = null;
+
+	    		// we need to calculate what the first node in the way is, we work from there
+	    		Segment firstSegment = sel.getFirst();
+	    		Segment secondSegment = sel.get(1);
+	    		if ( firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to )
+	    			lastNode = firstSegment.from;
+	    		else
+	    			lastNode = firstSegment.to;
+	    		
+	    		// go through each segment and fip them correctly
+		    	for (Segment s : sel) {
+	    			Segment snew = new Segment(s);
+	    			boolean segDirection = s.from == lastNode;
+	    			// segDirection = true when the 'from' node occurs before the 'to' node in the Way 
+	    			if ( direction != segDirection )
+	    			{    			
+	    				// reverse the node's direction
+	    				Node n = snew.from;
+	    				snew.from = snew.to;
+	    				snew.to = n;
+	    				c.add(new ChangeCommand(s, snew));
+	    			}	
+	    			
+		    		if ( direction ) // if its facing forwards,
+		    			lastNode = snew.to; // our next node is the 'to' one
+		    		else
+		    			lastNode = snew.from; // otherwise its the 'from' one
+		    	}
+
+		    	LinkedList<Segment> segments = new LinkedList<Segment>();
+		    	
+		    	// Now we recreate the segment list, in the correct order of the direction
+	    		for (Segment s : sel) 
+	    			if ( !direction ) 
+	    				segments.addFirst(s);
+	    			else
+	    				segments.addLast(s);
+	    			
+	    		// Check if the new segment list is actually different to the old one
+	    		// before we go and add a change command for it
+	    		for(int i = 0; i < segments.size(); i++)
+	    			if ( way.segments.get(i) != segments.get(i) )
+	    			{
+				    	Way newWay2 = new Way(way);
+				    	newWay2.segments.clear();
+						newWay2.segments.addAll(segments);
+						c.add(new ChangeCommand(way, newWay2));
+						break;
+	    			}
+	
+	    		// Check we've got some change commands before we add a sequence command
+	    		if ( c.size() != 0 )
+	    			Main.main.editLayer().add(new SequenceCommand(tr("Direct all way segments"), c));
+			}
+		}
+		Main.map.repaint();
+	}
+
+	/**
+	 * This sort is based on the sort in ReorderAction, but it works iresepective
+	 * of the direction of the segments. This produces a sort that can be useful
+	 * even if the segments are facing the wrong direction.
+	 */
+	public static LinkedList<Segment> sortSegments(LinkedList<Segment> segments) {
+		LinkedList<Segment> sortedSegments = new LinkedList<Segment>();
+		while (!segments.isEmpty()) {
+			LinkedList<Segment> pivotList = new LinkedList<Segment>();
+			pivotList.add(segments.getFirst());
+			segments.removeFirst();
+			for (boolean found = true; found;) {
+				found = false;
+				for (Iterator<Segment> it = segments.iterator(); it.hasNext();) {
+					Segment ls = it.next();
+					if (ls.incomplete)
+						continue; // incomplete segments are never added to a new way
+					if (ls.from == pivotList.getLast().to || ls.to == pivotList.getLast().to || ls.from == pivotList.getLast().from || ls.to == pivotList.getLast().from) {
+						pivotList.addLast(ls);
+						it.remove();
+						found = true;
+					} else if (ls.to == pivotList.getFirst().from || ls.from == pivotList.getFirst().from || ls.to == pivotList.getFirst().to || ls.from == pivotList.getFirst().to) {
+						pivotList.addFirst(ls);
+						it.remove(); 
+						found = true;
+					}
+				}
+			}
+			sortedSegments.addAll(pivotList);
+		}
+	    return sortedSegments;
+    }
+}
