Index: /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/Address.java
===================================================================
--- /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/Address.java	(revision 22185)
+++ /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/Address.java	(revision 22186)
@@ -16,4 +16,6 @@
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -30,4 +32,5 @@
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
@@ -36,4 +39,6 @@
 import javax.swing.JRadioButton;
 import javax.swing.JTextField;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 
 import org.openstreetmap.josm.Main;
@@ -61,16 +66,20 @@
     private static final long serialVersionUID = 1L;
     
-    static final String tagHighway = "highway";
-    static final String tagHighwayName = "name";
-    static final String tagHouseNumber = "addr:housenumber";
-    static final String tagHouseStreet = "addr:street";
-    static final String relationAddrType = "associatedStreet";
-    static final String relationStreetNameAttr = "name";
-    static final String relationAddrStreetRole = "street";
+    // perhaps make all these tags configurable in the future
+    private String tagHighway = "highway";
+    private String tagHighwayName = "name";
+    private String tagHouseNumber = "addr:housenumber";
+    private String tagHouseStreet = "addr:street";
+    private String tagBuilding = "building";
+    private String relationAddrType = "associatedStreet";
+    private String relationAddrName = "name";
+    private String relationAddrStreetRole = "street";
+    private String relationMemberHouse = "house";
     
     private JRadioButton plus_one = new JRadioButton("+1", false);
-    private JRadioButton plus_two = new JRadioButton("+2", true);
+    private JRadioButton plus_two = new JRadioButton("+2", true); // enable this by default
     private JRadioButton minus_one = new JRadioButton("-1", false);
     private JRadioButton minus_two = new JRadioButton("-2", false);
+    final JCheckBox tagPolygon = new JCheckBox(tr("on polygon"));
 
     JDialog dialog = null;
@@ -80,4 +89,5 @@
     JLabel link = new JLabel();
     private Way selectedWay;
+    private Relation selectedRelation;
     
     MapFrame mapFrame;
@@ -85,5 +95,5 @@
     public Address(MapFrame mapFrame) {
         super(tr("Add address"), "buildings", 
-                tr("Create house number and street name relation"),
+                tr("Helping tool for tag address"),
                 Shortcut.registerShortcut("mapmode:buildings", tr("Mode: {0}", tr("Buildings")), KeyEvent.VK_E, Shortcut.GROUP_EDIT),
                 mapFrame, getCursor());
@@ -114,10 +124,14 @@
         Point mousePos = e.getPoint();
         List<Way> mouseOnExistingWays = new ArrayList<Way>();
+        List<Way> mouseOnExistingBuildingWays = new ArrayList<Way>();
         mouseOnExistingWays = new ArrayList<Way>();
         Node currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
         if (currentMouseNode != null) {
+            // click on existing node
+            setNewSelection(currentMouseNode);
             String num = currentMouseNode.get(tagHouseNumber);
             if (num != null) {
                 try {
+                    // add new address
                     Integer.parseInt(num); 
                     inputNumber.setText(num);
@@ -129,26 +143,28 @@
             if (currentMouseNode.get(tagHouseStreet) != null) {
                 inputStreet.setText(currentMouseNode.get(tagHouseNumber));
-                setSelectedWay(null);
+                setSelectedWay((Way)null);
             } else {
                 // check if the node belongs to an associatedStreet relation
                 List<OsmPrimitive> l = currentMouseNode.getReferrers();
+                boolean nodeBelongsToRelation = false;
                 for (OsmPrimitive osm : l) {
                     if (osm instanceof Relation && osm.hasKey("type") && osm.get("type").equals(relationAddrType)) {
-                        if (osm.hasKey(relationStreetNameAttr)) {
-                            inputStreet.setText(osm.get(relationStreetNameAttr));
-                            setSelectedWay(null);
-                            break;
-                        } else {
-                            for (RelationMember rm : ((Relation)osm).getMembers())
-                                if (rm.getRole().equals(relationAddrStreetRole)) {
-                                    OsmPrimitive osp = rm.getMember();
-                                    if (osp instanceof Way && osp.hasKey(tagHighwayName)) {
-                                        inputStreet.setText(osp.get(tagHighwayName));
-                                        setSelectedWay((Way)osp);
-                                        break;
-                                    }
+                        for (RelationMember rm : ((Relation)osm).getMembers()) {
+                            if (rm.getRole().equals(relationAddrStreetRole)) {
+                                OsmPrimitive osp = rm.getMember();
+                                if (osp instanceof Way && osp.hasKey(tagHighwayName)) {
+                                    inputStreet.setText(osp.get(tagHighwayName));
+                                    setSelectedWay((Way)osp, (Relation)osm);
+                                    nodeBelongsToRelation = true;
+                                    break;
                                 }
+                            }
                         }
                     }
+                }
+                if (!nodeBelongsToRelation) {
+                    // node exists but doesn't carry address information : add tags like a new node
+                    Collection<Command> cmds = new LinkedList<Command>();
+                    addAddrToPrimitive(currentMouseNode, cmds);
                 }
             }
@@ -158,4 +174,6 @@
                 if (ws.way.get(tagHighway) != null && ws.way.get(tagHighwayName) != null)
                     mouseOnExistingWays.add(ws.way);
+                else if (ws.way.get(tagBuilding) != null && ws.way.get(tagHouseNumber) == null)
+                    mouseOnExistingBuildingWays.add(ws.way);
             }
             if (mouseOnExistingWays.size() == 1) {
@@ -163,4 +181,6 @@
                 inputStreet.setText(mouseOnExistingWays.get(0).get(tagHighwayName));
                 setSelectedWay(mouseOnExistingWays.get(0));
+                inputNumber.setText("");
+                setNewSelection(mouseOnExistingWays.get(0));
             } else if (mouseOnExistingWays.size() == 0) {
                 // clicked a non highway and not a node => add the new address 
@@ -168,12 +188,10 @@
                     Toolkit.getDefaultToolkit().beep();
                 } else {
-                    Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
                     Collection<Command> cmds = new LinkedList<Command>();
-                    if (n == null)
-                        n = createNewNode(e, cmds);
-                    addAddrToNode(n, cmds);
-                    if (cmds.size() > 0) {
-                        Command c = new SequenceCommand("Add node address", cmds);
-                        Main.main.undoRedo.add(c);
+                    if (tagPolygon.isSelected()) {
+                        addAddrToPolygon(mouseOnExistingBuildingWays, cmds);
+                    } else {
+                        Node n = createNewNode(e, cmds);
+                        addAddrToPrimitive(n, cmds);
                     }
                 }
@@ -183,10 +201,38 @@
     }
     
-    private void addAddrToNode(Node n, Collection<Command> cmds) {
+    private void addAddrToPolygon(List<Way> mouseOnExistingBuildingWays, Collection<Command> cmds) {
+        for (Way w:mouseOnExistingBuildingWays) {
+            cmds.add(new ChangePropertyCommand(w, tagHouseNumber, inputNumber.getText()));
+            addAddrToPrimitive(w, cmds);
+        }
+    }
+    
+    private void addAddrToPrimitive(OsmPrimitive osm, Collection<Command> cmds) {
+        // add the current tag addr:housenumber in node and member in relation
+        cmds.add(new ChangePropertyCommand(osm, tagHouseNumber, inputNumber.getText()));            
+        if (Main.pref.getBoolean("cadastrewms.addr.dontUseRelation", false)) {
+            cmds.add(new ChangePropertyCommand(osm, tagHouseStreet, inputStreet.getText()));
+        } else if (selectedWay != null) {
+           // add the node to its relation
+           if (selectedRelation != null) {
+               RelationMember rm = new RelationMember(relationMemberHouse, osm);
+               Relation newRel = new Relation(selectedRelation);
+               newRel.addMember(rm);
+               cmds.add(new ChangeCommand(selectedRelation, newRel));
+           } else {
+               // create new relation
+               Relation newRel = new Relation();
+               newRel.put("type", relationAddrType);
+               newRel.put(relationAddrName, selectedWay.get(tagHighwayName));
+               newRel.addMember(new RelationMember(relationAddrStreetRole, selectedWay));
+               newRel.addMember(new RelationMember(relationMemberHouse, osm));
+               cmds.add(new AddCommand(newRel));
+           }
+        }
         try {
-            // add the tag addr:housenumber in node and member in relation
-            cmds.add(new ChangePropertyCommand(n, tagHouseNumber, inputNumber.getText()));            
-            cmds.add(new ChangePropertyCommand(n, tagHouseStreet, inputStreet.getText()));            
             applyInputNumberChange();
+            Command c = new SequenceCommand("Add node address", cmds);
+            Main.main.undoRedo.add(c);
+            setNewSelection(osm);
         } catch (NumberFormatException en) {
             System.out.println("Unable to parse house number \"" + inputNumber.getText() + "\"");
@@ -335,5 +381,5 @@
         link.setEnabled(false);
         JPanel p = new JPanel(new GridBagLayout());
-        JLabel number = new JLabel(tr("Number"));
+        JLabel number = new JLabel(tr("Next no"));
         JLabel street = new JLabel(tr("Street"));
         p.add(number, GBC.std().insets(0, 0, 0, 0));
@@ -350,5 +396,5 @@
                 inputNumber.setText("");
                 inputStreet.setText("");
-                setSelectedWay(null);
+                setSelectedWay((Way)null);
             }
         });
@@ -359,8 +405,17 @@
         bgIncremental.add(minus_two);
         p.add(minus_one, GBC.std().insets(10, 0, 10, 0));
-        p.add(plus_one, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
+//        p.add(plus_one, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
+        p.add(plus_one, GBC.std().insets(0, 0, 10, 0));
+        tagPolygon.setSelected(Main.pref.getBoolean("cadastrewms.addr.onBuilding", false));
+        tagPolygon.addChangeListener(new ChangeListener() {
+            @Override
+            public void stateChanged(ChangeEvent arg0) {
+                Main.pref.put("cadastrewms.addr.onBuilding", tagPolygon.isSelected());
+            }
+        });
+        p.add(tagPolygon, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 0));
         p.add(minus_two, GBC.std().insets(10, 0, 10, 0));
-        p.add(plus_two, GBC.std().insets(10, 0, 10, 0));
-        p.add(clearButton, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
+        p.add(plus_two, GBC.std().insets(0, 0, 10, 0));
+        p.add(clearButton, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 0));
     
         final Object[] options = {};
@@ -373,5 +428,5 @@
         dialog.addComponentListener(new ComponentAdapter() {
             protected void rememberGeometry() {
-                Main.pref.put("cadastrewms.addr_bounds", dialog.getX()+","+dialog.getY()+","+dialog.getWidth()+","+dialog.getHeight());
+                Main.pref.put("cadastrewms.addr.bounds", dialog.getX()+","+dialog.getY()+","+dialog.getWidth()+","+dialog.getHeight());
             }
             @Override public void componentMoved(ComponentEvent e) {
@@ -382,5 +437,18 @@
             }
         });
-        String bounds = Main.pref.get("cadastrewms.addr_bounds",null);
+        dialog.addWindowListener(new WindowListener() {
+            @Override
+            public void windowClosing(WindowEvent arg0) {
+                exitMode();
+                Main.map.selectMapMode((MapMode)Main.map.getDefaultButtonAction());
+            }
+            public void windowClosed(WindowEvent e) {}
+            public void windowActivated(WindowEvent arg0) {}
+            public void windowDeactivated(WindowEvent arg0) {}
+            public void windowDeiconified(WindowEvent arg0) {}
+            public void windowIconified(WindowEvent arg0) {}
+            public void windowOpened(WindowEvent arg0) {}
+        });
+        String bounds = Main.pref.get("cadastrewms.addr.bounds",null);
         if (bounds != null) {
             String[] b = bounds.split(",");
@@ -388,19 +456,26 @@
                     Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3])));
         }
-
-
-    
-    
-//        exitMode();
-//        Main.map.selectMapMode((MapMode)Main.map.getDefaultButtonAction());
 }
     
     private void setSelectedWay(Way w) {
         this.selectedWay = w;
-        if (w == null)
+        this.selectedRelation = null;
+        if (w == null) {
             link.setEnabled(false);
-        else
+        } else
             link.setEnabled(true);
     }
+    
+    private void setSelectedWay(Way w, Relation r) {
+        setSelectedWay(w);
+        this.selectedRelation = r;
+    }
+    
+    private void setNewSelection(OsmPrimitive osm) {
+        Collection<OsmPrimitive> newSelection = new LinkedList<OsmPrimitive>(Main.main.getCurrentDataSet().getSelected());
+        newSelection.clear();
+        newSelection.add(osm);
+        getCurrentDataSet().setSelected(osm);
+    }
 
 }
Index: /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePreferenceSetting.java
===================================================================
--- /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePreferenceSetting.java	(revision 22185)
+++ /applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePreferenceSetting.java	(revision 22186)
@@ -41,4 +41,8 @@
     private JCheckBox disableImageCropping = new JCheckBox(tr("Disable image cropping during georeferencing."));
     
+    private JCheckBox autoFirstLayer = new JCheckBox(tr("Select first WMS layer in list."));
+    
+    private JCheckBox dontUseRelation = new JCheckBox(tr("Don't use relation for addresses (but \"addr:street\" on nodes)."));
+    
     private JRadioButton grabMultiplier1 = new JRadioButton("", true);
 
@@ -57,6 +61,4 @@
     private JRadioButton crosspiece4 = new JRadioButton("100m");
 
-    private JCheckBox autoFirstLayer = new JCheckBox(tr("Select first WMS layer in list."));
-    
     private JRadioButton grabRes1 = new JRadioButton("high");
 
@@ -324,16 +326,26 @@
         cadastrewms.add(jLabelCacheSize, GBC.std().insets(20, 0, 0, 0));
         cadastrewms.add(cacheSize, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 200, 5));
+
         // separator
         cadastrewms.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
+        
+        // option to select the first WMS layer
         autoFirstLayer.setSelected(Main.pref.getBoolean("cadastrewms.autoFirstLayer", false));
         autoFirstLayer.setToolTipText(tr("Automatically selects the first WMS layer if multiple layers exist when grabbing."));
         cadastrewms.add(autoFirstLayer, GBC.eop().insets(0, 0, 0, 0));
+
+        // separator
+        cadastrewms.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
+
+        // option to use or not relations in addresses
+        dontUseRelation.setSelected(Main.pref.getBoolean("cadastrewms.addr.dontUseRelation", false));
+        dontUseRelation.setToolTipText(tr("Enable this to use the tag \"add:street\" on nodes."));
+        cadastrewms.add(dontUseRelation, GBC.eop().insets(0, 0, 0, 0));
+        
+        // end of dialog, scroll bar
         cadastrewms.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
-//        JTabbedPane cadastrecontent = new JTabbedPane();
-//        cadastrecontent.add(cadastrewms);
         JScrollPane scrollpane = new JScrollPane(cadastrewms);
         scrollpane.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
         cadastrewmsMast.add(scrollpane, GBC.eol().fill(GBC.BOTH));
-
     }
 
@@ -403,4 +415,5 @@
         Main.pref.put("cadastrewms.autoFirstLayer", autoFirstLayer.isSelected());
         CacheControl.cacheEnabled = enableCache.isSelected();
+        Main.pref.put("cadastrewms.addr.dontUseRelation", dontUseRelation.isSelected());
         CadastrePlugin.refreshConfiguration();
         CadastrePlugin.refreshMenu();
