Index: /trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java	(revision 2575)
+++ /trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java	(revision 2575)
@@ -0,0 +1,900 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package org.openstreetmap.josm.actions;
+
+import static org.openstreetmap.josm.tools.I18n.marktr;
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.GridBagLayout;
+import java.awt.Polygon;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.awt.geom.Line2D;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.Map.Entry;
+
+import javax.swing.Box;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.CombineWayAction;
+import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.actions.ReverseWayAction;
+import org.openstreetmap.josm.actions.SplitWayAction;
+import org.openstreetmap.josm.command.AddCommand;
+import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.DeleteCommand;
+import org.openstreetmap.josm.command.SequenceCommand;
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.UndoRedoHandler;
+import org.openstreetmap.josm.data.coor.EastNorth;
+import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.DataSource;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.RelationMember;
+import org.openstreetmap.josm.data.osm.TigerUtils;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.gui.ExtendedDialog;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.tools.GBC;
+import org.openstreetmap.josm.tools.Shortcut;
+
+public class JoinAreasAction extends JosmAction {
+    // This will be used to commit commands and unite them into one large command sequence at the end
+    private LinkedList<Command> cmds = new LinkedList<Command>();
+    private int cmdsCount = 0;
+
+    // HelperClass
+    // Saves a node and two positions where to insert the node into the ways
+    private class NodeToSegs implements Comparable<NodeToSegs> {
+        public int pos;
+        public Node n;
+        public double dis;
+        public NodeToSegs(int pos, Node n, LatLon dis) {
+            this.pos = pos;
+            this.n = n;
+            this.dis = n.getCoor().greatCircleDistance(dis);
+        }
+
+        public int compareTo(NodeToSegs o) {
+            if(this.pos == o.pos)
+                return (this.dis - o.dis) > 0 ? 1 : -1;
+            return this.pos - o.pos;
+        }
+    };
+
+    // HelperClass
+    // Saves a relation and a role an OsmPrimitve was part of until it was stripped from all relations
+    private class RelationRole {
+        public Relation rel;
+        public String role;
+        public RelationRole(Relation rel, String role) {
+            this.rel = rel;
+            this.role = role;
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            if (!(other instanceof RelationRole)) return false;
+            RelationRole otherMember = (RelationRole) other;
+            return otherMember.role.equals(role) && otherMember.rel.equals(rel);
+        }
+    }
+
+    // Adds the menu entry, Shortcuts, etc.
+    public JoinAreasAction() {
+        super(tr("Join overlapping Areas"), "joinareas", tr("Joins areas that overlap each other"), Shortcut.registerShortcut("tools:joinareas", tr("Tool: {0}", tr("Join overlapping Areas")),
+        KeyEvent.VK_J, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
+    }
+
+    /**
+     * Gets called whenever the shortcut is pressed or the menu entry is selected
+     * Checks whether the selected objects are suitable to join and joins them if so
+     */
+    public void actionPerformed(ActionEvent e) {
+        Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelectedWays();
+
+        int ways = 0;
+        Way[] selWays = new Way[2];
+
+        LinkedList<Bounds> bounds = new LinkedList<Bounds>();
+        OsmDataLayer dataLayer = Main.map.mapView.getEditLayer();
+        for (DataSource ds : dataLayer.data.dataSources) {
+            if (ds.bounds != null)
+                bounds.add(ds.bounds);
+        }
+
+        boolean askedAlready = false;
+        for (OsmPrimitive prim : selection) {
+            Way way = (Way) prim;
+
+            // Too many ways
+            if(ways == 2) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Only up to two areas can be joined at the moment."));
+                return;
+            }
+
+            if(!way.isClosed()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("\"{0}\" is not closed and therefore can't be joined.", way.getName()));
+                return;
+            }
+
+            // This is copied from SimplifyAction and should be probably ported to tools
+            for (Node node : way.getNodes()) {
+                if(askedAlready) break;
+                boolean isInsideOneBoundingBox = false;
+                for (Bounds b : bounds) {
+                    if (b.contains(node.getCoor())) {
+                        isInsideOneBoundingBox = true;
+                        break;
+                    }
+                }
+
+                if (!isInsideOneBoundingBox) {
+                    int option = JOptionPane.showConfirmDialog(Main.parent,
+                            tr("The selected way(s) have nodes outside of the downloaded data region.\n"
+                                    + "This can lead to nodes being deleted accidentally.\n"
+                                    + "Are you really sure to continue?"),
+                            tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION,
+                            JOptionPane.WARNING_MESSAGE);
+
+                    if (option != JOptionPane.YES_OPTION) return;
+                    askedAlready = true;
+                    break;
+                }
+            }
+
+            selWays[ways] = way;
+            ways++;
+        }
+
+        if (ways < 1) {
+            JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one closed way the should be joined."));
+            return;
+        }
+
+        if(joinAreas(selWays[0], selWays[ways == 2 ? 1 : 0])) {
+            Main.map.mapView.repaint();
+            DataSet ds = Main.main.getCurrentDataSet();
+            ds.fireSelectionChanged();
+        } else
+            JOptionPane.showMessageDialog(Main.parent, tr("No intersection found. Nothing was changed."));
+    }
+
+    /**
+     * Will join two overlapping areas
+     * @param Way First way/area
+     * @param Way Second way/area
+     * @return boolean Whether to display the "no operation" message
+     */
+    private boolean joinAreas(Way a, Way b) {
+        // Fix self-overlapping first or other errors
+        boolean same = a.equals(b);
+        boolean hadChanges = false;
+        if(!same) {
+            if(checkForTagConflicts(a, b)) return true; // User aborted, so don't warn again
+            hadChanges = joinAreas(a, a);
+            hadChanges = joinAreas(b, b) || hadChanges;
+        }
+
+        ArrayList<OsmPrimitive> nodes = addIntersections(a, b);
+        if(nodes.size() == 0) return hadChanges;
+        commitCommands(marktr("Added node on all intersections"));
+
+        // Remove ways from all relations so ways can be combined/split quietly
+        ArrayList<RelationRole> relations = removeFromRelations(a);
+        if(!same) relations.addAll(removeFromRelations(b));
+
+        // Don't warn now, because it will really look corrupted
+        boolean warnAboutRelations = relations.size() > 0;
+
+        Collection<Way> allWays = splitWaysOnNodes(a, b, nodes);
+
+        // Find all nodes and inner ways save them to a list
+        Collection<Node> allNodes = getNodesFromWays(allWays);
+        Collection<Way> innerWays = findInnerWays(allWays, allNodes);
+
+        // Join outer ways
+        Way outerWay = joinOuterWays(allWays, innerWays);
+
+        // Fix Multipolygons if there are any
+        Collection<Way> newInnerWays = fixMultigons(innerWays, outerWay);
+
+        // Delete the remaining inner ways
+        if(innerWays != null && innerWays.size() > 0)
+            cmds.add(DeleteCommand.delete(Main.map.mapView.getEditLayer(), innerWays, true));
+        commitCommands(marktr("Delete Ways that are not part of an inner multipolygon"));
+
+        // We can attach our new multipolygon relation and pretend it has always been there
+        addOwnMultigonRelation(newInnerWays, outerWay, relations);
+        fixRelations(relations, outerWay);
+        commitCommands(marktr("Fix relations"));
+
+        stripTags(newInnerWays);
+        makeCommitsOneAction(
+            same
+                ? marktr("Joined self-overlapping area")
+                : marktr("Joined overlapping areas")
+        );
+
+        if(warnAboutRelations)
+            JOptionPane.showMessageDialog(Main.parent, tr("Some of the ways were part of relations that have been modified. Please verify no errors have been introduced."));
+
+        return true;
+    }
+
+    /**
+     * Checks if tags of two given ways differ, and presents the user a dialog to solve conflicts
+     * @param Way First way to check
+     * @param Way Second Way to check
+     * @return boolean True if not all conflicts could be resolved, False if everything's fine
+     */
+    private boolean checkForTagConflicts(Way a, Way b) {
+        ArrayList<Way> ways = new ArrayList<Way>();
+        ways.add(a);
+        ways.add(b);
+
+        // This is mostly copied and pasted from CombineWayAction.java and one day should be moved into tools
+        Map<String, Set<String>> props = new TreeMap<String, Set<String>>();
+        for (Way w : ways) {
+            for (Entry<String,String> e : w.entrySet()) {
+                if (!props.containsKey(e.getKey()))
+                    props.put(e.getKey(), new TreeSet<String>());
+                props.get(e.getKey()).add(e.getValue());
+            }
+        }
+
+        Way ax = new Way(a);
+        Way bx = new Way(b);
+
+        Map<String, JComboBox> components = new HashMap<String, JComboBox>();
+        JPanel p = new JPanel(new GridBagLayout());
+        for (Entry<String, Set<String>> e : props.entrySet()) {
+            if (TigerUtils.isTigerTag(e.getKey())) {
+                String combined = TigerUtils.combineTags(e.getKey(), e.getValue());
+                ax.put(e.getKey(), combined);
+                bx.put(e.getKey(), combined);
+            } else if (e.getValue().size() > 1) {
+                if("created_by".equals(e.getKey()))
+                {
+                    ax.put("created_by", "JOSM");
+                    bx.put("created_by", "JOSM");
+                } else {
+                    JComboBox c = new JComboBox(e.getValue().toArray());
+                    c.setEditable(true);
+                    p.add(new JLabel(e.getKey()), GBC.std());
+                    p.add(Box.createHorizontalStrut(10), GBC.std());
+                    p.add(c, GBC.eol());
+                    components.put(e.getKey(), c);
+                }
+            } else {
+                String val = e.getValue().iterator().next();
+                ax.put(e.getKey(), val);
+                bx.put(e.getKey(), val);
+            }
+        }
+
+        if (components.isEmpty())
+            return false; // No conflicts found
+
+        ExtendedDialog ed = new ExtendedDialog(Main.parent,
+                tr("Enter values for all conflicts."),
+                new String[] {tr("Solve Conflicts"), tr("Cancel")});
+        ed.setButtonIcons(new String[] {"dialogs/conflict.png", "cancel.png"});
+        ed.setContent(p);
+        ed.showDialog();
+
+        if (ed.getValue() != 1) return true; // user cancel, unresolvable conflicts
+
+        for (Entry<String, JComboBox> e : components.entrySet()) {
+            String val = e.getValue().getEditor().getItem().toString();
+            ax.put(e.getKey(), val);
+            bx.put(e.getKey(), val);
+        }
+
+        cmds.add(new ChangeCommand(a, ax));
+        cmds.add(new ChangeCommand(b, bx));
+        commitCommands(marktr("Fix tag conflicts"));
+        return false;
+    }
+
+    /**
+     * Will find all intersection and add nodes there for two given ways
+     * @param Way First way
+     * @param Way Second way
+     * @return ArrayList<OsmPrimitive> List of new nodes
+     */
+    private ArrayList<OsmPrimitive> addIntersections(Way a, Way b) {
+        boolean same = a.equals(b);
+        int nodesSizeA = a.getNodesCount();
+        int nodesSizeB = b.getNodesCount();
+
+        // We use OsmPrimitive here instead of Node because we later need to split a way at these nodes.
+        // With OsmPrimitve we can simply add the way and don't have to loop over the nodes
+        ArrayList<OsmPrimitive> nodes = new ArrayList<OsmPrimitive>();
+        ArrayList<NodeToSegs> nodesA = new ArrayList<NodeToSegs>();
+        ArrayList<NodeToSegs> nodesB = new ArrayList<NodeToSegs>();
+
+        for (int i = (same ? 1 : 0); i < nodesSizeA - 1; i++) {
+            for (int j = (same ? i + 2 : 0); j < nodesSizeB - 1; j++) {
+                // Avoid re-adding nodes that already exist on (some) intersections
+                if(a.getNode(i).equals(b.getNode(j)) || a.getNode(i+1).equals(b.getNode(j)))   {
+                    nodes.add(b.getNode(j));
+                    continue;
+                } else
+                if(a.getNode(i).equals(b.getNode(j+1)) || a.getNode(i+1).equals(b.getNode(j+1))) {
+                    nodes.add(b.getNode(j+1));
+                    continue;
+                }
+                LatLon intersection = getLineLineIntersection(
+                        a.getNode(i)  .getEastNorth().east(), a.getNode(i)  .getEastNorth().north(),
+                        a.getNode(i+1).getEastNorth().east(), a.getNode(i+1).getEastNorth().north(),
+                        b.getNode(j)  .getEastNorth().east(), b.getNode(j)  .getEastNorth().north(),
+                        b.getNode(j+1).getEastNorth().east(), b.getNode(j+1).getEastNorth().north());
+                if(intersection == null) continue;
+
+                // Create the node. Adding them to the ways must be delayed because we still loop over them
+                Node n = new Node(intersection);
+                cmds.add(new AddCommand(n));
+                nodes.add(n);
+                // The distance is needed to sort and add the nodes in direction of the way
+                nodesA.add(new NodeToSegs(i,  n, a.getNode(i).getCoor()));
+                if(same)
+                    nodesA.add(new NodeToSegs(j,  n, a.getNode(j).getCoor()));
+                else
+                    nodesB.add(new NodeToSegs(j,  n, b.getNode(j).getCoor()));
+            }
+        }
+
+        addNodesToWay(a, nodesA);
+        if(!same) addNodesToWay(b, nodesB);
+
+        return nodes;
+    }
+
+    /**
+     * Finds the intersection of two lines
+     * @return LatLon null if no intersection was found, the LatLon coordinates of the intersection otherwise
+     */
+    static private LatLon getLineLineIntersection(
+                double x1, double y1, double x2, double y2,
+                double x3, double y3, double x4, double y4) {
+
+        if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
+
+        // Convert line from (point, point) form to ax+by=c
+        double a1 = y2 - y1;
+        double b1 = x1 - x2;
+        double c1 = x2*y1 - x1*y2;
+
+        double a2 = y4 - y3;
+        double b2 = x3 - x4;
+        double c2 = x4*y3 - x3*y4;
+
+        // Solve the equations
+        double det = a1*b2 - a2*b1;
+        if(det == 0) return null; // Lines are parallel
+
+        return Main.proj.eastNorth2latlon(new EastNorth(
+            (b1*c2 - b2*c1)/det,
+            (a2*c1 -a1*c2)/det
+        ));
+    }
+
+    /**
+     * Inserts given nodes with positions into the given ways
+     * @param Way The way to insert the nodes into
+     * @param Collection<NodeToSegs> The list of nodes with positions to insert
+     */
+    private void addNodesToWay(Way a, ArrayList<NodeToSegs> nodes) {
+        Way ax=new Way(a);
+        Collections.sort(nodes);
+
+        int numOfAdds = 1;
+        for(NodeToSegs n : nodes) {
+            ax.addNode(n.pos + numOfAdds, n.n);
+            numOfAdds++;
+        }
+
+        cmds.add(new ChangeCommand(a, ax));
+    }
+
+    /**
+     * Commits the command list with a description
+     * @param String The description of what the commands do
+     */
+    private void commitCommands(String description) {
+        switch(cmds.size()) {
+            case 0:
+                return;
+            case 1:
+                Main.main.undoRedo.add(cmds.getFirst());
+                break;
+            default:
+                Command c = new SequenceCommand(tr(description), cmds);
+                Main.main.undoRedo.add(c);
+                break;
+        }
+
+        cmds.clear();
+        cmdsCount++;
+    }
+
+    /**
+     * Removes a given OsmPrimitive from all relations
+     * @param OsmPrimitive Element to remove from all relations
+     * @return ArrayList<RelationRole> List of relations with roles the primitives was part of
+     */
+    private ArrayList<RelationRole> removeFromRelations(OsmPrimitive osm) {
+        ArrayList<RelationRole> result = new ArrayList<RelationRole>();
+        for (Relation r : Main.main.getCurrentDataSet().getRelations()) {
+            if (r.isDeleted() || r.incomplete) continue;
+            for (RelationMember rm : r.getMembers()) {
+                if (rm.getMember() != osm) continue;
+
+                Relation newRel = new Relation(r);
+                List<RelationMember> members = newRel.getMembers();
+                members.remove(rm);
+                newRel.setMembers(members);
+
+                cmds.add(new ChangeCommand(r, newRel));
+                RelationRole saverel =  new RelationRole(r, rm.getRole());
+                if(!result.contains(saverel)) result.add(saverel);
+                break;
+            }
+        }
+
+        commitCommands(marktr("Removed Element from Relations"));
+        return result;
+    }
+
+    /**
+     * This is a hacky implementation to make use of the splitWayAction code and
+     * should be improved. SplitWayAction needs to expose its splitWay function though.
+     */
+    private Collection<Way> splitWaysOnNodes(Way a, Way b, Collection<OsmPrimitive> nodes) {
+        ArrayList<Way> ways = new ArrayList<Way>();
+        ways.add(a);
+        if(!a.equals(b)) ways.add(b);
+
+        List<OsmPrimitive> affected = new ArrayList<OsmPrimitive>();
+        for (Way way : ways) {
+            nodes.add(way);
+            Main.main.getCurrentDataSet().setSelected(nodes);
+            nodes.remove(way);
+            new SplitWayAction().actionPerformed(null);
+            cmdsCount++;
+            affected.addAll(Main.main.getCurrentDataSet().getSelectedWays());
+        }
+        return osmprim2way(affected);
+    }
+
+    /**
+     * Converts a list of OsmPrimitives to a list of Ways
+     * @param Collection<OsmPrimitive> The OsmPrimitives list that's needed as a list of Ways
+     * @return Collection<Way> The list as list of Ways
+     */
+    static private Collection<Way> osmprim2way(Collection<OsmPrimitive> ways) {
+        Collection<Way> result = new ArrayList<Way>();
+        for(OsmPrimitive w: ways) {
+            if(w instanceof Way) result.add((Way) w);
+        }
+        return result;
+    }
+
+    /**
+     * Returns all nodes for given ways
+     * @param Collection<Way> The list of ways which nodes are to be returned
+     * @return Collection<Node> The list of nodes the ways contain
+     */
+    private Collection<Node> getNodesFromWays(Collection<Way> ways) {
+        Collection<Node> allNodes = new ArrayList<Node>();
+        for(Way w: ways) allNodes.addAll(w.getNodes());
+        return allNodes;
+    }
+
+    /**
+     * Finds all inner ways for a given list of Ways and Nodes from a multigon by constructing a polygon
+     * for each way, looking for inner nodes that are not part of this way. If a node is found, all ways
+     * containing this node are added to the list
+     * @param Collection<Way> A list of (splitted) ways that form a multigon
+     * @param Collection<Node> A list of nodes that belong to the multigon
+     * @return Collection<Way> A list of ways that are positioned inside the outer borders of the multigon
+     */
+    private Collection<Way> findInnerWays(Collection<Way> multigonWays, Collection<Node> multigonNodes) {
+        Collection<Way> innerWays = new ArrayList<Way>();
+        for(Way w: multigonWays) {
+            Polygon poly = new Polygon();
+            for(Node n: (w).getNodes()) poly.addPoint(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon()));
+
+            for(Node n: multigonNodes) {
+                if(!(w).containsNode(n) && poly.contains(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon()))) {
+                    getWaysByNode(innerWays, multigonWays, n);
+                }
+            }
+        }
+
+        return innerWays;
+    }
+
+    // Polygon only supports int coordinates, so convert them
+    private int latlonToXY(double val) {
+        return (int)Math.round(val*1000000);
+    }
+
+    /**
+     * Finds all ways that contain the given node.
+     * @param Collection<Way> A list to which matching ways will be added
+     * @param Collection<Way> A list of ways to check
+     * @param Node The node the ways should be checked against
+     */
+    private void getWaysByNode(Collection<Way> innerWays, Collection<Way> w, Node n) {
+        for(Way way : w) {
+            if(!(way).containsNode(n)) continue;
+            if(!innerWays.contains(way)) innerWays.add(way); // Will need this later for multigons
+        }
+    }
+
+    /**
+     * Joins the two outer ways and deletes all short ways that can't be part of a multipolygon anyway
+     * @param Collection<OsmPrimitive> The list of all ways that belong to that multigon
+     * @param Collection<Way> The list of inner ways that belong to that multigon
+     * @return Way The newly created outer way
+     */
+    private Way joinOuterWays(Collection<Way> multigonWays, Collection<Way> innerWays) {
+        ArrayList<Way> join = new ArrayList<Way>();
+        for(Way w: multigonWays) {
+            // Skip inner ways
+            if(innerWays.contains(w)) continue;
+
+            if(w.getNodesCount() <= 2)
+                cmds.add(new DeleteCommand(w));
+            else
+                join.add(w);
+        }
+
+        commitCommands(marktr("Join Areas: Remove Short Ways"));
+        return closeWay(joinWays(join));
+    }
+
+    /**
+     * Ensures a way is closed. If it isn't, last and first node are connected.
+     * @param Way the way to ensure it's closed
+     * @return Way The joined way.
+     */
+    private Way closeWay(Way w) {
+        if(w.isClosed())
+            return w;
+        Main.main.getCurrentDataSet().setSelected(w);
+        Way wnew = new Way(w);
+        wnew.addNode(wnew.firstNode());
+        cmds.add(new ChangeCommand(w, wnew));
+        commitCommands(marktr("Closed Way"));
+        return (Way)(Main.main.getCurrentDataSet().getSelectedWays().toArray())[0];
+    }
+
+    /**
+     * Joins a list of ways (using CombineWayAction and ReverseWayAction if necessary to quiet the former)
+     * @param ArrayList<Way> The list of ways to join
+     * @return Way The newly created way
+     */
+    private Way joinWays(ArrayList<Way> ways) {
+        if(ways.size() < 2) return ways.get(0);
+
+        // This will turn ways so all of them point in the same direction and CombineAction won't bug
+        // the user about this.
+        Way a = null;
+        for(Way b : ways) {
+            if(a == null) {
+                a = b;
+                continue;
+            }
+            if(a.getNode(0).equals(b.getNode(0)) ||
+               a.getNode(a.getNodesCount()-1).equals(b.getNode(b.getNodesCount()-1))) {
+                Main.main.getCurrentDataSet().setSelected(b);
+                new ReverseWayAction().actionPerformed(null);
+                cmdsCount++;
+            }
+            a = b;
+        }
+        Main.main.getCurrentDataSet().setSelected(ways);
+        // TODO: It might be possible that a confirmation dialog is presented even after reversing (for
+        // "strange" ways). If the user cancels this, makeCommitsOneAction will wrongly consume a previous
+        // action. Make CombineWayAction either silent or expose its combining capabilities.
+        new CombineWayAction().actionPerformed(null);
+        cmdsCount++;
+        return (Way)(Main.main.getCurrentDataSet().getSelectedWays().toArray())[0];
+    }
+
+    /**
+     * Finds all ways that may be part of a multipolygon relation and removes them from the given list.
+     * It will automatically combine "good" ways
+     * @param Collection<Way> The list of inner ways to check
+     * @param Way The newly created outer way
+     * @return ArrayList<Way> The List of newly created inner ways
+     */
+    private ArrayList<Way> fixMultigons(Collection<Way> uninterestingWays, Way outerWay) {
+        Collection<Node> innerNodes = getNodesFromWays(uninterestingWays);
+        Collection<Node> outerNodes = outerWay.getNodes();
+
+        // The newly created inner ways. uninterestingWays is passed by reference and therefore modified in-place
+        ArrayList<Way> newInnerWays = new ArrayList<Way>();
+
+        // Now we need to find all inner ways that contain a remaining node, but no outer nodes
+        // Remaining nodes are those that contain to more than one way. All nodes that belong to an
+        // inner multigon part will have at least two ways, so we can use this to find which ways do
+        // belong to the multigon.
+        ArrayList<Way> possibleWays = new ArrayList<Way>();
+        wayIterator: for(Way w : uninterestingWays) {
+            boolean hasInnerNodes = false;
+            for(Node n : w.getNodes()) {
+                if(outerNodes.contains(n)) continue wayIterator;
+                if(!hasInnerNodes && innerNodes.contains(n)) hasInnerNodes = true;
+            }
+            if(!hasInnerNodes || w.getNodesCount() < 2) continue;
+            possibleWays.add(w);
+        }
+
+        // This removes unnecessary ways that might have been added.
+        removeAlmostAlikeWays(possibleWays);
+        removePartlyUnconnectedWays(possibleWays);
+
+        // Join all ways that have one start/ending node in common
+        Way joined = null;
+        outerIterator: do {
+            joined = null;
+            for(Way w1 : possibleWays) {
+                if(w1.isClosed()) {
+                    if(!wayIsCollapsed(w1)) {
+                        uninterestingWays.remove(w1);
+                        newInnerWays.add(w1);
+                    }
+                    joined = w1;
+                    possibleWays.remove(w1);
+                    continue outerIterator;
+                }
+                for(Way w2 : possibleWays) {
+                    // w2 cannot be closed, otherwise it would have been removed above
+                    if(!waysCanBeCombined(w1, w2)) continue;
+
+                    ArrayList<Way> joinThem = new ArrayList<Way>();
+                    joinThem.add(w1);
+                    joinThem.add(w2);
+                    uninterestingWays.removeAll(joinThem);
+                    possibleWays.removeAll(joinThem);
+
+                    // Although we joined the ways, we cannot simply assume that they are closed
+                    joined = joinWays(joinThem);
+                    uninterestingWays.add(joined);
+                    possibleWays.add(joined);
+                    continue outerIterator;
+                }
+            }
+        } while(joined != null);
+        return newInnerWays;
+    }
+
+    /**
+     * Removes almost alike ways (= ways that are on top of each other for all nodes)
+     * @param ArrayList<Way> the ways to remove almost-duplicates from
+     */
+    private void removeAlmostAlikeWays(ArrayList<Way> ways) {
+        Collection<Way> removables = new ArrayList<Way>();
+        outer: for(int i=0; i < ways.size(); i++) {
+            Way a = ways.get(i);
+            for(int j=i+1; j < ways.size(); j++) {
+                Way b = ways.get(j);
+                List<Node> revNodes = new ArrayList<Node>(b.getNodes());
+                Collections.reverse(revNodes);
+                if(a.getNodes().equals(b.getNodes()) || a.getNodes().equals(revNodes)) {
+                    removables.add(a);
+                    continue outer;
+                }
+            }
+        }
+        ways.removeAll(removables);
+    }
+
+    /**
+     * Removes ways from the given list whose starting or ending node doesn't
+     * connect to other ways from the same list (it's like removing spikes).
+     * @param ArrayList<Way> The list of ways to remove "spikes" from
+     */
+    private void removePartlyUnconnectedWays(ArrayList<Way> ways) {
+        List<Way> removables = new ArrayList<Way>();
+        for(Way a : ways) {
+            if(a.isClosed()) continue;
+            boolean connectedStart = false;
+            boolean connectedEnd = false;
+            for(Way b : ways) {
+                if(a.equals(b))
+                    continue;
+                if(b.isFirstLastNode(a.firstNode()))
+                    connectedStart = true;
+                if(b.isFirstLastNode(a.lastNode()))
+                    connectedEnd = true;
+            }
+            if(!connectedStart || !connectedEnd)
+                removables.add(a);
+        }
+        ways.removeAll(removables);
+    }
+
+    /**
+     * Checks if a way is collapsed (i.e. looks like <---->)
+     * @param Way A *closed* way to check if it is collapsed
+     * @return boolean If the closed way is collapsed or not
+     */
+    private boolean wayIsCollapsed(Way w) {
+        if(w.getNodesCount() <= 3) return true;
+
+        // If a way contains more than one node twice, it must be collapsed (only start/end node may be the same)
+        Way x = new Way(w);
+        int count = 0;
+        for(Node n : w.getNodes()) {
+            x.removeNode(n);
+            if(x.containsNode(n)) count++;
+            if(count == 2) return true;
+        }
+        return false;
+    }
+
+    /**
+     * Checks if two ways share one starting/ending node
+     * @param Way first way
+     * @param Way second way
+     * @return boolean Wheter the ways share a starting/ending node or not
+     */
+    private boolean waysCanBeCombined(Way w1, Way w2) {
+        if(w1.equals(w2)) return false;
+
+        if(w1.getNode(0).equals(w2.getNode(0))) return true;
+        if(w1.getNode(0).equals(w2.getNode(w2.getNodesCount()-1))) return true;
+
+        if(w1.getNode(w1.getNodesCount()-1).equals(w2.getNode(0))) return true;
+        if(w1.getNode(w1.getNodesCount()-1).equals(w2.getNode(w2.getNodesCount()-1))) return true;
+
+        return false;
+    }
+
+    /**
+     * Will add own multipolygon relation to the "previously existing" relations. Fixup is done by fixRelations
+     * @param Collection<Way> List of already closed inner ways
+     * @param Way The outer way
+     * @param ArrayList<RelationRole> The list of relation with roles to add own relation to
+     */
+    private void addOwnMultigonRelation(Collection<Way> inner, Way outer, ArrayList<RelationRole> rels) {
+        if(inner.size() == 0) return;
+        // Create new multipolygon relation and add all inner ways to it
+        Relation newRel = new Relation();
+        newRel.put("type", "multipolygon");
+        for(Way w : inner)
+            newRel.addMember(new RelationMember("inner", w));
+        cmds.add(new AddCommand(newRel));
+
+        // We don't add outer to the relation because it will be handed to fixRelations()
+        // which will then do the remaining work. Collections are passed by reference, so no
+        // need to return it
+        rels.add(new RelationRole(newRel, "outer"));
+        //return rels;
+    }
+
+    /**
+     * Adds the previously removed relations again to the outer way. If there are multiple multipolygon
+     * relations where the joined areas were in "outer" role a new relation is created instead with all
+     * members of both. This function depends on multigon relations to be valid already, it won't fix them.
+     * @param ArrayList<RelationRole> List of relations with roles the (original) ways were part of
+     * @param Way The newly created outer area/way
+     */
+    private void fixRelations(ArrayList<RelationRole> rels, Way outer) {
+        ArrayList<RelationRole> multiouters = new ArrayList<RelationRole>();
+        for(RelationRole r : rels) {
+            if( r.rel.get("type") != null &&
+                r.rel.get("type").equalsIgnoreCase("multipolygon") &&
+                r.role.equalsIgnoreCase("outer")
+              ) {
+                multiouters.add(r);
+                continue;
+            }
+            // Add it back!
+            Relation newRel = new Relation(r.rel);
+            newRel.addMember(new RelationMember(r.role, outer));
+            cmds.add(new ChangeCommand(r.rel, newRel));
+        }
+
+        Relation newRel = null;
+        switch(multiouters.size()) {
+            case 0:
+                return;
+            case 1:
+                // Found only one to be part of a multipolygon relation, so just add it back as well
+                newRel = new Relation(multiouters.get(0).rel);
+                newRel.addMember(new RelationMember(multiouters.get(0).role, outer));
+                cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel));
+                return;
+            default:
+                // Create a new relation with all previous members and (Way)outer as outer.
+                newRel = new Relation();
+                for(RelationRole r : multiouters) {
+                    // Add members
+                    for(RelationMember rm : r.rel.getMembers())
+                        if(!newRel.getMembers().contains(rm)) newRel.addMember(rm);
+                    // Add tags
+                    for (String key : r.rel.keySet()) {
+                        newRel.put(key, r.rel.get(key));
+                    }
+                    // Delete old relation
+                    cmds.add(new DeleteCommand(r.rel));
+                }
+                newRel.addMember(new RelationMember("outer", outer));
+                cmds.add(new AddCommand(newRel));
+        }
+    }
+
+    /**
+     * @param Collection<Way> The List of Ways to remove all tags from
+     */
+    private void stripTags(Collection<Way> ways) {
+        for(Way w: ways) stripTags(w);
+        commitCommands(marktr("Remove tags from inner ways"));
+    }
+
+    /**
+     * @param Way The Way to remove all tags from
+     */
+    private void stripTags(Way x) {
+        if(x.getKeys() == null) return;
+        Way y = new Way(x);
+        for (String key : x.keySet())
+            y.remove(key);
+        cmds.add(new ChangeCommand(x, y));
+    }
+
+    /**
+     * Takes the last cmdsCount actions back and combines them into a single action
+     * (for when the user wants to undo the join action)
+     * @param String The commit message to display
+     */
+    private void makeCommitsOneAction(String message) {
+        UndoRedoHandler ur = Main.main.undoRedo;
+        cmds.clear();
+        int i = Math.max(ur.commands.size() - cmdsCount, 0);
+        for(; i < ur.commands.size(); i++)
+            cmds.add(ur.commands.get(i));
+
+        for(i = 0; i < cmds.size(); i++)
+            ur.undo();
+
+        commitCommands(message == null ? marktr("Join Areas Function") : message);
+        cmdsCount = 0;
+    }
+
+    @Override
+    protected void updateEnabledState() {
+        if (getCurrentDataSet() == null) {
+            setEnabled(false);
+        } else {
+            updateEnabledState(getCurrentDataSet().getSelected());
+        }
+    }
+
+    @Override
+    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
+        setEnabled(selection != null && !selection.isEmpty());
+    }
+}
Index: /trunk/src/org/openstreetmap/josm/actions/JumpToAction.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/JumpToAction.java	(revision 2575)
+++ /trunk/src/org/openstreetmap/josm/actions/JumpToAction.java	(revision 2575)
@@ -0,0 +1,182 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package org.openstreetmap.josm.actions;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.BorderLayout;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.tools.GBC;
+import org.openstreetmap.josm.tools.OsmUrlToBounds;
+import org.openstreetmap.josm.tools.Shortcut;
+
+public class JumpToAction extends JosmAction implements MouseListener {
+    public JumpToAction() {
+        super(tr("Jump To Position"), null, tr("Opens a dialog that allows to jump to a specific location"), Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
+        KeyEvent.VK_G, Shortcut.GROUP_HOTKEY), true);
+    }
+
+    private JTextField url = new JTextField();
+    private JTextField lat = new JTextField();
+    private JTextField lon = new JTextField();
+    private JTextField zm = new JTextField();
+
+    private double zoomFactor = 0;
+    public void showJumpToDialog() {
+        MapView mv = Main.map.mapView;
+        if(mv == null)
+            return;
+        LatLon curPos=mv.getProjection().eastNorth2latlon(mv.getCenter());
+        lat.setText(java.lang.Double.toString(curPos.lat()));
+        lon.setText(java.lang.Double.toString(curPos.lon()));
+
+        double dist = mv.getDist100Pixel();
+        zoomFactor = 1/dist;
+
+        zm.setText(java.lang.Long.toString(Math.round(dist*100)/100));
+        updateUrl(true);
+
+        JPanel panel = new JPanel(new BorderLayout());
+        panel.add(new JLabel("<html>"
+                              + tr("Enter Lat/Lon to jump to position.")
+                              + "<br>"
+                              + tr("You can also paste an URL from www.openstreetmap.org")
+                              + "<br>"
+                              + "</html>"),
+                  BorderLayout.NORTH);
+
+        class osmURLListener implements DocumentListener {
+            public void changedUpdate(DocumentEvent e) { parseURL(); }
+            public void insertUpdate(DocumentEvent e) { parseURL(); }
+            public void removeUpdate(DocumentEvent e) { parseURL(); }
+        }
+
+        class osmLonLatListener implements DocumentListener {
+            public void changedUpdate(DocumentEvent e) { updateUrl(false); }
+            public void insertUpdate(DocumentEvent e) { updateUrl(false); }
+            public void removeUpdate(DocumentEvent e) { updateUrl(false); }
+        }
+
+        osmLonLatListener x=new osmLonLatListener();
+        lat.getDocument().addDocumentListener(x);
+        lon.getDocument().addDocumentListener(x);
+        zm.getDocument().addDocumentListener(x);
+        url.getDocument().addDocumentListener(new osmURLListener());
+
+        JPanel p = new JPanel(new GridBagLayout());
+        panel.add(p, BorderLayout.NORTH);
+
+        p.add(new JLabel(tr("Latitude")), GBC.eol());
+        p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
+
+        p.add(new JLabel(tr("Longitude")), GBC.eol());
+        p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
+
+        p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
+        p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
+
+        p.add(new JLabel(tr("URL")), GBC.eol());
+        p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
+
+        Object[] buttons = { tr("Jump there"), tr("Cancel") };
+        LatLon ll = null;
+        double zoomLvl = 100;
+        while(ll == null) {
+            int option = JOptionPane.showOptionDialog(
+                            Main.parent,
+                            panel,
+                            tr("Jump to Position"),
+                            JOptionPane.OK_CANCEL_OPTION,
+                            JOptionPane.PLAIN_MESSAGE,
+                            null,
+                            buttons,
+                            buttons[0]);
+
+            if (option != JOptionPane.OK_OPTION) return;
+            try {
+                zoomLvl = Double.parseDouble(zm.getText());
+                ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
+            } catch (Exception ex) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude, Longitude or Zoom. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
+            }
+        }
+
+        mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
+    }
+
+    private void parseURL() {
+        if(!url.hasFocus()) return;
+        Bounds b = OsmUrlToBounds.parse(url.getText());
+        if (b != null) {
+            lat.setText(Double.toString((b.getMin().lat() + b.getMax().lat())/2));
+            lon.setText(Double.toString((b.getMin().lon() + b.getMax().lon())/2));
+
+            int zoomLvl = 16;
+            String[] args = url.getText().substring(url.getText().indexOf('?')+1).split("&");
+            for (String arg : args) {
+                int eq = arg.indexOf('=');
+                if (eq == -1 || !arg.substring(0, eq).equalsIgnoreCase("zoom")) continue;
+
+                zoomLvl = Integer.parseInt(arg.substring(eq + 1));
+                break;
+            }
+
+            // 10 000 000 = 10 000 * 1000 = World * (km -> m)
+            zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
+        }
+    }
+
+    private void updateUrl(boolean force) {
+        if(!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
+        try {
+            double dlat = Double.parseDouble(lat.getText());
+            double dlon = Double.parseDouble(lon.getText());
+            double m = Double.parseDouble(zm.getText());
+            // Inverse function to the one above. 18 is the current maximum zoom
+            // available on standard renderers, so choose this is in case m
+            // should be zero
+            int zoomLvl = 18;
+            if(m > 0)
+                zoomLvl = (int)Math.round((-1) * Math.log(m/10000000)/Math.log(2));
+
+            int decimals = (int) Math.pow(10, (zoomLvl / 3));
+            dlat = Math.round(dlat * decimals);
+            dlat /= decimals;
+            dlon = Math.round(dlon * decimals);
+            dlon /= decimals;
+            url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoomLvl);
+        } catch (NumberFormatException x) {}
+    }
+
+    public void actionPerformed(ActionEvent e) {
+        showJumpToDialog();
+    }
+
+    public void mousePressed(MouseEvent e) {}
+
+    public void mouseReleased(MouseEvent e) {}
+
+    public void mouseEntered(MouseEvent e) {}
+
+    public void mouseExited(MouseEvent e) {}
+
+    public void mouseClicked(MouseEvent e) {
+        showJumpToDialog();
+    }
+}
Index: /trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java	(revision 2575)
+++ /trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java	(revision 2575)
@@ -0,0 +1,219 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package org.openstreetmap.josm.actions;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+import static org.openstreetmap.josm.tools.I18n.trn;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.swing.JOptionPane;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.DeleteCommand;
+import org.openstreetmap.josm.command.SequenceCommand;
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.osm.DataSource;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.tools.Shortcut;
+
+public class SimplifyWayAction extends JosmAction {
+    public SimplifyWayAction() {
+        super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."), Shortcut.registerShortcut("tools:simplify", tr("Tool: {0}", tr("Simplify Way")),
+        KeyEvent.VK_Y, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
+    }
+
+    public void actionPerformed(ActionEvent e) {
+        Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelected();
+
+        int ways = 0;
+        LinkedList<Bounds> bounds = new LinkedList<Bounds>();
+        OsmDataLayer dataLayer = Main.map.mapView.getEditLayer();
+        for (DataSource ds : dataLayer.data.dataSources) {
+            if (ds.bounds != null)
+                bounds.add(ds.bounds);
+        }
+        for (OsmPrimitive prim : selection) {
+            if (prim instanceof Way) {
+                if (bounds.size() > 0) {
+                    Way way = (Way) prim;
+                    // We check if each node of each way is at least in one download
+                    // bounding box. Otherwise nodes may get deleted that are necessary by
+                    // unloaded ways (see Ticket #1594)
+                    for (Node node : way.getNodes()) {
+                        boolean isInsideOneBoundingBox = false;
+                        for (Bounds b : bounds) {
+                            if (b.contains(node.getCoor())) {
+                                isInsideOneBoundingBox = true;
+                                break;
+                            }
+                        }
+                        if (!isInsideOneBoundingBox) {
+                            int option = JOptionPane.showConfirmDialog(Main.parent,
+                                    tr("The selected way(s) have nodes outside of the downloaded data region.\n"
+                                            + "This can lead to nodes being deleted accidentally.\n"
+                                            + "Are you really sure to continue?"),
+                                    tr("Please abort if you are not sure"), JOptionPane.YES_NO_CANCEL_OPTION,
+                                    JOptionPane.WARNING_MESSAGE);
+
+                            if (option != JOptionPane.YES_OPTION)
+                                return;
+                            break;
+                        }
+                    }
+                }
+
+                ways++;
+            }
+        }
+
+        if (ways == 0) {
+            JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way to simplify."));
+            return;
+        } else if (ways > 10) {
+            int option = JOptionPane.showConfirmDialog(Main.parent, trn(
+                    "The selection contains {0} way. Are you sure you want to simplify it?",
+                    "The selection contains {0} ways. Are you sure you want to simplify them all?",
+                    ways,ways),
+                    tr("Are you sure?"), JOptionPane.YES_NO_OPTION);
+            if (option != JOptionPane.YES_OPTION)
+                return;
+        }
+
+        for (OsmPrimitive prim : selection) {
+            if (prim instanceof Way) {
+                simplifyWay((Way) prim);
+            }
+        }
+    }
+
+    public void simplifyWay(Way w) {
+        double threshold = Double.parseDouble(Main.pref.get("simplify-way.max-error", "3"));
+
+        Way wnew = new Way(w);
+
+        int toI = wnew.getNodesCount() - 1;
+        List<OsmPrimitive> parents = new ArrayList<OsmPrimitive>();
+        for (int i = wnew.getNodesCount() - 1; i >= 0; i--) {
+            parents.addAll(w.getNode(i).getReferrers());
+            boolean used = false;
+            if (parents.size() == 2) {
+                used = Collections.frequency(w.getNodes(), wnew.getNode(i)) > 1;
+            } else {
+                parents.remove(w);
+                parents.remove(wnew);
+                used = !parents.isEmpty();
+            }
+            if (!used)
+                used = wnew.getNode(i).isTagged();
+
+            if (used) {
+                simplifyWayRange(wnew, i, toI, threshold);
+                toI = i;
+            }
+        }
+        simplifyWayRange(wnew, 0, toI, threshold);
+
+        HashSet<Node> delNodes = new HashSet<Node>();
+        delNodes.addAll(w.getNodes());
+        delNodes.removeAll(wnew.getNodes());
+
+        if (wnew.getNodesCount() != w.getNodesCount()) {
+            Collection<Command> cmds = new LinkedList<Command>();
+            cmds.add(new ChangeCommand(w, wnew));
+            cmds.add(new DeleteCommand(delNodes));
+            Main.main.undoRedo.add(new SequenceCommand(trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds));
+            Main.map.repaint();
+        }
+    }
+
+    public void simplifyWayRange(Way wnew, int from, int to, double thr) {
+        if (to - from >= 2) {
+            ArrayList<Node> ns = new ArrayList<Node>();
+            simplifyWayRange(wnew, from, to, ns, thr);
+            List<Node> nodes = wnew.getNodes();
+            for (int j = to - 1; j > from; j--) {
+                nodes.remove(j);
+            }
+            nodes.addAll(from + 1, ns);
+            wnew.setNodes(nodes);
+        }
+    }
+
+    /*
+     * Takes an interval [from,to] and adds nodes from (from,to) to ns.
+     * (from and to are indices of wnew.nodes.)
+     */
+    public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
+        Node fromN = wnew.getNode(from), toN = wnew.getNode(to);
+
+        int imax = -1;
+        double xtemax = 0;
+        for (int i = from + 1; i < to; i++) {
+            Node n = wnew.getNode(i);
+            double xte = Math.abs(EARTH_RAD
+                    * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
+                            / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
+                            / 180));
+            if (xte > xtemax) {
+                xtemax = xte;
+                imax = i;
+            }
+        }
+
+        if (imax != -1 && xtemax >= thr) {
+            simplifyWayRange(wnew, from, imax, ns, thr);
+            ns.add(wnew.getNode(imax));
+            simplifyWayRange(wnew, imax, to, ns, thr);
+        }
+    }
+
+    public static double EARTH_RAD = 6378137.0;
+
+    /* From Aviaton Formulary v1.3
+     * http://williams.best.vwh.net/avform.htm
+     */
+    public static double dist(double lat1, double lon1, double lat2, double lon2) {
+        return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
+                * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
+    }
+
+    public static double course(double lat1, double lon1, double lat2, double lon2) {
+        return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
+                * Math.cos(lat2) * Math.cos(lon1 - lon2))
+                % (2 * Math.PI);
+    }
+
+    public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
+        double dist_AD = dist(lat1, lon1, lat3, lon3);
+        double crs_AD = course(lat1, lon1, lat3, lon3);
+        double crs_AB = course(lat1, lon1, lat2, lon2);
+        return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB));
+    }
+
+    @Override
+    protected void updateEnabledState() {
+        if (getCurrentDataSet() == null) {
+            setEnabled(false);
+        } else {
+            updateEnabledState(getCurrentDataSet().getSelected());
+        }
+    }
+
+    @Override
+    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
+        setEnabled(selection != null && !selection.isEmpty());
+    }
+}
Index: /trunk/src/org/openstreetmap/josm/gui/MainMenu.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/MainMenu.java	(revision 2574)
+++ /trunk/src/org/openstreetmap/josm/gui/MainMenu.java	(revision 2575)
@@ -38,4 +38,5 @@
 import org.openstreetmap.josm.actions.InfoAction;
 import org.openstreetmap.josm.actions.JoinNodeWayAction;
+import org.openstreetmap.josm.actions.JoinAreasAction;
 import org.openstreetmap.josm.actions.JosmAction;
 import org.openstreetmap.josm.actions.MergeLayerAction;
@@ -56,4 +57,5 @@
 import org.openstreetmap.josm.actions.SelectAllAction;
 import org.openstreetmap.josm.actions.ShowStatusReportAction;
+import org.openstreetmap.josm.actions.SimplifyWayAction;
 import org.openstreetmap.josm.actions.SplitWayAction;
 import org.openstreetmap.josm.actions.ToggleGPXLinesAction;
@@ -140,4 +142,6 @@
     public final JosmAction joinNodeWay = new JoinNodeWayAction();
     public final JosmAction unglueNodes = new UnGlueAction();
+    public final JosmAction simplifyWay = new SimplifyWayAction();
+    public final JosmAction joinAreas = new JoinAreasAction();
     public final InfoAction info = new InfoAction();
     public final HistoryInfoAction historyinfo = new HistoryInfoAction();
@@ -265,4 +269,5 @@
         toolsMenu.addSeparator();
         add(toolsMenu, reverseWay);
+        add(toolsMenu, simplifyWay);
         toolsMenu.addSeparator();
         add(toolsMenu, alignInCircle);
@@ -278,4 +283,5 @@
         add(toolsMenu, joinNodeWay);
         add(toolsMenu, unglueNodes);
+        add(toolsMenu, joinAreas);
         toolsMenu.addSeparator();
         add(toolsMenu, info);
Index: /trunk/src/org/openstreetmap/josm/gui/MapStatus.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/MapStatus.java	(revision 2574)
+++ /trunk/src/org/openstreetmap/josm/gui/MapStatus.java	(revision 2575)
@@ -40,4 +40,5 @@
 
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.JumpToAction;
 import org.openstreetmap.josm.data.coor.CoordinateFormat;
 import org.openstreetmap.josm.data.coor.LatLon;
@@ -501,4 +502,8 @@
         this.mv = mapFrame.mapView;
 
+        JumpToAction JumpToAct = new JumpToAction();
+        lonText.addMouseListener(JumpToAct);
+        latText.addMouseListener(JumpToAct);
+
         // Listen for mouse movements and set the position text field
         mv.addMouseMotionListener(new MouseMotionListener(){
Index: /trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java	(revision 2574)
+++ /trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java	(revision 2575)
@@ -46,5 +46,5 @@
                 "lang-ru", "ewmsplugin", "ywms", "tways-0.2", "geotagged", "landsat",
                 "namefinder", "waypoints", "slippy_map_chooser", "tcx-support", "usertools",
-                "AgPifoJ"};
+                "AgPifoJ", "utilsplugin"};
     public static String [] unmaintained = new String[] {"gpsbabelgui", "Intersect_way"};
 
