Index: /applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyAction.java
===================================================================
--- /applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyAction.java	(revision 24178)
+++ /applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyAction.java	(revision 24179)
@@ -6,6 +6,9 @@
 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.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import javax.swing.JOptionPane;
@@ -16,4 +19,5 @@
 import org.openstreetmap.josm.actions.JosmAction;
 import org.openstreetmap.josm.command.AddCommand;
+import org.openstreetmap.josm.command.ChangePropertyCommand;
 import org.openstreetmap.josm.command.Command;
 import org.openstreetmap.josm.command.SequenceCommand;
@@ -23,5 +27,4 @@
 import org.openstreetmap.josm.data.osm.Way;
 import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
-import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.tools.Shortcut;
 
@@ -43,102 +46,173 @@
 public class MultipolyAction extends JosmAction {
 
-  public MultipolyAction() {
-    super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon."),
-      Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")),
-      KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
-  }
-  /**
-   * The action button has been clicked
-   *
-   * @param e Action Event
-   */
-  public void actionPerformed(ActionEvent e) {
-    if (Main.main.getEditLayer() == null) {
-      JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
-      return;
-    }
-
-    Collection < Way > selectedWays = Main.main.getCurrentDataSet().getSelectedWays();
-
-    if (selectedWays.size() < 1) {
-      // Sometimes it make sense creating multipoly of only one way (so it will form outer way)
-      // and then splitting the way later (so there are multiple ways forming outer way)
-      JOptionPane.showMessageDialog(Main.parent, tr("You must select at least one way."));
-      return;
-    }
-
-    Multipolygon polygon = this.analyzeWays(selectedWays);
-
-    if (polygon == null) {
-      return;                   //could not make multipolygon.
-    }
-
-    Relation relation = this.createRelation(polygon);
-
-    if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) {
-      //Open relation edit window, if set up in preferences
-      RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null);
-      editor.setVisible(true);
-    } else {
-      //Just add the relation
-      Main.main.undoRedo.add(new AddCommand(relation));
-      Main.map.repaint();
-    }
-  }
-
-  /** Enable this action only if something is selected */
-  @Override protected void updateEnabledState() {
-    if (getCurrentDataSet() == null) {
-      setEnabled(false);
-    } else {
-      updateEnabledState(getCurrentDataSet().getSelected());
-    }
-  }
-
-  /** Enable this action only if something is selected */
-  @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
-    setEnabled(selection != null && !selection.isEmpty());
-  }
-
-  /**
-   * This method analyzes ways and creates multipolygon.
-   * @param selectedWays
-   * @return null, if there was a problem with the ways.
-   */
-  private Multipolygon analyzeWays(Collection < Way > selectedWays) {
-
-    Multipolygon pol = new Multipolygon();
-    String error = pol.makeFromWays(selectedWays);
-
-    if (error != null) {
-      JOptionPane.showMessageDialog(Main.parent, error);
-      return null;
-    } else {
-      return pol;
-    }
-  }
-
-  /**
-   * Builds a relation from polygon ways.
-   * @param pol 
-   * @return
-   */
-  private Relation createRelation(Multipolygon pol) {
-    // Create new relation
-    Relation rel = new Relation();
-    rel.put("type", "multipolygon");
-    // Add ways to it
-    for (JoinedPolygon jway:pol.outerWays) {
-      for (Way way:jway.ways) {
-          rel.addMember(new RelationMember("outer", way));
-      }
-    }
-
-    for (JoinedPolygon jway:pol.innerWays) {
-      for (Way way:jway.ways) {
-          rel.addMember(new RelationMember("inner", way));
-      }
-    }
-    return rel;
-  }
+	public MultipolyAction() {
+		super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon."),
+				Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")),
+						KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
+	}
+	/**
+	 * The action button has been clicked
+	 *
+	 * @param e Action Event
+	 */
+	public void actionPerformed(ActionEvent e) {
+		if (Main.main.getEditLayer() == null) {
+			JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
+			return;
+		}
+
+		Collection < Way > selectedWays = Main.main.getCurrentDataSet().getSelectedWays();
+
+		if (selectedWays.size() < 1) {
+			// Sometimes it make sense creating multipoly of only one way (so it will form outer way)
+			// and then splitting the way later (so there are multiple ways forming outer way)
+			JOptionPane.showMessageDialog(Main.parent, tr("You must select at least one way."));
+			return;
+		}
+
+		Multipolygon polygon = this.analyzeWays(selectedWays);
+
+		if (polygon == null) {
+			return;                   //could not make multipolygon.
+		}
+
+		Relation relation = this.createRelation(polygon);
+
+		if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) {
+			//Open relation edit window, if set up in preferences
+			RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null);
+
+			editor.setModal(true);
+			editor.setVisible(true);
+
+			//TODO: cannot get the resulting relation from RelationEditor :(.
+			/*
+			if (relationCountBefore < relationCountAfter) {
+				//relation saved, clean up the tags
+				List<Command> list = this.removeTagsFromInnerWays(relation);
+				if (list.size() > 0)
+				{
+					Main.main.undoRedo.add(new SequenceCommand(tr("Remove tags from multipolygon inner ways"), list));
+				}
+			}
+			 */
+
+		} else {
+			//Just add the relation
+			List<Command> list = this.removeTagsFromInnerWays(relation);
+			list.add(new AddCommand(relation));
+			Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), list));
+			Main.map.repaint();
+		}
+
+
+	}
+
+	/** Enable this action only if something is selected */
+	@Override protected void updateEnabledState() {
+		if (getCurrentDataSet() == null) {
+			setEnabled(false);
+		} else {
+			updateEnabledState(getCurrentDataSet().getSelected());
+		}
+	}
+
+	/** Enable this action only if something is selected */
+	@Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
+		setEnabled(selection != null && !selection.isEmpty());
+	}
+
+	/**
+	 * This method analyzes ways and creates multipolygon.
+	 * @param selectedWays
+	 * @return null, if there was a problem with the ways.
+	 */
+	private Multipolygon analyzeWays(Collection < Way > selectedWays) {
+
+		Multipolygon pol = new Multipolygon();
+		String error = pol.makeFromWays(selectedWays);
+
+		if (error != null) {
+			JOptionPane.showMessageDialog(Main.parent, error);
+			return null;
+		} else {
+			return pol;
+		}
+	}
+
+	/**
+	 * Builds a relation from polygon ways.
+	 * @param pol
+	 * @return
+	 */
+	private Relation createRelation(Multipolygon pol) {
+		// Create new relation
+		Relation rel = new Relation();
+		rel.put("type", "multipolygon");
+		// Add ways to it
+		for (JoinedPolygon jway:pol.outerWays) {
+			for (Way way:jway.ways) {
+				rel.addMember(new RelationMember("outer", way));
+			}
+		}
+
+		for (JoinedPolygon jway:pol.innerWays) {
+			for (Way way:jway.ways) {
+				rel.addMember(new RelationMember("inner", way));
+			}
+		}
+		return rel;
+	}
+
+
+	/**
+	 * This method removes tags/value pairs from inner ways that are present in relation or outer ways.
+	 * @param relation
+	 */
+	private List<Command> removeTagsFromInnerWays(Relation relation) {
+		Map<String, String> values = new HashMap<String, String>();
+
+		if (relation.hasKeys()){
+			for(String key: relation.keySet()) {
+				values.put(key, relation.get(key));
+			}
+		}
+
+		List<Way> innerWays = new ArrayList<Way>();
+
+		for(RelationMember m: relation.getMembers()) {
+
+			if (m.hasRole() && m.getRole() == "inner" && m.isWay() && m.getWay().hasKeys()) {
+				innerWays.add(m.getWay());
+			}
+
+			if (m.hasRole() && m.getRole() == "outer" && m.isWay() && m.getWay().hasKeys()) {
+				Way way = m.getWay();
+				for (String key: way.keySet()) {
+					if (!values.containsKey(key)) { //relation values take precedence
+						values.put(key, way.get(key));
+					}
+				}
+			}
+		}
+
+		List<Command> commands = new ArrayList<Command>();
+
+		for(String key: values.keySet()) {
+			List<OsmPrimitive> affectedWays = new ArrayList<OsmPrimitive>();
+			String value = values.get(key);
+
+			for(Way way: innerWays){
+				if (value.equals(way.get(key))) {
+					affectedWays.add(way);
+				}
+			}
+
+			if (affectedWays.size() > 0) {
+				commands.add(new ChangePropertyCommand(affectedWays, key, null));
+			}
+		}
+
+		return commands;
+	}
 }
