Index: applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java
===================================================================
--- applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java	(revision 28589)
+++ applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java	(revision 28624)
@@ -32,5 +32,5 @@
  * @author ramack
  */
-public class MeasurementDialog extends ToggleDialog {
+public class MeasurementDialog extends ToggleDialog implements SelectionChangedListener {
     private static final long serialVersionUID = 4708541586297950021L;
 
@@ -59,5 +59,5 @@
      */
     protected JLabel segAngleLabel;
-
+    
     /**
      * Constructor
@@ -112,50 +112,6 @@
             resetButton
         }));
-
-        final MeasurementDialog dlg = this;
-
-        DataSet.addSelectionListener(new SelectionChangedListener() {
-            @Override
-            public void selectionChanged(Collection<? extends OsmPrimitive> arg0) {
-                double length = 0.0;
-                double segAngle = 0.0;
-                double area = 0.0;
-                Node lastNode = null;
-                for(OsmPrimitive p:arg0) {
-                    // ignore incomplete nodes
-                    if(p instanceof Node && !((Node)p).isIncomplete()) {
-                        Node n =(Node)p;
-                        if(lastNode == null) {
-                            lastNode = n;
-                        } else {
-                            length += lastNode.getCoor().greatCircleDistance(n.getCoor());
-                            segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor());
-                            lastNode = n;
-                        }
-                    } else if (p instanceof Way) {
-                        Way w = (Way)p;
-                        Node lastN = null;
-                        for (Node n: w.getNodes()) {
-                            if (lastN != null && lastN.getCoor() != null && n.getCoor() != null) {
-                                length += lastN.getCoor().greatCircleDistance(n.getCoor());
-                                //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
-                                area += (MeasurementLayer.calcX(n.getCoor()) * MeasurementLayer.calcY(lastN.getCoor()))
-                                - (MeasurementLayer.calcY(n.getCoor()) * MeasurementLayer.calcX(lastN.getCoor()));
-                                segAngle = MeasurementLayer.angleBetween(lastN.getCoor(), n.getCoor());
-                            }
-                            lastN = n;
-                        }
-                        if (lastN != null && lastN == w.getNodes().iterator().next())
-                            area = Math.abs(area / 2);
-                        else
-                            area = 0;
-                    }
-                }
-                dlg.selectLengthLabel.setText(new DecimalFormat("#0.00").format(length) + " m");
-
-                dlg.segAngleLabel.setText(new DecimalFormat("#0.0").format(segAngle) + " \u00b0");
-                dlg.selectAreaLabel.setText(new DecimalFormat("#0.00").format(area) + " m\u00b2");
-            }
-        });
+        
+        DataSet.addSelectionListener(this);
     }
 
@@ -166,3 +122,53 @@
         MeasurementPlugin.getCurrentLayer().reset();
     }
+
+	@Override
+	public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
+        double length = 0.0;
+        double segAngle = 0.0;
+        double area = 0.0;
+        Node lastNode = null;
+        for (OsmPrimitive p : newSelection) {
+            // ignore incomplete nodes
+            if (p instanceof Node && !((Node)p).isIncomplete()) {
+                Node n =(Node)p;
+                if (lastNode == null) {
+                    lastNode = n;
+                } else {
+                    length += lastNode.getCoor().greatCircleDistance(n.getCoor());
+                    segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor());
+                    lastNode = n;
+                }
+            } else if (p instanceof Way) {
+                Way w = (Way)p;
+                Node lastN = null;
+                for (Node n: w.getNodes()) {
+                    if (lastN != null && lastN.getCoor() != null && n.getCoor() != null) {
+                        length += lastN.getCoor().greatCircleDistance(n.getCoor());
+                        //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
+                        area += (MeasurementLayer.calcX(n.getCoor()) * MeasurementLayer.calcY(lastN.getCoor()))
+                        - (MeasurementLayer.calcY(n.getCoor()) * MeasurementLayer.calcX(lastN.getCoor()));
+                        segAngle = MeasurementLayer.angleBetween(lastN.getCoor(), n.getCoor());
+                    }
+                    lastN = n;
+                }
+                if (lastN != null && lastN == w.getNodes().iterator().next())
+                    area = Math.abs(area / 2);
+                else
+                    area = 0;
+            }
+        }
+        selectLengthLabel.setText(new DecimalFormat("#0.00").format(length) + " m");
+        segAngleLabel.setText(new DecimalFormat("#0.0").format(segAngle) + " \u00b0");
+        selectAreaLabel.setText(new DecimalFormat("#0.00").format(area) + " m\u00b2");
+	}
+
+	/* (non-Javadoc)
+	 * @see org.openstreetmap.josm.gui.dialogs.ToggleDialog#destroy()
+	 */
+	@Override
+	public void destroy() {
+		super.destroy();
+		DataSet.removeSelectionListener(this);
+	}
 }
Index: applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementLayer.java
===================================================================
--- applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementLayer.java	(revision 28589)
+++ applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementLayer.java	(revision 28624)
@@ -141,5 +141,7 @@
         DecimalFormat nf = new DecimalFormat("#0.00");
         DecimalFormat nf2 = new DecimalFormat("#0.0");
-        MeasurementPlugin.measurementDialog.pathLengthLabel.setText(pathLength < 800?nf2.format(pathLength) + " m":nf.format(pathLength/1000) + " km");
+        if (MeasurementPlugin.measurementDialog != null) { 
+            MeasurementPlugin.measurementDialog.pathLengthLabel.setText(pathLength < 800?nf2.format(pathLength) + " m":nf.format(pathLength/1000) + " km");
+        }
     }
 
Index: applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementPlugin.java
===================================================================
--- applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementPlugin.java	(revision 28589)
+++ applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementPlugin.java	(revision 28624)
@@ -21,20 +21,23 @@
     public MeasurementPlugin(PluginInformation info) {
         super(info);
-        mode = new MeasurementMode(Main.map, "measurement", tr("measurement mode"));
-        btn = new IconToggleButton(mode);
-        btn.setVisible(true);
-        measurementDialog = new MeasurementDialog();
     }
 
     @Override
     public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
-        if(newFrame != null)
-            newFrame.addToggleDialog(measurementDialog);
-        if(Main.map != null)
-            Main.map.addMapMode(btn);
+        if (newFrame != null) {
+            newFrame.addToggleDialog(measurementDialog = new MeasurementDialog());
+            mode = new MeasurementMode(newFrame, "measurement", tr("measurement mode"));
+            btn = new IconToggleButton(mode);
+            btn.setVisible(true);
+            newFrame.addMapMode(btn);
+        } else {
+        	btn = null;
+        	mode = null;
+        	measurementDialog = null;
+        }
     }
 
-    public static MeasurementLayer getCurrentLayer(){
-        if(currentLayer == null){
+    public static MeasurementLayer getCurrentLayer() {
+        if (currentLayer == null) {
             currentLayer = new MeasurementLayer(tr("Measurements"));
             Main.main.addLayer(currentLayer);
