001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.properties;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trn;
006
007import java.awt.BorderLayout;
008import java.awt.Component;
009import java.awt.Cursor;
010import java.awt.Dimension;
011import java.awt.FlowLayout;
012import java.awt.Font;
013import java.awt.GridBagConstraints;
014import java.awt.GridBagLayout;
015import java.awt.Toolkit;
016import java.awt.datatransfer.Clipboard;
017import java.awt.datatransfer.Transferable;
018import java.awt.event.ActionEvent;
019import java.awt.event.ActionListener;
020import java.awt.event.FocusAdapter;
021import java.awt.event.FocusEvent;
022import java.awt.event.InputEvent;
023import java.awt.event.KeyEvent;
024import java.awt.event.MouseAdapter;
025import java.awt.event.MouseEvent;
026import java.awt.event.WindowAdapter;
027import java.awt.event.WindowEvent;
028import java.awt.image.BufferedImage;
029import java.text.Normalizer;
030import java.util.ArrayList;
031import java.util.Arrays;
032import java.util.Collection;
033import java.util.Collections;
034import java.util.Comparator;
035import java.util.HashMap;
036import java.util.Iterator;
037import java.util.LinkedHashMap;
038import java.util.LinkedList;
039import java.util.List;
040import java.util.Map;
041
042import javax.swing.AbstractAction;
043import javax.swing.Action;
044import javax.swing.Box;
045import javax.swing.DefaultListCellRenderer;
046import javax.swing.ImageIcon;
047import javax.swing.JCheckBoxMenuItem;
048import javax.swing.JComponent;
049import javax.swing.JLabel;
050import javax.swing.JList;
051import javax.swing.JOptionPane;
052import javax.swing.JPanel;
053import javax.swing.JPopupMenu;
054import javax.swing.KeyStroke;
055import javax.swing.ListCellRenderer;
056import javax.swing.table.DefaultTableModel;
057import javax.swing.text.JTextComponent;
058
059import org.openstreetmap.josm.Main;
060import org.openstreetmap.josm.actions.JosmAction;
061import org.openstreetmap.josm.command.ChangePropertyCommand;
062import org.openstreetmap.josm.command.Command;
063import org.openstreetmap.josm.command.SequenceCommand;
064import org.openstreetmap.josm.data.osm.OsmPrimitive;
065import org.openstreetmap.josm.data.osm.Tag;
066import org.openstreetmap.josm.data.preferences.BooleanProperty;
067import org.openstreetmap.josm.data.preferences.IntegerProperty;
068import org.openstreetmap.josm.gui.ExtendedDialog;
069import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
070import org.openstreetmap.josm.gui.tagging.TaggingPreset;
071import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
072import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
073import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
074import org.openstreetmap.josm.gui.util.GuiHelper;
075import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
076import org.openstreetmap.josm.io.XmlWriter;
077import org.openstreetmap.josm.tools.GBC;
078import org.openstreetmap.josm.tools.Shortcut;
079import org.openstreetmap.josm.tools.WindowGeometry;
080
081/**
082 * Class that helps PropertiesDialog add and edit tag values.
083 * @since 5633
084 */
085class TagEditHelper {
086    private final DefaultTableModel tagData;
087    private final Map<String, Map<String, Integer>> valueCount;
088
089    // Selection that we are editing by using both dialogs
090    Collection<OsmPrimitive> sel;
091
092    private String changedKey;
093    private String objKey;
094
095    Comparator<AutoCompletionListItem> defaultACItemComparator = new Comparator<AutoCompletionListItem>() {
096        @Override
097        public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) {
098            return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
099        }
100    };
101
102    private String lastAddKey = null;
103    private String lastAddValue = null;
104
105    public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
106    public static final int MAX_LRU_TAGS_NUMBER = 30;
107
108    // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
109    private final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(MAX_LRU_TAGS_NUMBER+1, 1.1f, true) {
110        @Override
111        protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
112            return size() > MAX_LRU_TAGS_NUMBER;
113        }
114    };
115
116    TagEditHelper(DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
117        this.tagData = propertyData;
118        this.valueCount = valueCount;
119    }
120
121    /**
122     * Open the add selection dialog and add a new key/value to the table (and
123     * to the dataset, of course).
124     */
125    public void addTag() {
126        changedKey = null;
127        sel = Main.main.getInProgressSelection();
128        if (sel == null || sel.isEmpty()) return;
129
130        final AddTagsDialog addDialog = new AddTagsDialog();
131
132        addDialog.showDialog();
133
134        addDialog.destroyActions();
135        if (addDialog.getValue() == 1)
136            addDialog.performTagAdding();
137        else
138            addDialog.undoAllTagsAdding();
139    }
140
141    /**
142    * Edit the value in the tags table row.
143    * @param row The row of the table from which the value is edited.
144    * @param focusOnKey Determines if the initial focus should be set on key instead of value
145    * @since 5653
146    */
147    public void editTag(final int row, boolean focusOnKey) {
148        changedKey = null;
149        sel = Main.main.getInProgressSelection();
150        if (sel == null || sel.isEmpty()) return;
151
152        String key = tagData.getValueAt(row, 0).toString();
153        objKey=key;
154
155        @SuppressWarnings("unchecked")
156        final EditTagDialog editDialog = new EditTagDialog(key, row,
157                (Map<String, Integer>) tagData.getValueAt(row, 1), focusOnKey);
158        editDialog.showDialog();
159        if (editDialog.getValue() !=1 ) return;
160        editDialog.performTagEdit();
161    }
162
163    /**
164     * If during last editProperty call user changed the key name, this key will be returned
165     * Elsewhere, returns null.
166     * @return The modified key, or {@code null}
167     */
168    public String getChangedKey() {
169        return changedKey;
170    }
171
172    public void resetChangedKey() {
173        changedKey = null;
174    }
175
176    /**
177     * For a given key k, return a list of keys which are used as keys for
178     * auto-completing values to increase the search space.
179     * @param key the key k
180     * @return a list of keys
181     */
182    private static List<String> getAutocompletionKeys(String key) {
183        if ("name".equals(key) || "addr:street".equals(key))
184            return Arrays.asList("addr:street", "name");
185        else
186            return Arrays.asList(key);
187    }
188
189    /**
190     * Load recently used tags from preferences if needed.
191     */
192    public void loadTagsIfNeeded() {
193        if (PROPERTY_REMEMBER_TAGS.get() && recentTags.isEmpty()) {
194            recentTags.clear();
195            Collection<String> c = Main.pref.getCollection("properties.recent-tags");
196            Iterator<String> it = c.iterator();
197            String key, value;
198            while (it.hasNext()) {
199                key = it.next();
200                value = it.next();
201                recentTags.put(new Tag(key, value), null);
202            }
203        }
204    }
205
206    /**
207     * Store recently used tags in preferences if needed.
208     */
209    public void saveTagsIfNeeded() {
210        if (PROPERTY_REMEMBER_TAGS.get() && !recentTags.isEmpty()) {
211            List<String> c = new ArrayList<>( recentTags.size()*2 );
212            for (Tag t: recentTags.keySet()) {
213                c.add(t.getKey());
214                c.add(t.getValue());
215            }
216            Main.pref.putCollection("properties.recent-tags", c);
217        }
218    }
219
220    /**
221     * Warns user about a key being overwritten.
222     * @param action The action done by the user. Must state what key is changed
223     * @param togglePref  The preference to save the checkbox state to
224     * @return {@code true} if the user accepts to overwrite key, {@code false} otherwise
225     */
226    private boolean warnOverwriteKey(String action, String togglePref) {
227        ExtendedDialog ed = new ExtendedDialog(
228                Main.parent,
229                tr("Overwrite key"),
230                new String[]{tr("Replace"), tr("Cancel")});
231        ed.setButtonIcons(new String[]{"purge", "cancel"});
232        ed.setContent(action+"\n"+ tr("The new key is already used, overwrite values?"));
233        ed.setCancelButton(2);
234        ed.toggleEnable(togglePref);
235        ed.showDialog();
236
237        return ed.getValue() == 1;
238    }
239
240    public final class EditTagDialog extends AbstractTagsDialog {
241        final String key;
242        final Map<String, Integer> m;
243        final int row;
244
245        Comparator<AutoCompletionListItem> usedValuesAwareComparator = new Comparator<AutoCompletionListItem>() {
246                @Override
247                public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) {
248                    boolean c1 = m.containsKey(o1.getValue());
249                    boolean c2 = m.containsKey(o2.getValue());
250                    if (c1 == c2)
251                        return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
252                    else if (c1)
253                        return -1;
254                    else
255                        return +1;
256                }
257            };
258
259        ListCellRenderer<AutoCompletionListItem> cellRenderer = new ListCellRenderer<AutoCompletionListItem>() {
260            final DefaultListCellRenderer def = new DefaultListCellRenderer();
261            @Override
262            public Component getListCellRendererComponent(JList<? extends AutoCompletionListItem> list,
263                    AutoCompletionListItem value, int index, boolean isSelected,  boolean cellHasFocus){
264                Component c = def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
265                if (c instanceof JLabel) {
266                    String str = value.getValue();
267                    if (valueCount.containsKey(objKey)) {
268                        Map<String, Integer> m = valueCount.get(objKey);
269                        if (m.containsKey(str)) {
270                            str = tr("{0} ({1})", str, m.get(str));
271                            c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD));
272                        }
273                    }
274                    ((JLabel) c).setText(str);
275                }
276                return c;
277            }
278        };
279
280        private EditTagDialog(String key, int row, Map<String, Integer> map, final boolean initialFocusOnKey) {
281            super(Main.parent, trn("Change value?", "Change values?", map.size()), new String[] {tr("OK"),tr("Cancel")});
282            setButtonIcons(new String[] {"ok","cancel"});
283            setCancelButton(2);
284            configureContextsensitiveHelp("/Dialog/EditValue", true /* show help button */);
285            this.key = key;
286            this.row = row;
287            this.m = map;
288
289            JPanel mainPanel = new JPanel(new BorderLayout());
290
291            String msg = "<html>"+trn("This will change {0} object.",
292                    "This will change up to {0} objects.", sel.size(), sel.size())
293                    +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
294
295            mainPanel.add(new JLabel(msg), BorderLayout.NORTH);
296
297            JPanel p = new JPanel(new GridBagLayout());
298            mainPanel.add(p, BorderLayout.CENTER);
299
300            AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
301            List<AutoCompletionListItem> keyList = autocomplete.getKeys();
302            Collections.sort(keyList, defaultACItemComparator);
303
304            keys = new AutoCompletingComboBox(key);
305            keys.setPossibleACItems(keyList);
306            keys.setEditable(true);
307            keys.setSelectedItem(key);
308
309            p.add(Box.createVerticalStrut(5),GBC.eol());
310            p.add(new JLabel(tr("Key")), GBC.std());
311            p.add(Box.createHorizontalStrut(10), GBC.std());
312            p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
313
314            List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
315            Collections.sort(valueList, usedValuesAwareComparator);
316
317            final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
318
319            values = new AutoCompletingComboBox(selection);
320            values.setRenderer(cellRenderer);
321
322            values.setEditable(true);
323            values.setPossibleACItems(valueList);
324            values.setSelectedItem(selection);
325            values.getEditor().setItem(selection);
326            p.add(Box.createVerticalStrut(5),GBC.eol());
327            p.add(new JLabel(tr("Value")), GBC.std());
328            p.add(Box.createHorizontalStrut(10), GBC.std());
329            p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
330            values.getEditor().addActionListener(new ActionListener() {
331                @Override
332                public void actionPerformed(ActionEvent e) {
333                    buttonAction(0, null); // emulate OK button click
334                }
335            });
336            addFocusAdapter(autocomplete, usedValuesAwareComparator);
337
338            setContent(mainPanel, false);
339
340            addWindowListener(new WindowAdapter() {
341                @Override
342                public void windowOpened(WindowEvent e) {
343                    if (initialFocusOnKey) {
344                        selectKeysComboBox();
345                    } else {
346                        selectValuesCombobox();
347                    }
348                }
349            });
350        }
351
352        /**
353         * Edit tags of multiple selected objects according to selected ComboBox values
354         * If value == "", tag will be deleted
355         * Confirmations may be needed.
356         */
357        private void performTagEdit() {
358            String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
359            value = Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
360            if (value.isEmpty()) {
361                value = null; // delete the key
362            }
363            String newkey = Tag.removeWhiteSpaces(keys.getEditor().getItem().toString());
364            newkey = Normalizer.normalize(newkey, java.text.Normalizer.Form.NFC);
365            if (newkey.isEmpty()) {
366                newkey = key;
367                value = null; // delete the key instead
368            }
369            if (key.equals(newkey) && tr("<different>").equals(value))
370                return;
371            if (key.equals(newkey) || value == null) {
372                Main.main.undoRedo.add(new ChangePropertyCommand(sel, newkey, value));
373                AutoCompletionManager.rememberUserInput(newkey, value, true);
374            } else {
375                for (OsmPrimitive osm: sel) {
376                    if (osm.get(newkey) != null) {
377                        if (!warnOverwriteKey(tr("You changed the key from ''{0}'' to ''{1}''.", key, newkey),
378                                "overwriteEditKey"))
379                            return;
380                        break;
381                    }
382                }
383                Collection<Command> commands = new ArrayList<>();
384                commands.add(new ChangePropertyCommand(sel, key, null));
385                if (value.equals(tr("<different>"))) {
386                    Map<String, List<OsmPrimitive>> map = new HashMap<>();
387                    for (OsmPrimitive osm: sel) {
388                        String val = osm.get(key);
389                        if (val != null) {
390                            if (map.containsKey(val)) {
391                                map.get(val).add(osm);
392                            } else {
393                                List<OsmPrimitive> v = new ArrayList<>();
394                                v.add(osm);
395                                map.put(val, v);
396                            }
397                        }
398                    }
399                    for (Map.Entry<String, List<OsmPrimitive>> e: map.entrySet()) {
400                        commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
401                    }
402                } else {
403                    commands.add(new ChangePropertyCommand(sel, newkey, value));
404                    AutoCompletionManager.rememberUserInput(newkey, value, false);
405                }
406                Main.main.undoRedo.add(new SequenceCommand(
407                        trn("Change properties of up to {0} object",
408                                "Change properties of up to {0} objects", sel.size(), sel.size()),
409                                commands));
410            }
411
412            changedKey = newkey;
413        }
414    }
415
416    public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false);
417    public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true);
418    public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags", DEFAULT_LRU_TAGS_NUMBER);
419
420    abstract class AbstractTagsDialog extends ExtendedDialog {
421        AutoCompletingComboBox keys;
422        AutoCompletingComboBox values;
423        Component componentUnderMouse;
424
425        public AbstractTagsDialog(Component parent, String title, String[] buttonTexts) {
426            super(parent, title, buttonTexts);
427            addMouseListener(new PopupMenuLauncher(popupMenu));
428        }
429
430        @Override
431        public void setupDialog() {
432            super.setupDialog();
433            final Dimension size = getSize();
434            // Set resizable only in width
435            setMinimumSize(size);
436            setPreferredSize(size);
437            // setMaximumSize does not work, and never worked, but still it seems not to bother Oracle to fix this 10-year-old bug
438            // https://bugs.openjdk.java.net/browse/JDK-6200438
439            // https://bugs.openjdk.java.net/browse/JDK-6464548
440
441            setRememberWindowGeometry(getClass().getName() + ".geometry",
442                WindowGeometry.centerInWindow(Main.parent, size));
443        }
444
445        @Override
446        public void setVisible(boolean visible) {
447            // Do not want dialog to be resizable in height, as its size may increase each time because of the recently added tags
448            // So need to modify the stored geometry (size part only) in order to use the automatic positioning mechanism
449            if (visible) {
450                WindowGeometry geometry = initWindowGeometry();
451                Dimension storedSize = geometry.getSize();
452                Dimension size = getSize();
453                if (!storedSize.equals(size)) {
454                    if (storedSize.width < size.width) {
455                        storedSize.width = size.width;
456                    }
457                    if (storedSize.height != size.height) {
458                        storedSize.height = size.height;
459                    }
460                    rememberWindowGeometry(geometry);
461                }
462                keys.setFixedLocale(PROPERTY_FIX_TAG_LOCALE.get());
463            }
464            super.setVisible(visible);
465        }
466
467        private void selectACComboBoxSavingUnixBuffer(AutoCompletingComboBox cb) {
468            // select combobox with saving unix system selection (middle mouse paste)
469            Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
470            if(sysSel != null) {
471                Transferable old = sysSel.getContents(null);
472                cb.requestFocusInWindow();
473                cb.getEditor().selectAll();
474                sysSel.setContents(old, null);
475            } else {
476                cb.requestFocusInWindow();
477                cb.getEditor().selectAll();
478            }
479        }
480
481        public void selectKeysComboBox() {
482            selectACComboBoxSavingUnixBuffer(keys);
483        }
484
485        public void selectValuesCombobox()   {
486            selectACComboBoxSavingUnixBuffer(values);
487        }
488
489        /**
490        * Create a focus handling adapter and apply in to the editor component of value
491        * autocompletion box.
492        * @param autocomplete Manager handling the autocompletion
493        * @param comparator Class to decide what values are offered on autocompletion
494        * @return The created adapter
495        */
496        protected FocusAdapter addFocusAdapter(final AutoCompletionManager autocomplete, final Comparator<AutoCompletionListItem> comparator) {
497           // get the combo box' editor component
498           JTextComponent editor = (JTextComponent)values.getEditor().getEditorComponent();
499           // Refresh the values model when focus is gained
500           FocusAdapter focus = new FocusAdapter() {
501               @Override
502               public void focusGained(FocusEvent e) {
503                   String key = keys.getEditor().getItem().toString();
504
505                   List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
506                   Collections.sort(valueList, comparator);
507
508                   values.setPossibleACItems(valueList);
509                   values.getEditor().selectAll();
510                   objKey=key;
511               }
512           };
513           editor.addFocusListener(focus);
514           return focus;
515        }
516
517        protected JPopupMenu popupMenu = new JPopupMenu() {
518            JCheckBoxMenuItem fixTagLanguageCb = new JCheckBoxMenuItem(
519                new AbstractAction(tr("Use English language for tag by default")){
520                @Override
521                public void actionPerformed(ActionEvent e) {
522                    boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
523                    PROPERTY_FIX_TAG_LOCALE.put(sel);
524                }
525            });
526            {
527                add(fixTagLanguageCb);
528                fixTagLanguageCb.setState(PROPERTY_FIX_TAG_LOCALE.get());
529            }
530        };
531    }
532
533    class AddTagsDialog extends AbstractTagsDialog {
534        List<JosmAction> recentTagsActions = new ArrayList<>();
535
536        // Counter of added commands for possible undo
537        private int commandCount;
538
539        public AddTagsDialog() {
540            super(Main.parent, tr("Add value?"), new String[] {tr("OK"),tr("Cancel")});
541            setButtonIcons(new String[] {"ok","cancel"});
542            setCancelButton(2);
543            configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */);
544
545            JPanel mainPanel = new JPanel(new GridBagLayout());
546            keys = new AutoCompletingComboBox();
547            values = new AutoCompletingComboBox();
548
549            mainPanel.add(new JLabel("<html>"+trn("This will change up to {0} object.",
550                "This will change up to {0} objects.", sel.size(),sel.size())
551                +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
552
553            AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
554            List<AutoCompletionListItem> keyList = autocomplete.getKeys();
555
556            AutoCompletionListItem itemToSelect = null;
557            // remove the object's tag keys from the list
558            Iterator<AutoCompletionListItem> iter = keyList.iterator();
559            while (iter.hasNext()) {
560                AutoCompletionListItem item = iter.next();
561                if (item.getValue().equals(lastAddKey)) {
562                    itemToSelect = item;
563                }
564                for (int i = 0; i < tagData.getRowCount(); ++i) {
565                    if (item.getValue().equals(tagData.getValueAt(i, 0))) {
566                        if (itemToSelect == item) {
567                            itemToSelect = null;
568                        }
569                        iter.remove();
570                        break;
571                    }
572                }
573            }
574
575            Collections.sort(keyList, defaultACItemComparator);
576            keys.setPossibleACItems(keyList);
577            keys.setEditable(true);
578
579            mainPanel.add(keys, GBC.eop().fill());
580
581            mainPanel.add(new JLabel(tr("Please select a value")), GBC.eol());
582            values.setEditable(true);
583            mainPanel.add(values, GBC.eop().fill());
584            if (itemToSelect != null) {
585                keys.setSelectedItem(itemToSelect);
586                if (lastAddValue != null) {
587                    values.setSelectedItem(lastAddValue);
588                }
589            }
590
591            FocusAdapter focus = addFocusAdapter(autocomplete, defaultACItemComparator);
592            // fire focus event in advance or otherwise the popup list will be too small at first
593            focus.focusGained(null);
594
595            int recentTagsToShow = PROPERTY_RECENT_TAGS_NUMBER.get();
596            if (recentTagsToShow > MAX_LRU_TAGS_NUMBER) {
597                recentTagsToShow = MAX_LRU_TAGS_NUMBER;
598            }
599
600            // Add tag on Shift-Enter
601            mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
602                        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK), "addAndContinue");
603                mainPanel.getActionMap().put("addAndContinue", new AbstractAction() {
604                    @Override
605                    public void actionPerformed(ActionEvent e) {
606                        performTagAdding();
607                        selectKeysComboBox();
608                    }
609                });
610
611            suggestRecentlyAddedTags(mainPanel, recentTagsToShow, focus);
612
613            setContent(mainPanel, false);
614
615            selectKeysComboBox();
616
617            popupMenu.add(new AbstractAction(tr("Set number of recently added tags")) {
618                @Override
619                public void actionPerformed(ActionEvent e) {
620                    selectNumberOfTags();
621                }
622            });
623            JCheckBoxMenuItem rememberLastTags = new JCheckBoxMenuItem(
624                new AbstractAction(tr("Remember last used tags after a restart")){
625                @Override
626                public void actionPerformed(ActionEvent e) {
627                    boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
628                    PROPERTY_REMEMBER_TAGS.put(sel);
629                    if (sel) saveTagsIfNeeded();
630                }
631            });
632            rememberLastTags.setState(PROPERTY_REMEMBER_TAGS.get());
633            popupMenu.add(rememberLastTags);
634        }
635
636        private void selectNumberOfTags() {
637            String s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"));
638            if (s!=null) try {
639                int v = Integer.parseInt(s);
640                if (v>=0 && v<=MAX_LRU_TAGS_NUMBER) {
641                    PROPERTY_RECENT_TAGS_NUMBER.put(v);
642                    return;
643                }
644            } catch (NumberFormatException ex) {
645                Main.warn(ex);
646            }
647            JOptionPane.showMessageDialog(this, tr("Please enter integer number between 0 and {0}", MAX_LRU_TAGS_NUMBER));
648        }
649
650        private void suggestRecentlyAddedTags(JPanel mainPanel, int tagsToShow, final FocusAdapter focus) {
651            if (!(tagsToShow > 0 && !recentTags.isEmpty()))
652                return;
653
654            mainPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
655
656            int count = 1;
657            // We store the maximum number (9) of recent tags to allow dynamic change of number of tags shown in the preferences.
658            // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum numbern and not the number of tags to show.
659            // However, as Set does not allow to iterate in descending order, we need to copy its elements into a List we can access in reverse order.
660            List<Tag> tags = new LinkedList<>(recentTags.keySet());
661            for (int i = tags.size()-1; i >= 0 && count <= tagsToShow; i--, count++) {
662                final Tag t = tags.get(i);
663                // Create action for reusing the tag, with keyboard shortcut Ctrl+(1-5)
664                String actionShortcutKey = "properties:recent:"+count;
665                String actionShortcutShiftKey = "properties:recent:shift:"+count;
666                Shortcut sc = Shortcut.registerShortcut(actionShortcutKey, tr("Choose recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL);
667                final JosmAction action = new JosmAction(actionShortcutKey, null, tr("Use this tag again"), sc, false) {
668                    @Override
669                    public void actionPerformed(ActionEvent e) {
670                        keys.setSelectedItem(t.getKey());
671                        // fix #7951, #8298 - update list of values before setting value (?)
672                        focus.focusGained(null);
673                        values.setSelectedItem(t.getValue());
674                        selectValuesCombobox();
675                    }
676                };
677                Shortcut scShift = Shortcut.registerShortcut(actionShortcutShiftKey, tr("Apply recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL_SHIFT);
678                final JosmAction actionShift = new JosmAction(actionShortcutShiftKey, null, tr("Use this tag again"), scShift, false) {
679                    @Override
680                    public void actionPerformed(ActionEvent e) {
681                        action.actionPerformed(null);
682                        performTagAdding();
683                        selectKeysComboBox();
684                    }
685                };
686                recentTagsActions.add(action);
687                recentTagsActions.add(actionShift);
688                disableTagIfNeeded(t, action);
689                // Find and display icon
690                ImageIcon icon = MapPaintStyles.getNodeIcon(t, false); // Filters deprecated icon
691                if (icon == null) {
692                    // If no icon found in map style look at presets
693                    Map<String, String> map = new HashMap<>();
694                    map.put(t.getKey(), t.getValue());
695                    for (TaggingPreset tp : TaggingPreset.getMatchingPresets(null, map, false)) {
696                        icon = tp.getIcon();
697                        if (icon != null) {
698                            break;
699                        }
700                    }
701                    // If still nothing display an empty icon
702                    if (icon == null) {
703                        icon = new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
704                    }
705                }
706                GridBagConstraints gbc = new GridBagConstraints();
707                gbc.ipadx = 5;
708                mainPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
709                // Create tag label
710                final String color = action.isEnabled() ? "" : "; color:gray";
711                final JLabel tagLabel = new JLabel("<html>"
712                    + "<style>td{border:1px solid gray; font-weight:normal"+color+"}</style>"
713                    + "<table><tr><td>" + XmlWriter.encode(t.toString(), true) + "</td></tr></table></html>");
714                if (action.isEnabled()) {
715                    // Register action
716                    mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);
717                    mainPanel.getActionMap().put(actionShortcutKey, action);
718                    mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scShift.getKeyStroke(), actionShortcutShiftKey);
719                    mainPanel.getActionMap().put(actionShortcutShiftKey, actionShift);
720                    // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
721                    tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
722                    tagLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
723                    tagLabel.addMouseListener(new MouseAdapter() {
724                        @Override
725                        public void mouseClicked(MouseEvent e) {
726                            action.actionPerformed(null);
727                            // add tags and close window on double-click
728                            if (e.getClickCount()>1) {
729                                buttonAction(0, null); // emulate OK click and close the dialog
730                            }
731                            // add tags on Shift-Click
732                            if (e.isShiftDown()) {
733                                performTagAdding();
734                                selectKeysComboBox();
735                            }
736                        }
737                    });
738                } else {
739                    // Disable tag label
740                    tagLabel.setEnabled(false);
741                    // Explain in the tooltip why
742                    tagLabel.setToolTipText(tr("The key ''{0}'' is already used", t.getKey()));
743                }
744                // Finally add label to the resulting panel
745                JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
746                tagPanel.add(tagLabel);
747                mainPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
748            }
749        }
750
751        public void destroyActions() {
752            for (JosmAction action : recentTagsActions) {
753                action.destroy();
754            }
755        }
756
757        /**
758         * Read tags from comboboxes and add it to all selected objects
759         */
760        public final void performTagAdding() {
761            String key = Tag.removeWhiteSpaces(keys.getEditor().getItem().toString());
762            String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
763            if (key.isEmpty() || value.isEmpty()) return;
764            for (OsmPrimitive osm: sel) {
765                String val = osm.get(key);
766                if (val != null && !val.equals(value)) {
767                    if (!warnOverwriteKey(tr("You changed the value of ''{0}'' from ''{1}'' to ''{2}''.", key, val, value),
768                            "overwriteAddKey"))
769                        return;
770                    break;
771                }
772            }
773            lastAddKey = key;
774            lastAddValue = value;
775            recentTags.put(new Tag(key, value), null);
776            AutoCompletionManager.rememberUserInput(key, value, false);
777            commandCount++;
778            Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
779            changedKey = key;
780        }
781
782        public void undoAllTagsAdding() {
783            Main.main.undoRedo.undo(commandCount);
784        }
785
786        private void disableTagIfNeeded(final Tag t, final JosmAction action) {
787            // Disable action if its key is already set on the object (the key being absent from the keys list for this reason
788            // performing this action leads to autocomplete to the next key (see #7671 comments)
789            for (int j = 0; j < tagData.getRowCount(); ++j) {
790                if (t.getKey().equals(tagData.getValueAt(j, 0))) {
791                    action.setEnabled(false);
792                    break;
793                }
794            }
795        }
796    }
797}