Index: trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java	(revision 5641)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java	(revision 5642)
@@ -66,4 +66,5 @@
 import org.openstreetmap.josm.tools.GBC;
 import org.openstreetmap.josm.tools.Shortcut;
+import org.openstreetmap.josm.tools.WindowGeometry;
 
 /**
@@ -76,10 +77,5 @@
     private String changedKey;
 
-    private String lastAddKey = null;
-    private String lastAddValue = null;
-
-    public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
-    public static final int MAX_LRU_TAGS_NUMBER = 9;
-
+    
     private String objKey;
 
@@ -90,12 +86,19 @@
     };
 
-    
-    // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html) 
-    private final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(MAX_LRU_TAGS_NUMBER+1, 1.1f, true) {
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
-            return size() > MAX_LRU_TAGS_NUMBER;
-        }
-    };
+    Collection<OsmPrimitive> sel;
+        
+            private String lastAddKey = null;
+        private String lastAddValue = null;
+
+        public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
+        public static final int MAX_LRU_TAGS_NUMBER = 9;
+
+        // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html) 
+        private final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(MAX_LRU_TAGS_NUMBER+1, 1.1f, true) {
+            @Override
+            protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
+                return size() > MAX_LRU_TAGS_NUMBER;
+            }
+        };
 
     TagEditHelper(DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
@@ -110,5 +113,4 @@
     public void addProperty() {
         changedKey = null;
-        Collection<OsmPrimitive> sel;
         if (Main.map.mapMode instanceof DrawAction) {
             sel = ((DrawAction) Main.map.mapMode).getInProgressSelection();
@@ -120,39 +122,58 @@
         if (sel.isEmpty()) return;
 
-        final AddTagsPanel p = new AddTagsPanel(sel);
-
-        final JOptionPane optionPane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
-            @Override public void selectInitialValue() {
-                // save unix system selection (middle mouse paste)
-                Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
-                if(sysSel != null) {
-                    Transferable old = sysSel.getContents(null);
-                    p.keys.requestFocusInWindow();
-                    p.keys.getEditor().selectAll();
-                    sysSel.setContents(old, null);
-                } else {
-                    p.keys.requestFocusInWindow();
-                    p.keys.getEditor().selectAll();
-                }
-            }
-        };
-        final JDialog dialog = optionPane.createDialog(Main.parent, tr("Add value?"));
-        dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
-        // We specify what to do if panel wants to close the entire dialog
-        p.setCloseActionListener(new ActionListener() {
-            public void actionPerformed(ActionEvent e) {
-                dialog.setVisible(false);
-                optionPane.setValue(JOptionPane.OK_OPTION);
-            }
-        });
-        dialog.setVisible(true);
-        
-        p.destroyActions();
-        
-        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(optionPane.getValue()))
-            return;
-        p.performTagAdding();
+        final AddTagsDialog addDialog = new AddTagsDialog();        
+        
+        addDialog.showDialog();
+        
+        addDialog.destroyActions();
+        if (addDialog.getValue() == 1)           
+            addDialog.performTagAdding();
+        else 
+            addDialog.undoAllTagsAdding();
     }
     
+    /**
+    * Edit the value in the properties table row
+    * @param row The row of the table from which the value is edited.
+    */
+    public void editProperty(final int row) {
+        changedKey = null;
+        Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
+        if (sel.isEmpty()) return;
+
+        String key = propertyData.getValueAt(row, 0).toString();
+        objKey=key;
+        
+        final EditTagDialog editDialog = new EditTagDialog(key, row, 
+                (Map<String, Integer>) propertyData.getValueAt(row, 1));
+        editDialog.showDialog();
+        if (editDialog.getValue() !=1 ) return;
+        editDialog.performTagEdit();
+    }
+    /**
+     * If during last editProperty call user changed the key name, this key will be returned
+     * Elsewhere, returns null.
+     */
+    public String getChangedKey() {
+        return changedKey;
+    }
+    
+    public void resetChangedKey() {
+        changedKey = null;
+    }
+
+    /**
+     * For a given key k, return a list of keys which are used as keys for
+     * auto-completing values to increase the search space.
+     * @param key the key k
+     * @return a list of keys
+     */
+    private static List<String> getAutocompletionKeys(String key) {
+        if ("name".equals(key) || "addr:street".equals(key))
+            return Arrays.asList("addr:street", "name");
+        else
+            return Arrays.asList(key);
+    }
+
     /**
      * Create a focus handling adapter and apply in to the editor component of value
@@ -185,129 +206,14 @@
     }
     
-    /**
-    * Edit the value in the properties table row
-    * @param row The row of the table from which the value is edited.
-    */
-    public void editProperty(final int row) {
-        changedKey = null;
-        Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
-        if (sel.isEmpty()) return;
-
-        String key = propertyData.getValueAt(row, 0).toString();
-        objKey=key;
-        
-        @SuppressWarnings("unchecked")
-        final EditTagsPanel p = new EditTagsPanel(key, row, sel, 
-                (Map<String, Integer>) propertyData.getValueAt(row, 1));
-
-        final JOptionPane optionPane = new JOptionPane(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
-            @Override public void selectInitialValue() {
-                // save unix system selection (middle mouse paste)
-                Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
-                if(sysSel != null) {
-                    Transferable old = sysSel.getContents(null);
-                    p.values.requestFocusInWindow();
-                    p.values.getEditor().selectAll();
-                    sysSel.setContents(old, null);
-                } else {
-                    p.values.requestFocusInWindow();
-                    p.values.getEditor().selectAll();
-                }
-            }
-        };
-        
-        final JDialog dialog = optionPane.createDialog(Main.parent, trn("Change value?", "Change values?", p.getValueCount()));
-        dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
-        Dimension dlgSize = dialog.getSize();
-        if(dlgSize.width > Main.parent.getSize().width) {
-            dlgSize.width = Math.max(250, Main.parent.getSize().width);
-            dialog.setSize(dlgSize);
-        }
-        dialog.setLocationRelativeTo(Main.parent);
-        
-        // Auto-close dialog when user selects a value from list
-        p.values.getEditor().addActionListener(new ActionListener() {
-            public void actionPerformed(ActionEvent e) {
-                dialog.setVisible(false);
-                optionPane.setValue(JOptionPane.OK_OPTION);
-            }
-        });
-
-        dialog.setVisible(true);
-        
-         if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(optionPane.getValue())) {
-            return;
-         }
-
-        p.performTagEdit();
-
-    }
-    
-    /**
-     * If during last editProperty call user changed the key name, this key will be returned
-     * Elsewhere, returns null.
-     */
-    public String getChangedKey() {
-        return changedKey;
-    }
-    
-    public void resetChangedKey() {
-        changedKey = null;
-    }
-
-    /**
-     * For a given key k, return a list of keys which are used as keys for
-     * auto-completing values to increase the search space.
-     * @param key the key k
-     * @return a list of keys
-     */
-    private static List<String> getAutocompletionKeys(String key) {
-        if ("name".equals(key) || "addr:street".equals(key))
-            return Arrays.asList("addr:street", "name");
-        else
-            return Arrays.asList(key);
-    }
-
-    
-    class EditTagsPanel extends JPanel {
-        final AutoCompletingComboBox keys;
-        final AutoCompletingComboBox values;
+        
+    public class EditTagDialog extends ExtendedDialog {
+        AutoCompletingComboBox keys;
+        AutoCompletingComboBox values;
         String oldValue;
-        final String key;
-        final Collection<OsmPrimitive> sel;
-        final Map<String, Integer> m;
-            
-        public EditTagsPanel(final String key, final int row, Collection<OsmPrimitive> sel, Map<String, Integer> countMap) {
-            super(new BorderLayout());
-            this.key = key;
-            this.sel = sel;
-            m = countMap;
-    
- 
-            String msg = "<html>"+trn("This will change {0} object.",
-                    "This will change up to {0} objects.", sel.size(), sel.size())
-                    +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
-
-            add(new JLabel(msg), BorderLayout.NORTH);
-
-            JPanel p = new JPanel(new GridBagLayout());
-            add(p, BorderLayout.CENTER);
-
-            AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
-            List<AutoCompletionListItem> keyList = autocomplete.getKeys();
-            Collections.sort(keyList, defaultACItemComparator);
-
-            keys = new AutoCompletingComboBox(key);
-            keys.setPossibleACItems(keyList);
-            keys.setEditable(true);
-            keys.setSelectedItem(key);
-
-            p.add(new JLabel(tr("Key")), GBC.std());
-            p.add(Box.createHorizontalStrut(10), GBC.std());
-            p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
-
-            
-            Comparator<AutoCompletionListItem> usedValuesAwareComparator = new Comparator<AutoCompletionListItem>() {
-
+        String key;
+        Map<String, Integer> m;
+        int row;
+
+        Comparator<AutoCompletionListItem> usedValuesAwareComparator = new Comparator<AutoCompletionListItem>() {
                 @Override
                 public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) {
@@ -322,12 +228,6 @@
                 }
             };
-
-            List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
-            Collections.sort(valueList, usedValuesAwareComparator);
-
-            final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
-
-            values = new AutoCompletingComboBox(selection);
-            values.setRenderer(new DefaultListCellRenderer() {
+        
+        DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                 @Override public Component getListCellRendererComponent(JList list,
                         Object value, int index, boolean isSelected,  boolean cellHasFocus){
@@ -347,5 +247,52 @@
                     return c;
                 }
-            });
+            };
+        
+        private EditTagDialog(String key, int row, Map<String, Integer> map) {
+            super(Main.parent, trn("Change value?", "Change values?", map.size()), new String[] {tr("OK"),tr("Cancel")});
+            setButtonIcons(new String[] {"ok","cancel"});
+            setCancelButton(2);
+            setIcon(JOptionPane.QUESTION_MESSAGE);
+            this.key = key;
+            this.row = row;
+            this.m = map;
+            
+            // TODO : How to remember position, allowing autosizing?
+            // setRememberWindowGeometry(getClass().getName() + ".geometry",
+            //    WindowGeometry.centerInWindow(Main.parent, new Dimension(270, 180)));
+            //setRememberWindowGeometry(getClass().getName() + ".geometry",
+            //        WindowGeometry.centerInWindow(Main.parent, new Dimension(270, 180)));
+            
+            JPanel mainPanel = new JPanel(new BorderLayout());
+                    
+            String msg = "<html>"+trn("This will change {0} object.",
+                    "This will change up to {0} objects.", sel.size(), sel.size())
+                    +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
+
+            mainPanel.add(new JLabel(msg), BorderLayout.NORTH);
+
+            JPanel p = new JPanel(new GridBagLayout());
+            mainPanel.add(p, BorderLayout.CENTER);
+
+            AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
+            List<AutoCompletionListItem> keyList = autocomplete.getKeys();
+            Collections.sort(keyList, defaultACItemComparator);
+
+            keys = new AutoCompletingComboBox(key);
+            keys.setPossibleACItems(keyList);
+            keys.setEditable(true);
+            keys.setSelectedItem(key);
+
+            p.add(new JLabel(tr("Key")), GBC.std());
+            p.add(Box.createHorizontalStrut(10), GBC.std());
+            p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
+
+            List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
+            Collections.sort(valueList, usedValuesAwareComparator);
+
+            final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
+            
+            values = new AutoCompletingComboBox(selection);
+            values.setRenderer(cellRenderer);
 
             values.setEditable(true);
@@ -356,13 +303,29 @@
             p.add(Box.createHorizontalStrut(10), GBC.std());
             p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
+            values.getEditor().addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    buttonAction(0, null); // emulate OK button click
+                }
+            });
             addFocusAdapter(keys, values, autocomplete, usedValuesAwareComparator);
             
-        }
-        
-        private int getValueCount() {
-            return m.size();
-        }
-
-        /**
+            setContent(mainPanel, false);
+            
+            // TODO: Is it correct place for thois code - was in 
+            //  new JOptionPane(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
+            //    @Override public void selectInitialValue() {
+            Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
+                if(sysSel != null) {
+                    Transferable old = sysSel.getContents(null);
+                    values.requestFocusInWindow();
+                    values.getEditor().selectAll();
+                    sysSel.setContents(old, null);
+                } else {
+                    values.requestFocusInWindow();
+                    values.getEditor().selectAll();
+                }
+        }
+        
+           /**
          * Edit tags of multiple selected objects according to selected ComboBox values
          * If value == "", tag will be deleted
@@ -436,19 +399,28 @@
             changedKey = newkey;
         }
-        
-        
     }
     
-    class AddTagsPanel extends JPanel {
-        final AutoCompletingComboBox keys = new AutoCompletingComboBox();
-        final AutoCompletingComboBox values = new AutoCompletingComboBox();
+    class AddTagsDialog extends ExtendedDialog {
+        AutoCompletingComboBox keys;
+        AutoCompletingComboBox values;
         List<JosmAction> recentTagsActions = new ArrayList<JosmAction>();
-        Collection<OsmPrimitive> sel;
-        private ActionListener autoCloseActionListener;
-        
-        public AddTagsPanel(Collection<OsmPrimitive> sel) {
-            super(new GridBagLayout());
-            this.sel = sel;
-            add(new JLabel("<html>"+trn("This will change up to {0} object.",
+        
+        // Counter of added commands for possible undo
+        private int commandCount;
+
+        public AddTagsDialog() {
+            super(Main.parent, tr("Add value?"), new String[] {tr("OK"),tr("Cancel")});
+            setButtonIcons(new String[] {"ok","cancel"});
+            setCancelButton(2);
+            
+            // TODO : How to remember position, allowing autosizing?
+            // setRememberWindowGeometry(getClass().getName() + ".geometry",
+            //    WindowGeometry.centerInWindow(Main.parent, new Dimension(270, 180)));
+            
+            JPanel mainPanel = new JPanel(new GridBagLayout());
+            keys = new AutoCompletingComboBox();
+            values = new AutoCompletingComboBox();
+            
+            mainPanel.add(new JLabel("<html>"+trn("This will change up to {0} object.",
                 "This will change up to {0} objects.", sel.size(),sel.size())
                 +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
@@ -480,9 +452,9 @@
             keys.setEditable(true);
 
-            add(keys, GBC.eop().fill());
-
-            add(new JLabel(tr("Please select a value")), GBC.eol());
+            mainPanel.add(keys, GBC.eop().fill());
+
+            mainPanel.add(new JLabel(tr("Please select a value")), GBC.eol());
             values.setEditable(true);
-            add(values, GBC.eop().fill());
+            mainPanel.add(values, GBC.eop().fill());
             if (itemToSelect != null) {
                 keys.setSelectedItem(itemToSelect);
@@ -500,12 +472,30 @@
                 recentTagsToShow = MAX_LRU_TAGS_NUMBER;
             }
-            suggestRecentlyAddedTags(recentTagsToShow, focus);
-        }
-
-        private void suggestRecentlyAddedTags(int tagsToShow, final FocusAdapter focus) {
+            suggestRecentlyAddedTags(mainPanel, recentTagsToShow, focus);
+            
+            setContent(mainPanel, false);
+            
+            // TODO: Is it correct place for thois code - was in 
+            //  new JOptionPane(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
+            //    @Override public void selectInitialValue() {
+            Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
+                if(sysSel != null) {
+                    Transferable old = sysSel.getContents(null);
+                    values.requestFocusInWindow();
+                    values.getEditor().selectAll();
+                    sysSel.setContents(old, null);
+                } else {
+                    values.requestFocusInWindow();
+                    values.getEditor().selectAll();
+                }
+                
+            
+        }
+
+        private void suggestRecentlyAddedTags(JPanel mainPanel, int tagsToShow, final FocusAdapter focus) {
             if (!(tagsToShow > 0 && !recentTags.isEmpty())) 
                 return;
 
-            add(new JLabel(tr("Recently added tags")), GBC.eol());
+            mainPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
             
             int count = 1;
@@ -538,5 +528,5 @@
                 GridBagConstraints gbc = new GridBagConstraints();
                 gbc.ipadx = 5;
-                add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
+                mainPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
                 // Create tag label
                 final String color = action.isEnabled() ? "" : "; color:gray";
@@ -546,6 +536,6 @@
                 if (action.isEnabled()) {
                     // Register action
-                    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);
-                    getActionMap().put(actionShortcutKey, action);
+                    mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);
+                    mainPanel.getActionMap().put(actionShortcutKey, action);
                     // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
                     tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
@@ -558,6 +548,5 @@
                             if (e.getClickCount()>1) {
                                 performTagAdding();
-                                if (autoCloseActionListener!=null)
-                                    autoCloseActionListener.actionPerformed(null);
+                                buttonAction(0, null); // emulate OK click and close the dialog
                             }
                             // add tags on Shift-Click
@@ -576,5 +565,5 @@
                 JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
                 tagPanel.add(tagLabel);
-                add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
+                mainPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
             }
         }
@@ -597,7 +586,14 @@
             lastAddValue = value;
             recentTags.put(new Tag(key, value), null);
+            commandCount++;
             Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
             changedKey = key;
         }
+        
+        
+        public void undoAllTagsAdding() {
+            Main.main.undoRedo.undo(commandCount);
+        }
+
 
         private void disableTagIfNeeded(final Tag t, final JosmAction action) {
@@ -612,12 +608,4 @@
         }
 
-        /**
-         * @param autoCloseActionListener will be called when we need to close the entire dialog
-         */
-        public void setCloseActionListener(ActionListener autoCloseActionListener) {
-            this.autoCloseActionListener = autoCloseActionListener;
-        }
-    }
-}
-    
-   
+    }
+ }
