| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.BorderLayout;
|
|---|
| 8 | import java.awt.Component;
|
|---|
| 9 | import java.awt.ComponentOrientation;
|
|---|
| 10 | import java.awt.Container;
|
|---|
| 11 | import java.awt.Cursor;
|
|---|
| 12 | import java.awt.Dimension;
|
|---|
| 13 | import java.awt.FlowLayout;
|
|---|
| 14 | import java.awt.Font;
|
|---|
| 15 | import java.awt.GridBagConstraints;
|
|---|
| 16 | import java.awt.GridBagLayout;
|
|---|
| 17 | import java.awt.event.ActionEvent;
|
|---|
| 18 | import java.awt.event.FocusEvent;
|
|---|
| 19 | import java.awt.event.FocusListener;
|
|---|
| 20 | import java.awt.event.InputEvent;
|
|---|
| 21 | import java.awt.event.KeyEvent;
|
|---|
| 22 | import java.awt.event.MouseAdapter;
|
|---|
| 23 | import java.awt.event.MouseEvent;
|
|---|
| 24 | import java.awt.event.WindowAdapter;
|
|---|
| 25 | import java.awt.event.WindowEvent;
|
|---|
| 26 | import java.awt.image.BufferedImage;
|
|---|
| 27 | import java.text.Normalizer;
|
|---|
| 28 | import java.util.ArrayList;
|
|---|
| 29 | import java.util.Arrays;
|
|---|
| 30 | import java.util.Collection;
|
|---|
| 31 | import java.util.Collections;
|
|---|
| 32 | import java.util.Comparator;
|
|---|
| 33 | import java.util.Iterator;
|
|---|
| 34 | import java.util.List;
|
|---|
| 35 | import java.util.Map;
|
|---|
| 36 | import java.util.Objects;
|
|---|
| 37 | import java.util.Optional;
|
|---|
| 38 | import java.util.TreeMap;
|
|---|
| 39 | import java.util.stream.Collectors;
|
|---|
| 40 | import java.util.stream.IntStream;
|
|---|
| 41 |
|
|---|
| 42 | import javax.swing.AbstractAction;
|
|---|
| 43 | import javax.swing.Action;
|
|---|
| 44 | import javax.swing.Box;
|
|---|
| 45 | import javax.swing.ButtonGroup;
|
|---|
| 46 | import javax.swing.ImageIcon;
|
|---|
| 47 | import javax.swing.JCheckBoxMenuItem;
|
|---|
| 48 | import javax.swing.JComponent;
|
|---|
| 49 | import javax.swing.JLabel;
|
|---|
| 50 | import javax.swing.JList;
|
|---|
| 51 | import javax.swing.JMenu;
|
|---|
| 52 | import javax.swing.JOptionPane;
|
|---|
| 53 | import javax.swing.JPanel;
|
|---|
| 54 | import javax.swing.JPopupMenu;
|
|---|
| 55 | import javax.swing.JRadioButtonMenuItem;
|
|---|
| 56 | import javax.swing.JTable;
|
|---|
| 57 | import javax.swing.KeyStroke;
|
|---|
| 58 | import javax.swing.ListCellRenderer;
|
|---|
| 59 | import javax.swing.SwingUtilities;
|
|---|
| 60 | import javax.swing.event.PopupMenuEvent;
|
|---|
| 61 | import javax.swing.event.PopupMenuListener;
|
|---|
| 62 | import javax.swing.table.DefaultTableModel;
|
|---|
| 63 |
|
|---|
| 64 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 65 | import org.openstreetmap.josm.actions.search.SearchAction;
|
|---|
| 66 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
|---|
| 67 | import org.openstreetmap.josm.command.Command;
|
|---|
| 68 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 69 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 70 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 71 | import org.openstreetmap.josm.data.osm.OsmDataManager;
|
|---|
| 72 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 73 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 74 | import org.openstreetmap.josm.data.osm.Tag;
|
|---|
| 75 | import org.openstreetmap.josm.data.osm.Tagged;
|
|---|
| 76 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
|---|
| 77 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
|---|
| 78 | import org.openstreetmap.josm.data.osm.search.SearchSetting;
|
|---|
| 79 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
|---|
| 80 | import org.openstreetmap.josm.data.preferences.EnumProperty;
|
|---|
| 81 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
|---|
| 82 | import org.openstreetmap.josm.data.preferences.ListProperty;
|
|---|
| 83 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
|---|
| 84 | import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
|
|---|
| 85 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
|---|
| 86 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 87 | import org.openstreetmap.josm.gui.IExtendedDialog;
|
|---|
| 88 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 89 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBox;
|
|---|
| 90 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompEvent;
|
|---|
| 91 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompListener;
|
|---|
| 92 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
|---|
| 93 | import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
|
|---|
| 94 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 95 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
|---|
| 96 | import org.openstreetmap.josm.gui.widgets.JosmListCellRenderer;
|
|---|
| 97 | import org.openstreetmap.josm.gui.widgets.OrientationAction;
|
|---|
| 98 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
|---|
| 99 | import org.openstreetmap.josm.io.XmlWriter;
|
|---|
| 100 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 101 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 102 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 103 | import org.openstreetmap.josm.tools.OsmPrimitiveImageProvider;
|
|---|
| 104 | import org.openstreetmap.josm.tools.PlatformManager;
|
|---|
| 105 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 106 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Class that helps PropertiesDialog add and edit tag values.
|
|---|
| 110 | * @since 5633
|
|---|
| 111 | */
|
|---|
| 112 | public class TagEditHelper {
|
|---|
| 113 |
|
|---|
| 114 | private final JTable tagTable;
|
|---|
| 115 | private final DefaultTableModel tagData;
|
|---|
| 116 | private final Map<String, Map<String, Integer>> valueCount;
|
|---|
| 117 |
|
|---|
| 118 | // Selection that we are editing by using both dialogs
|
|---|
| 119 | protected Collection<OsmPrimitive> sel;
|
|---|
| 120 |
|
|---|
| 121 | private String changedKey;
|
|---|
| 122 |
|
|---|
| 123 | static final Comparator<AutoCompletionItem> DEFAULT_AC_ITEM_COMPARATOR =
|
|---|
| 124 | (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
|---|
| 125 |
|
|---|
| 126 | /** Default number of recent tags */
|
|---|
| 127 | public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
|
|---|
| 128 | /** Maximum number of recent tags */
|
|---|
| 129 | public static final int MAX_LRU_TAGS_NUMBER = 30;
|
|---|
| 130 | /** Autocomplete keys by default */
|
|---|
| 131 | public static final BooleanProperty AUTOCOMPLETE_KEYS = new BooleanProperty("properties.autocomplete-keys", true);
|
|---|
| 132 | /** Autocomplete values by default */
|
|---|
| 133 | public static final BooleanProperty AUTOCOMPLETE_VALUES = new BooleanProperty("properties.autocomplete-values", true);
|
|---|
| 134 | /** Use English language for tag by default */
|
|---|
| 135 | public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false);
|
|---|
| 136 | /** Whether recent tags must be remembered */
|
|---|
| 137 | public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true);
|
|---|
| 138 | /** Number of recent tags */
|
|---|
| 139 | public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags",
|
|---|
| 140 | DEFAULT_LRU_TAGS_NUMBER);
|
|---|
| 141 | /** The preference storage of recent tags */
|
|---|
| 142 | public static final ListProperty PROPERTY_RECENT_TAGS = new ListProperty("properties.recent-tags",
|
|---|
| 143 | Collections.<String>emptyList());
|
|---|
| 144 | /** The preference list of tags which should not be remembered, since r9940 */
|
|---|
| 145 | public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
|
|---|
| 146 | new SearchSetting().writeToString());
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * What to do with recent tags where keys already exist
|
|---|
| 150 | */
|
|---|
| 151 | private enum RecentExisting {
|
|---|
| 152 | ENABLE,
|
|---|
| 153 | DISABLE,
|
|---|
| 154 | HIDE
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Preference setting for popup menu item "Recent tags with existing key"
|
|---|
| 159 | */
|
|---|
| 160 | public static final EnumProperty<RecentExisting> PROPERTY_RECENT_EXISTING = new EnumProperty<>(
|
|---|
| 161 | "properties.recently-added-tags-existing-key", RecentExisting.class, RecentExisting.DISABLE);
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * What to do after applying tag
|
|---|
| 165 | */
|
|---|
| 166 | private enum RefreshRecent {
|
|---|
| 167 | NO,
|
|---|
| 168 | STATUS,
|
|---|
| 169 | REFRESH
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Preference setting for popup menu item "Refresh recent tags list after applying tag"
|
|---|
| 174 | */
|
|---|
| 175 | public static final EnumProperty<RefreshRecent> PROPERTY_REFRESH_RECENT = new EnumProperty<>(
|
|---|
| 176 | "properties.refresh-recently-added-tags", RefreshRecent.class, RefreshRecent.STATUS);
|
|---|
| 177 |
|
|---|
| 178 | final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
|
|---|
| 179 | SearchSetting tagsToIgnore;
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Copy of recently added tags in sorted from newest to oldest order.
|
|---|
| 183 | *
|
|---|
| 184 | * We store the maximum number of recent tags to allow dynamic change of number of tags shown in the preferences.
|
|---|
| 185 | * Used to cache initial status.
|
|---|
| 186 | */
|
|---|
| 187 | private List<Tag> tags;
|
|---|
| 188 |
|
|---|
| 189 | static {
|
|---|
| 190 | // init user input based on recent tags
|
|---|
| 191 | final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
|
|---|
| 192 | recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
|
|---|
| 193 | recentTags.toList().forEach(tag -> AutoCompletionManager.rememberUserInput(tag.getKey(), tag.getValue(), false));
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * A custom list cell renderer that adds the value count to some items.
|
|---|
| 198 | */
|
|---|
| 199 | static class TEHListCellRenderer extends JosmListCellRenderer<AutoCompletionItem> {
|
|---|
| 200 | protected Map<String, Integer> map;
|
|---|
| 201 |
|
|---|
| 202 | TEHListCellRenderer(Component component, ListCellRenderer<? super AutoCompletionItem> renderer, Map<String, Integer> map) {
|
|---|
| 203 | super(component, renderer);
|
|---|
| 204 | this.map = map;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | @Override
|
|---|
| 208 | public Component getListCellRendererComponent(JList<? extends AutoCompletionItem> list, AutoCompletionItem value,
|
|---|
| 209 | int index, boolean isSelected, boolean cellHasFocus) {
|
|---|
| 210 | Integer count = null;
|
|---|
| 211 | // if there is a value count add it to the text
|
|---|
| 212 | if (map != null) {
|
|---|
| 213 | String text = value == null ? "" : value.toString();
|
|---|
| 214 | count = map.get(text);
|
|---|
| 215 | if (count != null) {
|
|---|
| 216 | value = new AutoCompletionItem(tr("{0} ({1})", text, count));
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | Component l = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
|---|
| 220 | l.setComponentOrientation(component.getComponentOrientation());
|
|---|
| 221 | if (count != null) {
|
|---|
| 222 | l.setFont(l.getFont().deriveFont(Font.ITALIC + Font.BOLD));
|
|---|
| 223 | }
|
|---|
| 224 | return l;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Constructs a new {@code TagEditHelper}.
|
|---|
| 230 | * @param tagTable tag table
|
|---|
| 231 | * @param propertyData table model
|
|---|
| 232 | * @param valueCount tag value count
|
|---|
| 233 | */
|
|---|
| 234 | public TagEditHelper(JTable tagTable, DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
|
|---|
| 235 | this.tagTable = tagTable;
|
|---|
| 236 | this.tagData = propertyData;
|
|---|
| 237 | this.valueCount = valueCount;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /**
|
|---|
| 241 | * Finds the key from given row of tag editor.
|
|---|
| 242 | * @param viewRow index of row
|
|---|
| 243 | * @return key of tag
|
|---|
| 244 | */
|
|---|
| 245 | public final String getDataKey(int viewRow) {
|
|---|
| 246 | return tagData.getValueAt(tagTable.convertRowIndexToModel(viewRow), 0).toString();
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * Determines if the given tag key is already used (by all selected primitives, not just some of them)
|
|---|
| 251 | * @param key the key to check
|
|---|
| 252 | * @return {@code true} if the key is used by all selected primitives (key not unset for at least one primitive)
|
|---|
| 253 | */
|
|---|
| 254 | @SuppressWarnings("unchecked")
|
|---|
| 255 | boolean containsDataKey(String key) {
|
|---|
| 256 | return IntStream.range(0, tagData.getRowCount())
|
|---|
| 257 | .anyMatch(i -> key.equals(tagData.getValueAt(i, 0)) /* sic! do not use getDataKey*/
|
|---|
| 258 | && !((Map<String, Integer>) tagData.getValueAt(i, 1)).containsKey("") /* sic! do not use getDataValues*/);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /**
|
|---|
| 262 | * Finds the values from given row of tag editor.
|
|---|
| 263 | * @param viewRow index of row
|
|---|
| 264 | * @return map of values and number of occurrences
|
|---|
| 265 | */
|
|---|
| 266 | @SuppressWarnings("unchecked")
|
|---|
| 267 | public final Map<String, Integer> getDataValues(int viewRow) {
|
|---|
| 268 | return (Map<String, Integer>) tagData.getValueAt(tagTable.convertRowIndexToModel(viewRow), 1);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * Open the add selection dialog and add a new key/value to the table (and
|
|---|
| 273 | * to the dataset, of course).
|
|---|
| 274 | */
|
|---|
| 275 | public void addTag() {
|
|---|
| 276 | changedKey = null;
|
|---|
| 277 | DataSet activeDataSet = OsmDataManager.getInstance().getActiveDataSet();
|
|---|
| 278 | if (activeDataSet == null)
|
|---|
| 279 | return;
|
|---|
| 280 | try {
|
|---|
| 281 | activeDataSet.beginUpdate();
|
|---|
| 282 | Collection<OsmPrimitive> selection = OsmDataManager.getInstance().getInProgressSelection();
|
|---|
| 283 | this.sel = selection;
|
|---|
| 284 | if (Utils.isEmpty(selection))
|
|---|
| 285 | return;
|
|---|
| 286 |
|
|---|
| 287 | final AddTagsDialog addDialog = getAddTagsDialog();
|
|---|
| 288 |
|
|---|
| 289 | addDialog.showDialog();
|
|---|
| 290 |
|
|---|
| 291 | addDialog.destroyActions();
|
|---|
| 292 | // Remote control can cause the selection to change, see #23191.
|
|---|
| 293 | if (addDialog.getValue() == 1 && (selection == sel || warnSelectionChanged())) {
|
|---|
| 294 | addDialog.performTagAdding(selection);
|
|---|
| 295 | } else {
|
|---|
| 296 | addDialog.undoAllTagsAdding();
|
|---|
| 297 | }
|
|---|
| 298 | } finally {
|
|---|
| 299 | activeDataSet.endUpdate();
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * Returns a new {@code AddTagsDialog}.
|
|---|
| 305 | * @return a new {@code AddTagsDialog}
|
|---|
| 306 | */
|
|---|
| 307 | protected AddTagsDialog getAddTagsDialog() {
|
|---|
| 308 | return new AddTagsDialog();
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /**
|
|---|
| 312 | * Edit the value in the tags table row.
|
|---|
| 313 | * @param row The row of the table from which the value is edited.
|
|---|
| 314 | * @param focusOnKey Determines if the initial focus should be set on key instead of value
|
|---|
| 315 | * @since 5653
|
|---|
| 316 | */
|
|---|
| 317 | public void editTag(final int row, boolean focusOnKey) {
|
|---|
| 318 | changedKey = null;
|
|---|
| 319 | sel = OsmDataManager.getInstance().getInProgressSelection();
|
|---|
| 320 | if (Utils.isEmpty(sel))
|
|---|
| 321 | return;
|
|---|
| 322 |
|
|---|
| 323 | final IEditTagDialog editDialog = getEditTagDialog(row, focusOnKey, getDataKey(row));
|
|---|
| 324 | editDialog.showDialog();
|
|---|
| 325 | if (editDialog.getValue() != 1)
|
|---|
| 326 | return;
|
|---|
| 327 | editDialog.performTagEdit();
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /**
|
|---|
| 331 | * Extracted interface of {@link EditTagDialog}.
|
|---|
| 332 | */
|
|---|
| 333 | protected interface IEditTagDialog extends IExtendedDialog {
|
|---|
| 334 | /**
|
|---|
| 335 | * Edit tags of multiple selected objects according to selected ComboBox values
|
|---|
| 336 | * If value == "", tag will be deleted
|
|---|
| 337 | * Confirmations may be needed.
|
|---|
| 338 | */
|
|---|
| 339 | void performTagEdit();
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | protected IEditTagDialog getEditTagDialog(int row, boolean focusOnKey, String key) {
|
|---|
| 343 | return new EditTagDialog(key, getDataValues(row), focusOnKey);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /**
|
|---|
| 347 | * If during last editProperty call user changed the key name, this key will be returned
|
|---|
| 348 | * Elsewhere, returns null.
|
|---|
| 349 | * @return The modified key, or {@code null}
|
|---|
| 350 | */
|
|---|
| 351 | public String getChangedKey() {
|
|---|
| 352 | return changedKey;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /**
|
|---|
| 356 | * Reset last changed key.
|
|---|
| 357 | */
|
|---|
| 358 | public void resetChangedKey() {
|
|---|
| 359 | changedKey = null;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /**
|
|---|
| 363 | * For a given key k, return a list of keys which are used as keys for
|
|---|
| 364 | * auto-completing values to increase the search space.
|
|---|
| 365 | * @param key the key k
|
|---|
| 366 | * @return a list of keys
|
|---|
| 367 | */
|
|---|
| 368 | private static List<String> getAutocompletionKeys(String key) {
|
|---|
| 369 | if ("name".equals(key) || "addr:street".equals(key))
|
|---|
| 370 | return Arrays.asList("addr:street", "name");
|
|---|
| 371 | else
|
|---|
| 372 | return Arrays.asList(key);
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /**
|
|---|
| 376 | * Load recently used tags from preferences if needed.
|
|---|
| 377 | */
|
|---|
| 378 | public void loadTagsIfNeeded() {
|
|---|
| 379 | loadTagsToIgnore();
|
|---|
| 380 | if (Boolean.TRUE.equals(PROPERTY_REMEMBER_TAGS.get()) && recentTags.isEmpty()) {
|
|---|
| 381 | recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
|
|---|
| 382 | }
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | void loadTagsToIgnore() {
|
|---|
| 386 | final SearchSetting searchSetting = Utils.firstNonNull(
|
|---|
| 387 | SearchSetting.readFromString(PROPERTY_TAGS_TO_IGNORE.get()), new SearchSetting());
|
|---|
| 388 | if (!Objects.equals(tagsToIgnore, searchSetting)) {
|
|---|
| 389 | try {
|
|---|
| 390 | tagsToIgnore = searchSetting;
|
|---|
| 391 | recentTags.setTagsToIgnore(tagsToIgnore);
|
|---|
| 392 | } catch (SearchParseError parseError) {
|
|---|
| 393 | warnAboutParseError(parseError);
|
|---|
| 394 | tagsToIgnore = new SearchSetting();
|
|---|
| 395 | recentTags.setTagsToIgnore(SearchCompiler.Never.INSTANCE);
|
|---|
| 396 | }
|
|---|
| 397 | }
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | private static void warnAboutParseError(SearchParseError parseError) {
|
|---|
| 401 | Logging.warn(parseError);
|
|---|
| 402 | JOptionPane.showMessageDialog(
|
|---|
| 403 | MainApplication.getMainFrame(),
|
|---|
| 404 | parseError.getMessage(),
|
|---|
| 405 | tr("Error"),
|
|---|
| 406 | JOptionPane.ERROR_MESSAGE
|
|---|
| 407 | );
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /**
|
|---|
| 411 | * Store recently used tags in preferences if needed.
|
|---|
| 412 | */
|
|---|
| 413 | public void saveTagsIfNeeded() {
|
|---|
| 414 | if (Boolean.TRUE.equals(PROPERTY_REMEMBER_TAGS.get()) && !recentTags.isEmpty()) {
|
|---|
| 415 | recentTags.saveToPreference(PROPERTY_RECENT_TAGS);
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | /**
|
|---|
| 420 | * Forget recently selected primitives to allow GC.
|
|---|
| 421 | * @since 14509
|
|---|
| 422 | */
|
|---|
| 423 | public void resetSelection() {
|
|---|
| 424 | sel = null;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /**
|
|---|
| 428 | * Update cache of recent tags used for displaying tags.
|
|---|
| 429 | */
|
|---|
| 430 | private void cacheRecentTags() {
|
|---|
| 431 | tags = recentTags.toList();
|
|---|
| 432 | Collections.reverse(tags);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /**
|
|---|
| 436 | * Returns the edited item with whitespaces removed
|
|---|
| 437 | * @param cb the combobox
|
|---|
| 438 | * @return the edited item with whitespaces removed
|
|---|
| 439 | * @since 18173
|
|---|
| 440 | */
|
|---|
| 441 | public static String getEditItem(AutoCompComboBox<AutoCompletionItem> cb) {
|
|---|
| 442 | return Utils.removeWhiteSpaces(cb.getEditorItemAsString());
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | /**
|
|---|
| 446 | * Returns the selected item or the edited item as string
|
|---|
| 447 | * @param cb the combobox
|
|---|
| 448 | * @return the selected item or the edited item as string
|
|---|
| 449 | * @since 18173
|
|---|
| 450 | */
|
|---|
| 451 | public static String getSelectedOrEditItem(AutoCompComboBox<AutoCompletionItem> cb) {
|
|---|
| 452 | final Object selectedItem = cb.getSelectedItem();
|
|---|
| 453 | if (selectedItem != null)
|
|---|
| 454 | return selectedItem.toString();
|
|---|
| 455 | return getEditItem(cb);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /**
|
|---|
| 459 | * Warn user about a selection change
|
|---|
| 460 | * @return {@code true} if the user wants to apply the tag change to the old selection
|
|---|
| 461 | */
|
|---|
| 462 | private static boolean warnSelectionChanged() {
|
|---|
| 463 | return ConditionalOptionPaneUtil.showConfirmationDialog("properties.selection-changed",
|
|---|
| 464 | MainApplication.getMainFrame(),
|
|---|
| 465 | tr("Data selection has changed since the dialog was opened"),
|
|---|
| 466 | tr("Apply tag change to old selection?"),
|
|---|
| 467 | JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.YES_OPTION);
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | /**
|
|---|
| 471 | * Warns user about a key being overwritten.
|
|---|
| 472 | * @param action The action done by the user. Must state what key is changed
|
|---|
| 473 | * @param togglePref The preference to save the checkbox state to
|
|---|
| 474 | * @return {@code true} if the user accepts to overwrite key, {@code false} otherwise
|
|---|
| 475 | */
|
|---|
| 476 | private static boolean warnOverwriteKey(String action, String togglePref) {
|
|---|
| 477 | return new ExtendedDialog(
|
|---|
| 478 | MainApplication.getMainFrame(),
|
|---|
| 479 | tr("Overwrite tag"),
|
|---|
| 480 | tr("Overwrite"), tr("Cancel"))
|
|---|
| 481 | .setButtonIcons("ok", "cancel")
|
|---|
| 482 | .setContent(action)
|
|---|
| 483 | .setCancelButton(2)
|
|---|
| 484 | .toggleEnable(togglePref)
|
|---|
| 485 | .showDialog().getValue() == 1;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | protected class EditTagDialog extends AbstractTagsDialog implements IEditTagDialog {
|
|---|
| 489 | private final String key;
|
|---|
| 490 | private final transient Map<String, Integer> m;
|
|---|
| 491 | private final transient Comparator<AutoCompletionItem> usedValuesAwareComparator;
|
|---|
| 492 | private final transient AutoCompletionManager autocomplete;
|
|---|
| 493 |
|
|---|
| 494 | protected EditTagDialog(String key, Map<String, Integer> map, boolean initialFocusOnKey) {
|
|---|
| 495 | super(MainApplication.getMainFrame(), trn("Change value?", "Change values?", map.size()), tr("OK"), tr("Cancel"));
|
|---|
| 496 | setButtonIcons("ok", "cancel");
|
|---|
| 497 | setCancelButton(2);
|
|---|
| 498 | configureContextsensitiveHelp("/Dialog/EditValue", true /* show help button */);
|
|---|
| 499 | this.key = key;
|
|---|
| 500 | this.m = map;
|
|---|
| 501 | this.initialFocusOnKey = initialFocusOnKey;
|
|---|
| 502 |
|
|---|
| 503 | usedValuesAwareComparator = (o1, o2) -> {
|
|---|
| 504 | boolean c1 = m.containsKey(o1.getValue());
|
|---|
| 505 | boolean c2 = m.containsKey(o2.getValue());
|
|---|
| 506 | if (c1 == c2)
|
|---|
| 507 | return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
|---|
| 508 | else if (c1)
|
|---|
| 509 | return -1;
|
|---|
| 510 | else
|
|---|
| 511 | return +1;
|
|---|
| 512 | };
|
|---|
| 513 |
|
|---|
| 514 | JPanel mainPanel = new JPanel(new BorderLayout());
|
|---|
| 515 |
|
|---|
| 516 | String msg = "<html>"+trn("This will change {0} object.",
|
|---|
| 517 | "This will change up to {0} objects.", sel.size(), sel.size())
|
|---|
| 518 | +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
|
|---|
| 519 |
|
|---|
| 520 | mainPanel.add(new JLabel(msg), BorderLayout.NORTH);
|
|---|
| 521 |
|
|---|
| 522 | JPanel p = new JPanel(new GridBagLayout()) {
|
|---|
| 523 | /**
|
|---|
| 524 | * This hack allows the comboboxes to have their own orientation.
|
|---|
| 525 | * <p>
|
|---|
| 526 | * The problem is that
|
|---|
| 527 | * {@link org.openstreetmap.josm.gui.ExtendedDialog#showDialog ExtendedDialog} calls
|
|---|
| 528 | * {@code applyComponentOrientation} very late in the dialog construction process
|
|---|
| 529 | * thus overwriting the orientation the components have chosen for themselves.
|
|---|
| 530 | * <p>
|
|---|
| 531 | * This stops the propagation of {@code applyComponentOrientation}, thus all
|
|---|
| 532 | * components may (and have to) set their own orientation.
|
|---|
| 533 | */
|
|---|
| 534 | @Override
|
|---|
| 535 | public void applyComponentOrientation(ComponentOrientation o) {
|
|---|
| 536 | setComponentOrientation(o);
|
|---|
| 537 | }
|
|---|
| 538 | };
|
|---|
| 539 | mainPanel.add(p, BorderLayout.CENTER);
|
|---|
| 540 |
|
|---|
| 541 | autocomplete = AutoCompletionManager.of(OsmDataManager.getInstance().getActiveDataSet());
|
|---|
| 542 | List<AutoCompletionItem> keyList = autocomplete.getTagKeys(DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 543 |
|
|---|
| 544 | keys = new AutoCompComboBox<>();
|
|---|
| 545 | keys.getModel().setComparator(Comparator.naturalOrder()); // according to Comparable
|
|---|
| 546 | keys.setEditable(true);
|
|---|
| 547 | keys.setPrototypeDisplayValue(new AutoCompletionItem("dummy"));
|
|---|
| 548 | keys.getModel().addAllElements(keyList);
|
|---|
| 549 | keys.setSelectedItemText(key);
|
|---|
| 550 |
|
|---|
| 551 | p.add(Box.createVerticalStrut(5), GBC.eol());
|
|---|
| 552 | p.add(new JLabel(tr("Key")), GBC.std());
|
|---|
| 553 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
|---|
| 554 | p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 555 |
|
|---|
| 556 | List<AutoCompletionItem> valueList = autocomplete.getTagValues(getAutocompletionKeys(key), usedValuesAwareComparator);
|
|---|
| 557 |
|
|---|
| 558 | final String selection = m.size() != 1 ? KeyedItem.DIFFERENT_I18N : m.entrySet().iterator().next().getKey();
|
|---|
| 559 |
|
|---|
| 560 | values = new AutoCompComboBox<>();
|
|---|
| 561 | values.getModel().setComparator(Comparator.naturalOrder());
|
|---|
| 562 | values.setRenderer(new TEHListCellRenderer(values, values.getRenderer(), valueCount.get(key)));
|
|---|
| 563 | values.setEditable(true);
|
|---|
| 564 | values.setPrototypeDisplayValue(new AutoCompletionItem("dummy"));
|
|---|
| 565 | values.getModel().addAllElements(valueList);
|
|---|
| 566 | values.setSelectedItemText(selection);
|
|---|
| 567 |
|
|---|
| 568 | p.add(Box.createVerticalStrut(5), GBC.eol());
|
|---|
| 569 | p.add(new JLabel(tr("Value")), GBC.std());
|
|---|
| 570 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
|---|
| 571 | p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 572 | p.add(Box.createVerticalStrut(2), GBC.eol());
|
|---|
| 573 |
|
|---|
| 574 | p.applyComponentOrientation(OrientationAction.getDefaultComponentOrientation());
|
|---|
| 575 | keys.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
|
|---|
| 576 | values.applyComponentOrientation(OrientationAction.getNamelikeOrientation(keys.getText()));
|
|---|
| 577 |
|
|---|
| 578 | setContent(mainPanel, false);
|
|---|
| 579 |
|
|---|
| 580 | addEventListeners();
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | @Override
|
|---|
| 584 | public void autoCompBefore(AutoCompEvent e) {
|
|---|
| 585 | updateValueModel(autocomplete, usedValuesAwareComparator);
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | @Override
|
|---|
| 589 | public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
|
|---|
| 590 | updateValueModel(autocomplete, usedValuesAwareComparator);
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | @Override
|
|---|
| 594 | public void performTagEdit() {
|
|---|
| 595 | String value = getEditItem(values);
|
|---|
| 596 | value = Normalizer.normalize(value, Normalizer.Form.NFC);
|
|---|
| 597 | if (value.isEmpty()) {
|
|---|
| 598 | value = null; // delete the key
|
|---|
| 599 | }
|
|---|
| 600 | String newkey = getEditItem(keys);
|
|---|
| 601 | newkey = Normalizer.normalize(newkey, Normalizer.Form.NFC);
|
|---|
| 602 | if (newkey.isEmpty()) {
|
|---|
| 603 | newkey = key;
|
|---|
| 604 | value = null; // delete the key instead
|
|---|
| 605 | }
|
|---|
| 606 | if (key.equals(newkey) && KeyedItem.DIFFERENT_I18N.equals(value))
|
|---|
| 607 | return;
|
|---|
| 608 | if (key.equals(newkey) || value == null) {
|
|---|
| 609 | UndoRedoHandler.getInstance().add(new ChangePropertyCommand(sel, newkey, value));
|
|---|
| 610 | if (value != null) {
|
|---|
| 611 | AutoCompletionManager.rememberUserInput(newkey, value, true);
|
|---|
| 612 | recentTags.add(new Tag(key, value));
|
|---|
| 613 | }
|
|---|
| 614 | } else {
|
|---|
| 615 | for (OsmPrimitive osm: sel) {
|
|---|
| 616 | if (osm.get(newkey) != null) {
|
|---|
| 617 | if (!warnOverwriteKey(tr("You changed the key from ''{0}'' to ''{1}''.", key, newkey)
|
|---|
| 618 | + "\n" + tr("The new key is already used, overwrite values?"),
|
|---|
| 619 | "overwriteEditKey"))
|
|---|
| 620 | return;
|
|---|
| 621 | break;
|
|---|
| 622 | }
|
|---|
| 623 | }
|
|---|
| 624 | Collection<Command> commands = new ArrayList<>();
|
|---|
| 625 | commands.add(new ChangePropertyCommand(sel, key, null));
|
|---|
| 626 | if (value.equals(KeyedItem.DIFFERENT_I18N)) {
|
|---|
| 627 | String newKey = newkey;
|
|---|
| 628 | sel.stream()
|
|---|
| 629 | .filter(osm -> osm.hasKey(key))
|
|---|
| 630 | .collect(Collectors.groupingBy(osm -> osm.get(key)))
|
|---|
| 631 | .forEach((newValue, osmPrimitives) -> commands.add(new ChangePropertyCommand(osmPrimitives, newKey, newValue)));
|
|---|
| 632 | } else {
|
|---|
| 633 | commands.add(new ChangePropertyCommand(sel, newkey, value));
|
|---|
| 634 | AutoCompletionManager.rememberUserInput(newkey, value, false);
|
|---|
| 635 | }
|
|---|
| 636 | UndoRedoHandler.getInstance().add(new SequenceCommand(
|
|---|
| 637 | trn("Change properties of up to {0} object",
|
|---|
| 638 | "Change properties of up to {0} objects", sel.size(), sel.size()),
|
|---|
| 639 | commands));
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | changedKey = newkey;
|
|---|
| 643 | }
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | protected abstract class AbstractTagsDialog extends ExtendedDialog implements AutoCompListener, FocusListener, PopupMenuListener {
|
|---|
| 647 | protected AutoCompComboBox<AutoCompletionItem> keys;
|
|---|
| 648 | protected AutoCompComboBox<AutoCompletionItem> values;
|
|---|
| 649 | protected boolean initialFocusOnKey = true;
|
|---|
| 650 | /**
|
|---|
| 651 | * The 'values' model is currently holding values for this key. Used for lazy-loading of values.
|
|---|
| 652 | */
|
|---|
| 653 | protected String currentValuesModelKey = "";
|
|---|
| 654 |
|
|---|
| 655 | AbstractTagsDialog(Component parent, String title, String... buttonTexts) {
|
|---|
| 656 | super(parent, title, buttonTexts);
|
|---|
| 657 | addMouseListener(new PopupMenuLauncher(popupMenu));
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | @Override
|
|---|
| 661 | public void setupDialog() {
|
|---|
| 662 | super.setupDialog();
|
|---|
| 663 | buttons.get(0).setEnabled(!OsmDataManager.getInstance().getActiveDataSet().isLocked());
|
|---|
| 664 | final Dimension size = getSize();
|
|---|
| 665 | // Set resizable only in width
|
|---|
| 666 | setMinimumSize(size);
|
|---|
| 667 | setPreferredSize(size);
|
|---|
| 668 | // setMaximumSize does not work, and never worked, but still it seems not to bother Oracle to fix this 10-year-old bug
|
|---|
| 669 | // https://bugs.openjdk.java.net/browse/JDK-6200438
|
|---|
| 670 | // https://bugs.openjdk.java.net/browse/JDK-6464548
|
|---|
| 671 |
|
|---|
| 672 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
|---|
| 673 | WindowGeometry.centerInWindow(MainApplication.getMainFrame(), size));
|
|---|
| 674 | keys.setFixedLocale(PROPERTY_FIX_TAG_LOCALE.get());
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | @Override
|
|---|
| 678 | public void setVisible(boolean visible) {
|
|---|
| 679 | // Do not want dialog to be resizable in height, as its size may increase each time because of the recently added tags
|
|---|
| 680 | // So need to modify the stored geometry (size part only) in order to use the automatic positioning mechanism
|
|---|
| 681 | if (visible) {
|
|---|
| 682 | WindowGeometry geometry = initWindowGeometry();
|
|---|
| 683 | Dimension storedSize = geometry.getSize();
|
|---|
| 684 | Dimension size = getSize();
|
|---|
| 685 | if (!storedSize.equals(size)) {
|
|---|
| 686 | if (storedSize.width < size.width) {
|
|---|
| 687 | storedSize.width = size.width;
|
|---|
| 688 | }
|
|---|
| 689 | if (storedSize.height != size.height) {
|
|---|
| 690 | storedSize.height = size.height;
|
|---|
| 691 | }
|
|---|
| 692 | rememberWindowGeometry(geometry);
|
|---|
| 693 | }
|
|---|
| 694 | updateOkButtonIcon();
|
|---|
| 695 | }
|
|---|
| 696 | super.setVisible(visible);
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | /**
|
|---|
| 700 | * Updates the values model if the key has changed
|
|---|
| 701 | *
|
|---|
| 702 | * @param autocomplete the autocompletion manager
|
|---|
| 703 | * @param comparator sorting order for the items in the combo dropdown
|
|---|
| 704 | */
|
|---|
| 705 | protected void updateValueModel(AutoCompletionManager autocomplete, Comparator<AutoCompletionItem> comparator) {
|
|---|
| 706 | String key = keys.getText();
|
|---|
| 707 | if (!key.equals(currentValuesModelKey)) {
|
|---|
| 708 | Logging.debug("updateValueModel: lazy loading values for key ''{0}''", key);
|
|---|
| 709 | // key has changed, reload model
|
|---|
| 710 | String savedText = values.getText();
|
|---|
| 711 | values.getModel().removeAllElements();
|
|---|
| 712 | values.getModel().addAllElements(autocomplete.getTagValues(getAutocompletionKeys(key), comparator));
|
|---|
| 713 | values.applyComponentOrientation(OrientationAction.getNamelikeOrientation(key));
|
|---|
| 714 | values.setSelectedItemText(savedText);
|
|---|
| 715 | values.getEditor().selectAll();
|
|---|
| 716 | currentValuesModelKey = key;
|
|---|
| 717 | }
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | protected void addEventListeners() {
|
|---|
| 721 | // OK on Enter in values
|
|---|
| 722 | values.getEditor().addActionListener(e -> buttonAction(0, null));
|
|---|
| 723 | // update values orientation according to key
|
|---|
| 724 | keys.getEditorComponent().addFocusListener(this);
|
|---|
| 725 | // update the "values" data model before an autocomplete or list dropdown
|
|---|
| 726 | values.getEditorComponent().addAutoCompListener(this);
|
|---|
| 727 | values.addPopupMenuListener(this);
|
|---|
| 728 | // set the initial focus to either combobox
|
|---|
| 729 | addWindowListener(new WindowAdapter() {
|
|---|
| 730 | @Override
|
|---|
| 731 | public void windowOpened(WindowEvent e) {
|
|---|
| 732 | if (initialFocusOnKey) {
|
|---|
| 733 | keys.requestFocus();
|
|---|
| 734 | } else {
|
|---|
| 735 | values.requestFocus();
|
|---|
| 736 | }
|
|---|
| 737 | }
|
|---|
| 738 | });
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | @Override
|
|---|
| 742 | public void autoCompPerformed(AutoCompEvent e) {
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | @Override
|
|---|
| 746 | public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | @Override
|
|---|
| 750 | public void popupMenuCanceled(PopupMenuEvent e) {
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | @Override
|
|---|
| 754 | public void focusGained(FocusEvent e) {
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | @Override
|
|---|
| 758 | public void focusLost(FocusEvent e) {
|
|---|
| 759 | // update the values combobox orientation if the key changed
|
|---|
| 760 | values.applyComponentOrientation(OrientationAction.getNamelikeOrientation(keys.getText()));
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | protected void updateOkButtonIcon() {
|
|---|
| 764 | if (buttons.isEmpty()) {
|
|---|
| 765 | return;
|
|---|
| 766 | }
|
|---|
| 767 | buttons.get(0).setIcon(findIcon(getSelectedOrEditItem(keys), getSelectedOrEditItem(values))
|
|---|
| 768 | .orElse(ImageProvider.get("ok", ImageProvider.ImageSizes.LARGEICON)));
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | protected Optional<ImageIcon> findIcon(String key, String value) {
|
|---|
| 772 | final Iterator<OsmPrimitive> osmPrimitiveIterator = sel.iterator();
|
|---|
| 773 | final OsmPrimitiveType type = osmPrimitiveIterator.hasNext() ? osmPrimitiveIterator.next().getType() : OsmPrimitiveType.NODE;
|
|---|
| 774 | return OsmPrimitiveImageProvider.getResource(key, value, type)
|
|---|
| 775 | .map(resource -> resource.getPaddedIcon(ImageProvider.ImageSizes.LARGEICON.getImageDimension()));
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | protected JPopupMenu popupMenu = new JPopupMenu() {
|
|---|
| 779 | private final JCheckBoxMenuItem fixTagLanguageCb = new JCheckBoxMenuItem(
|
|---|
| 780 | new AbstractAction(tr("Use English language for tag by default")) {
|
|---|
| 781 | @Override
|
|---|
| 782 | public void actionPerformed(ActionEvent e) {
|
|---|
| 783 | boolean use = ((JCheckBoxMenuItem) e.getSource()).getState();
|
|---|
| 784 | PROPERTY_FIX_TAG_LOCALE.put(use);
|
|---|
| 785 | keys.setFixedLocale(use);
|
|---|
| 786 | }
|
|---|
| 787 | });
|
|---|
| 788 | {
|
|---|
| 789 | add(fixTagLanguageCb);
|
|---|
| 790 | fixTagLanguageCb.setState(PROPERTY_FIX_TAG_LOCALE.get());
|
|---|
| 791 | }
|
|---|
| 792 | };
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | protected class AddTagsDialog extends AbstractTagsDialog {
|
|---|
| 796 | private final List<JosmAction> recentTagsActions = new ArrayList<>();
|
|---|
| 797 | private final JPanel mainPanel;
|
|---|
| 798 | private JPanel recentTagsPanel;
|
|---|
| 799 |
|
|---|
| 800 | // Counter of added commands for possible undo
|
|---|
| 801 | private int commandCount;
|
|---|
| 802 | private final transient AutoCompletionManager autocomplete;
|
|---|
| 803 |
|
|---|
| 804 | protected AddTagsDialog() {
|
|---|
| 805 | super(MainApplication.getMainFrame(), tr("Add tag"), tr("OK"), tr("Cancel"));
|
|---|
| 806 | setButtonIcons("ok", "cancel");
|
|---|
| 807 | setCancelButton(2);
|
|---|
| 808 | configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */);
|
|---|
| 809 |
|
|---|
| 810 | mainPanel = new JPanel(new GridBagLayout()) {
|
|---|
| 811 | /**
|
|---|
| 812 | * This hack allows the comboboxes to have their own orientation.
|
|---|
| 813 | * <p>
|
|---|
| 814 | * The problem is that
|
|---|
| 815 | * {@link org.openstreetmap.josm.gui.ExtendedDialog#showDialog ExtendedDialog} calls
|
|---|
| 816 | * {@code applyComponentOrientation} very late in the dialog construction process
|
|---|
| 817 | * thus overwriting the orientation the components have chosen for themselves.
|
|---|
| 818 | * <p>
|
|---|
| 819 | * This stops the propagation of {@code applyComponentOrientation}, thus all
|
|---|
| 820 | * components may (and have to) set their own orientation.
|
|---|
| 821 | */
|
|---|
| 822 | @Override
|
|---|
| 823 | public void applyComponentOrientation(ComponentOrientation o) {
|
|---|
| 824 | setComponentOrientation(o);
|
|---|
| 825 | }
|
|---|
| 826 | };
|
|---|
| 827 | mainPanel.add(new JLabel("<html>"+trn("This will change up to {0} object.",
|
|---|
| 828 | "This will change up to {0} objects.", sel.size(), sel.size())
|
|---|
| 829 | +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 830 |
|
|---|
| 831 | keys = new AutoCompComboBox<>();
|
|---|
| 832 | keys.setPrototypeDisplayValue(new AutoCompletionItem("dummy"));
|
|---|
| 833 | keys.setEditable(true);
|
|---|
| 834 | keys.getModel().setComparator(Comparator.naturalOrder()); // according to Comparable
|
|---|
| 835 | keys.setAutocompleteEnabled(AUTOCOMPLETE_KEYS.get());
|
|---|
| 836 |
|
|---|
| 837 | mainPanel.add(keys, GBC.eop().fill(GBC.HORIZONTAL));
|
|---|
| 838 | mainPanel.add(new JLabel(tr("Choose a value")), GBC.eol());
|
|---|
| 839 |
|
|---|
| 840 | values = new AutoCompComboBox<>();
|
|---|
| 841 | values.setPrototypeDisplayValue(new AutoCompletionItem("dummy"));
|
|---|
| 842 | values.setEditable(true);
|
|---|
| 843 | values.getModel().setComparator(Comparator.naturalOrder());
|
|---|
| 844 | values.setAutocompleteEnabled(AUTOCOMPLETE_VALUES.get());
|
|---|
| 845 |
|
|---|
| 846 | mainPanel.add(values, GBC.eop().fill(GBC.HORIZONTAL));
|
|---|
| 847 |
|
|---|
| 848 | cacheRecentTags();
|
|---|
| 849 | autocomplete = AutoCompletionManager.of(OsmDataManager.getInstance().getActiveDataSet());
|
|---|
| 850 | List<AutoCompletionItem> keyList = autocomplete.getTagKeys(DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 851 |
|
|---|
| 852 | // remove the object's tag keys from the list
|
|---|
| 853 | keyList.removeIf(item -> containsDataKey(item.getValue()));
|
|---|
| 854 |
|
|---|
| 855 | keys.getModel().addAllElements(keyList);
|
|---|
| 856 |
|
|---|
| 857 | updateValueModel(autocomplete, DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 858 |
|
|---|
| 859 | // pre-fill first recent tag for which the key is not already present
|
|---|
| 860 | tags.stream()
|
|---|
| 861 | .filter(tag -> !containsDataKey(tag.getKey()))
|
|---|
| 862 | .findFirst()
|
|---|
| 863 | .ifPresent(tag -> {
|
|---|
| 864 | keys.setSelectedItemText(tag.getKey());
|
|---|
| 865 | values.setSelectedItemText(tag.getValue());
|
|---|
| 866 | });
|
|---|
| 867 |
|
|---|
| 868 |
|
|---|
| 869 | keys.addActionListener(ignore -> updateOkButtonIcon());
|
|---|
| 870 | values.addActionListener(ignore -> updateOkButtonIcon());
|
|---|
| 871 |
|
|---|
| 872 | // Add tag on Shift-Enter
|
|---|
| 873 | mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
|---|
| 874 | KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK), "addAndContinue");
|
|---|
| 875 | mainPanel.getActionMap().put("addAndContinue", new AbstractAction() {
|
|---|
| 876 | @Override
|
|---|
| 877 | public void actionPerformed(ActionEvent e) {
|
|---|
| 878 | performTagAdding();
|
|---|
| 879 | refreshRecentTags();
|
|---|
| 880 | keys.requestFocus();
|
|---|
| 881 | }
|
|---|
| 882 | });
|
|---|
| 883 |
|
|---|
| 884 | suggestRecentlyAddedTags();
|
|---|
| 885 |
|
|---|
| 886 | mainPanel.add(Box.createVerticalGlue(), GBC.eop().fill());
|
|---|
| 887 | mainPanel.applyComponentOrientation(OrientationAction.getDefaultComponentOrientation());
|
|---|
| 888 |
|
|---|
| 889 | setContent(mainPanel, false);
|
|---|
| 890 |
|
|---|
| 891 | addEventListeners();
|
|---|
| 892 |
|
|---|
| 893 | popupMenu.add(new AbstractAction(tr("Set number of recently added tags")) {
|
|---|
| 894 | @Override
|
|---|
| 895 | public void actionPerformed(ActionEvent e) {
|
|---|
| 896 | selectNumberOfTags();
|
|---|
| 897 | suggestRecentlyAddedTags();
|
|---|
| 898 | }
|
|---|
| 899 | });
|
|---|
| 900 |
|
|---|
| 901 | popupMenu.add(buildMenuRecentExisting());
|
|---|
| 902 | popupMenu.add(buildMenuRefreshRecent());
|
|---|
| 903 |
|
|---|
| 904 | JCheckBoxMenuItem rememberLastTags = new JCheckBoxMenuItem(
|
|---|
| 905 | new AbstractAction(tr("Remember last used tags after a restart")) {
|
|---|
| 906 | @Override
|
|---|
| 907 | public void actionPerformed(ActionEvent e) {
|
|---|
| 908 | boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
|
|---|
| 909 | PROPERTY_REMEMBER_TAGS.put(state);
|
|---|
| 910 | if (state)
|
|---|
| 911 | saveTagsIfNeeded();
|
|---|
| 912 | }
|
|---|
| 913 | });
|
|---|
| 914 | rememberLastTags.setState(PROPERTY_REMEMBER_TAGS.get());
|
|---|
| 915 | popupMenu.add(rememberLastTags);
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | @Override
|
|---|
| 919 | public void autoCompBefore(AutoCompEvent e) {
|
|---|
| 920 | updateValueModel(autocomplete, DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | @Override
|
|---|
| 924 | public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
|
|---|
| 925 | updateValueModel(autocomplete, DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | private JMenu buildMenuRecentExisting() {
|
|---|
| 929 | JMenu menu = new JMenu(tr("Recent tags with existing key"));
|
|---|
| 930 | TreeMap<RecentExisting, String> radios = new TreeMap<>();
|
|---|
| 931 | radios.put(RecentExisting.ENABLE, tr("Enable"));
|
|---|
| 932 | radios.put(RecentExisting.DISABLE, tr("Disable"));
|
|---|
| 933 | radios.put(RecentExisting.HIDE, tr("Hide"));
|
|---|
| 934 | ButtonGroup buttonGroup = new ButtonGroup();
|
|---|
| 935 | for (final Map.Entry<RecentExisting, String> entry : radios.entrySet()) {
|
|---|
| 936 | JRadioButtonMenuItem radio = new JRadioButtonMenuItem(new AbstractAction(entry.getValue()) {
|
|---|
| 937 | @Override
|
|---|
| 938 | public void actionPerformed(ActionEvent e) {
|
|---|
| 939 | PROPERTY_RECENT_EXISTING.put(entry.getKey());
|
|---|
| 940 | suggestRecentlyAddedTags();
|
|---|
| 941 | }
|
|---|
| 942 | });
|
|---|
| 943 | buttonGroup.add(radio);
|
|---|
| 944 | radio.setSelected(PROPERTY_RECENT_EXISTING.get() == entry.getKey());
|
|---|
| 945 | menu.add(radio);
|
|---|
| 946 | }
|
|---|
| 947 | return menu;
|
|---|
| 948 | }
|
|---|
| 949 |
|
|---|
| 950 | private JMenu buildMenuRefreshRecent() {
|
|---|
| 951 | JMenu menu = new JMenu(tr("Refresh recent tags list after applying tag"));
|
|---|
| 952 | TreeMap<RefreshRecent, String> radios = new TreeMap<>();
|
|---|
| 953 | radios.put(RefreshRecent.NO, tr("No refresh"));
|
|---|
| 954 | radios.put(RefreshRecent.STATUS, tr("Refresh tag status only (enabled / disabled)"));
|
|---|
| 955 | radios.put(RefreshRecent.REFRESH, tr("Refresh tag status and list of recently added tags"));
|
|---|
| 956 | ButtonGroup buttonGroup = new ButtonGroup();
|
|---|
| 957 | for (final Map.Entry<RefreshRecent, String> entry : radios.entrySet()) {
|
|---|
| 958 | JRadioButtonMenuItem radio = new JRadioButtonMenuItem(new AbstractAction(entry.getValue()) {
|
|---|
| 959 | @Override
|
|---|
| 960 | public void actionPerformed(ActionEvent e) {
|
|---|
| 961 | PROPERTY_REFRESH_RECENT.put(entry.getKey());
|
|---|
| 962 | }
|
|---|
| 963 | });
|
|---|
| 964 | buttonGroup.add(radio);
|
|---|
| 965 | radio.setSelected(PROPERTY_REFRESH_RECENT.get() == entry.getKey());
|
|---|
| 966 | menu.add(radio);
|
|---|
| 967 | }
|
|---|
| 968 | return menu;
|
|---|
| 969 | }
|
|---|
| 970 |
|
|---|
| 971 | @Override
|
|---|
| 972 | public void setContentPane(Container contentPane) {
|
|---|
| 973 | final int commandDownMask = PlatformManager.getPlatform().getMenuShortcutKeyMaskEx();
|
|---|
| 974 | List<String> lines = new ArrayList<>();
|
|---|
| 975 | Shortcut.findShortcut(KeyEvent.VK_1, commandDownMask).ifPresent(sc ->
|
|---|
| 976 | lines.add(sc.getKeyText() + ' ' + tr("to apply first suggestion"))
|
|---|
| 977 | );
|
|---|
| 978 | lines.add(Shortcut.getKeyText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK)) + ' '
|
|---|
| 979 | +tr("to add without closing the dialog"));
|
|---|
| 980 | Shortcut.findShortcut(KeyEvent.VK_1, commandDownMask | KeyEvent.SHIFT_DOWN_MASK).ifPresent(sc ->
|
|---|
| 981 | lines.add(sc.getKeyText() + ' ' + tr("to add first suggestion without closing the dialog"))
|
|---|
| 982 | );
|
|---|
| 983 | final JLabel helpLabel = new JLabel("<html>" + String.join("<br>", lines) + "</html>");
|
|---|
| 984 | helpLabel.setFont(helpLabel.getFont().deriveFont(Font.PLAIN));
|
|---|
| 985 | contentPane.add(helpLabel, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(5, 5, 5, 5));
|
|---|
| 986 | super.setContentPane(contentPane);
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | protected void selectNumberOfTags() {
|
|---|
| 990 | String s = String.format("%d", PROPERTY_RECENT_TAGS_NUMBER.get());
|
|---|
| 991 | while (true) {
|
|---|
| 992 | s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"), s);
|
|---|
| 993 | if (Utils.isEmpty(s)) {
|
|---|
| 994 | return;
|
|---|
| 995 | }
|
|---|
| 996 | try {
|
|---|
| 997 | int v = Integer.parseInt(s);
|
|---|
| 998 | if (v >= 0 && v <= MAX_LRU_TAGS_NUMBER) {
|
|---|
| 999 | PROPERTY_RECENT_TAGS_NUMBER.put(v);
|
|---|
| 1000 | return;
|
|---|
| 1001 | }
|
|---|
| 1002 | } catch (NumberFormatException ex) {
|
|---|
| 1003 | Logging.warn(ex);
|
|---|
| 1004 | }
|
|---|
| 1005 | JOptionPane.showMessageDialog(this, tr("Please enter integer number between 0 and {0}", MAX_LRU_TAGS_NUMBER));
|
|---|
| 1006 | }
|
|---|
| 1007 | }
|
|---|
| 1008 |
|
|---|
| 1009 | protected void suggestRecentlyAddedTags() {
|
|---|
| 1010 | if (recentTagsPanel == null) {
|
|---|
| 1011 | recentTagsPanel = new JPanel(new GridBagLayout());
|
|---|
| 1012 | buildRecentTagsPanel();
|
|---|
| 1013 | mainPanel.add(recentTagsPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 1014 | } else {
|
|---|
| 1015 | Dimension panelOldSize = recentTagsPanel.getPreferredSize();
|
|---|
| 1016 | recentTagsPanel.removeAll();
|
|---|
| 1017 | buildRecentTagsPanel();
|
|---|
| 1018 | Dimension panelNewSize = recentTagsPanel.getPreferredSize();
|
|---|
| 1019 | Dimension dialogOldSize = getMinimumSize();
|
|---|
| 1020 | Dimension dialogNewSize = new Dimension(dialogOldSize.width, dialogOldSize.height-panelOldSize.height+panelNewSize.height);
|
|---|
| 1021 | setMinimumSize(dialogNewSize);
|
|---|
| 1022 | setPreferredSize(dialogNewSize);
|
|---|
| 1023 | setSize(dialogNewSize);
|
|---|
| 1024 | revalidate();
|
|---|
| 1025 | repaint();
|
|---|
| 1026 | }
|
|---|
| 1027 | }
|
|---|
| 1028 |
|
|---|
| 1029 | protected void buildRecentTagsPanel() {
|
|---|
| 1030 | final int tagsToShow = Math.min(PROPERTY_RECENT_TAGS_NUMBER.get(), MAX_LRU_TAGS_NUMBER);
|
|---|
| 1031 | if (!(tagsToShow > 0 && !recentTags.isEmpty()))
|
|---|
| 1032 | return;
|
|---|
| 1033 | recentTagsPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
|
|---|
| 1034 |
|
|---|
| 1035 | int count = 0;
|
|---|
| 1036 | destroyActions();
|
|---|
| 1037 | for (int i = 0; i < tags.size() && count < tagsToShow; i++) {
|
|---|
| 1038 | final Tag t = tags.get(i);
|
|---|
| 1039 | boolean keyExists = containsDataKey(t.getKey());
|
|---|
| 1040 | if (keyExists && PROPERTY_RECENT_EXISTING.get() == RecentExisting.HIDE)
|
|---|
| 1041 | continue;
|
|---|
| 1042 | count++;
|
|---|
| 1043 | // Create action for reusing the tag, with keyboard shortcut
|
|---|
| 1044 | /* POSSIBLE SHORTCUTS: 1,2,3,4,5,6,7,8,9,0=10 */
|
|---|
| 1045 | final Shortcut sc = count > 10 ? null : Shortcut.registerShortcut("properties:recent:" + count,
|
|---|
| 1046 | tr("Choose recent tag {0}", count), KeyEvent.VK_0 + (count % 10), Shortcut.CTRL);
|
|---|
| 1047 | final JosmAction action = new JosmAction(
|
|---|
| 1048 | tr("Choose recent tag {0}", count), null, tr("Use this tag again"), sc, false) {
|
|---|
| 1049 | @Override
|
|---|
| 1050 | public void actionPerformed(ActionEvent e) {
|
|---|
| 1051 | keys.setSelectedItemText(t.getKey());
|
|---|
| 1052 | // fix #7951, #8298 - update list of values before setting value (?)
|
|---|
| 1053 | updateValueModel(autocomplete, DEFAULT_AC_ITEM_COMPARATOR);
|
|---|
| 1054 | values.setSelectedItemText(t.getValue());
|
|---|
| 1055 | values.requestFocus();
|
|---|
| 1056 | }
|
|---|
| 1057 | };
|
|---|
| 1058 | /* POSSIBLE SHORTCUTS: 1,2,3,4,5,6,7,8,9,0=10 */
|
|---|
| 1059 | final Shortcut scShift = count > 10 ? null : Shortcut.registerShortcut("properties:recent:apply:" + count,
|
|---|
| 1060 | tr("Apply recent tag {0}", count), KeyEvent.VK_0 + (count % 10), Shortcut.CTRL_SHIFT);
|
|---|
| 1061 | final JosmAction actionShift = new JosmAction(
|
|---|
| 1062 | tr("Apply recent tag {0}", count), null, tr("Use this tag again"), scShift, false) {
|
|---|
| 1063 | @Override
|
|---|
| 1064 | public void actionPerformed(ActionEvent e) {
|
|---|
| 1065 | action.actionPerformed(null);
|
|---|
| 1066 | performTagAdding();
|
|---|
| 1067 | refreshRecentTags();
|
|---|
| 1068 | keys.requestFocus();
|
|---|
| 1069 | }
|
|---|
| 1070 | };
|
|---|
| 1071 | recentTagsActions.add(action);
|
|---|
| 1072 | recentTagsActions.add(actionShift);
|
|---|
| 1073 | if (keyExists && PROPERTY_RECENT_EXISTING.get() == RecentExisting.DISABLE) {
|
|---|
| 1074 | action.setEnabled(false);
|
|---|
| 1075 | }
|
|---|
| 1076 | ImageIcon icon = findIcon(t.getKey(), t.getValue())
|
|---|
| 1077 | // If still nothing display an empty icon
|
|---|
| 1078 |
|
|---|
| 1079 | .orElseGet(() -> new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)));
|
|---|
| 1080 | GridBagConstraints gbc = new GridBagConstraints();
|
|---|
| 1081 | gbc.ipadx = 5;
|
|---|
| 1082 | recentTagsPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
|
|---|
| 1083 | // Create tag label
|
|---|
| 1084 | final String color = action.isEnabled() ? "" : "; color:gray";
|
|---|
| 1085 | final JLabel tagLabel = new JLabel("<html>"
|
|---|
| 1086 | + "<style>td{" + color + "}</style>"
|
|---|
| 1087 | + "<table><tr>"
|
|---|
| 1088 | + "<td>" + count + ".</td>"
|
|---|
| 1089 | + "<td style='border:1px solid gray'>" + XmlWriter.encode(t.toString(), true) + '<' +
|
|---|
| 1090 | "/td></tr></table></html>");
|
|---|
| 1091 | tagLabel.setFont(tagLabel.getFont().deriveFont(Font.PLAIN));
|
|---|
| 1092 | if (action.isEnabled() && sc != null && scShift != null) {
|
|---|
| 1093 | // Register action
|
|---|
| 1094 | recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "choose"+count);
|
|---|
| 1095 | recentTagsPanel.getActionMap().put("choose"+count, action);
|
|---|
| 1096 | recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scShift.getKeyStroke(), "apply"+count);
|
|---|
| 1097 | recentTagsPanel.getActionMap().put("apply"+count, actionShift);
|
|---|
| 1098 | }
|
|---|
| 1099 | if (action.isEnabled()) {
|
|---|
| 1100 | // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
|
|---|
| 1101 | tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
|
|---|
| 1102 | tagLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|---|
| 1103 | tagLabel.addMouseListener(new MouseAdapter() {
|
|---|
| 1104 | @Override
|
|---|
| 1105 | public void mouseClicked(MouseEvent e) {
|
|---|
| 1106 | action.actionPerformed(null);
|
|---|
| 1107 | if (SwingUtilities.isRightMouseButton(e)) {
|
|---|
| 1108 | Component component = e.getComponent();
|
|---|
| 1109 | if (component.isShowing()) {
|
|---|
| 1110 | new TagPopupMenu(t).show(component, e.getX(), e.getY());
|
|---|
| 1111 | }
|
|---|
| 1112 | } else if (e.isShiftDown()) {
|
|---|
| 1113 | // add tags on Shift-Click
|
|---|
| 1114 | performTagAdding();
|
|---|
| 1115 | refreshRecentTags();
|
|---|
| 1116 | keys.requestFocus();
|
|---|
| 1117 | } else if (e.getClickCount() > 1) {
|
|---|
| 1118 | // add tags and close window on double-click
|
|---|
| 1119 | buttonAction(0, null); // emulate OK click and close the dialog
|
|---|
| 1120 | }
|
|---|
| 1121 | }
|
|---|
| 1122 | });
|
|---|
| 1123 | } else {
|
|---|
| 1124 | // Disable tag label
|
|---|
| 1125 | tagLabel.setEnabled(false);
|
|---|
| 1126 | // Explain in the tooltip why
|
|---|
| 1127 | tagLabel.setToolTipText(tr("The key ''{0}'' is already used", t.getKey()));
|
|---|
| 1128 | }
|
|---|
| 1129 | // Finally add label to the resulting panel
|
|---|
| 1130 | JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
|---|
| 1131 | tagPanel.add(tagLabel);
|
|---|
| 1132 | recentTagsPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 1133 | }
|
|---|
| 1134 | // Clear label if no tags were added
|
|---|
| 1135 | if (count == 0) {
|
|---|
| 1136 | recentTagsPanel.removeAll();
|
|---|
| 1137 | }
|
|---|
| 1138 | }
|
|---|
| 1139 |
|
|---|
| 1140 | class TagPopupMenu extends JPopupMenu {
|
|---|
| 1141 |
|
|---|
| 1142 | TagPopupMenu(Tag t) {
|
|---|
| 1143 | add(new IgnoreTagAction(tr("Ignore key ''{0}''", t.getKey()), new Tag(t.getKey(), "")));
|
|---|
| 1144 | add(new IgnoreTagAction(tr("Ignore tag ''{0}''", t), t));
|
|---|
| 1145 | add(new EditIgnoreTagsAction());
|
|---|
| 1146 | }
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | class IgnoreTagAction extends AbstractAction {
|
|---|
| 1150 | final transient Tag tag;
|
|---|
| 1151 |
|
|---|
| 1152 | IgnoreTagAction(String name, Tag tag) {
|
|---|
| 1153 | super(name);
|
|---|
| 1154 | this.tag = tag;
|
|---|
| 1155 | }
|
|---|
| 1156 |
|
|---|
| 1157 | @Override
|
|---|
| 1158 | public void actionPerformed(ActionEvent e) {
|
|---|
| 1159 | try {
|
|---|
| 1160 | if (tagsToIgnore != null) {
|
|---|
| 1161 | recentTags.ignoreTag(tag, tagsToIgnore);
|
|---|
| 1162 | PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
|
|---|
| 1163 | }
|
|---|
| 1164 | } catch (SearchParseError parseError) {
|
|---|
| 1165 | throw new IllegalStateException(parseError);
|
|---|
| 1166 | }
|
|---|
| 1167 | }
|
|---|
| 1168 | }
|
|---|
| 1169 |
|
|---|
| 1170 | class EditIgnoreTagsAction extends AbstractAction {
|
|---|
| 1171 |
|
|---|
| 1172 | EditIgnoreTagsAction() {
|
|---|
| 1173 | super(tr("Edit ignore list"));
|
|---|
| 1174 | }
|
|---|
| 1175 |
|
|---|
| 1176 | @Override
|
|---|
| 1177 | public void actionPerformed(ActionEvent e) {
|
|---|
| 1178 | final SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
|
|---|
| 1179 | if (newTagsToIngore == null) {
|
|---|
| 1180 | return;
|
|---|
| 1181 | }
|
|---|
| 1182 | try {
|
|---|
| 1183 | tagsToIgnore = newTagsToIngore;
|
|---|
| 1184 | recentTags.setTagsToIgnore(tagsToIgnore);
|
|---|
| 1185 | PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
|
|---|
| 1186 | } catch (SearchParseError parseError) {
|
|---|
| 1187 | warnAboutParseError(parseError);
|
|---|
| 1188 | }
|
|---|
| 1189 | }
|
|---|
| 1190 | }
|
|---|
| 1191 |
|
|---|
| 1192 | /**
|
|---|
| 1193 | * Destroy the recentTagsActions.
|
|---|
| 1194 | */
|
|---|
| 1195 | public void destroyActions() {
|
|---|
| 1196 | for (JosmAction action : recentTagsActions) {
|
|---|
| 1197 | action.destroy();
|
|---|
| 1198 | }
|
|---|
| 1199 | recentTagsActions.clear();
|
|---|
| 1200 | }
|
|---|
| 1201 |
|
|---|
| 1202 | /**
|
|---|
| 1203 | * Read tags from comboboxes and add it to all selected objects
|
|---|
| 1204 | */
|
|---|
| 1205 | public final void performTagAdding() {
|
|---|
| 1206 | Collection<OsmPrimitive> selection = sel;
|
|---|
| 1207 | if (!Utils.isEmpty(selection)) {
|
|---|
| 1208 | performTagAdding(selection);
|
|---|
| 1209 | }
|
|---|
| 1210 | }
|
|---|
| 1211 |
|
|---|
| 1212 | /**
|
|---|
| 1213 | * Read tags from comboboxes and add it to all selected objects
|
|---|
| 1214 | * @param selection The selection to perform tag adding on
|
|---|
| 1215 | * @since 18842
|
|---|
| 1216 | */
|
|---|
| 1217 | private void performTagAdding(Collection<OsmPrimitive> selection) {
|
|---|
| 1218 | String key = getEditItem(keys);
|
|---|
| 1219 | String value = getEditItem(values);
|
|---|
| 1220 | if (key.isEmpty() || value.isEmpty())
|
|---|
| 1221 | return;
|
|---|
| 1222 | for (Tagged osm : selection) {
|
|---|
| 1223 | String val = osm.get(key);
|
|---|
| 1224 | if (val != null && !val.equals(value)) {
|
|---|
| 1225 | String valueHtmlString = Utils.joinAsHtmlUnorderedList(Arrays.asList("<strike>" + val + "</strike>", value));
|
|---|
| 1226 | if (!warnOverwriteKey("<html>"
|
|---|
| 1227 | + tr("You changed the value of ''{0}'': {1}", key, valueHtmlString)
|
|---|
| 1228 | + tr("Overwrite?"), "overwriteAddKey"))
|
|---|
| 1229 | return;
|
|---|
| 1230 | break;
|
|---|
| 1231 | }
|
|---|
| 1232 | }
|
|---|
| 1233 | recentTags.add(new Tag(key, value));
|
|---|
| 1234 | valueCount.put(key, new TreeMap<>());
|
|---|
| 1235 | AutoCompletionManager.rememberUserInput(key, value, false);
|
|---|
| 1236 | commandCount++;
|
|---|
| 1237 | UndoRedoHandler.getInstance().add(new ChangePropertyCommand(selection, key, value));
|
|---|
| 1238 | changedKey = key;
|
|---|
| 1239 | clearEntries();
|
|---|
| 1240 | }
|
|---|
| 1241 |
|
|---|
| 1242 | protected void clearEntries() {
|
|---|
| 1243 | keys.getEditor().setItem("");
|
|---|
| 1244 | values.getEditor().setItem("");
|
|---|
| 1245 | }
|
|---|
| 1246 |
|
|---|
| 1247 | /**
|
|---|
| 1248 | * Undo all tag add commands that this dialog has created
|
|---|
| 1249 | */
|
|---|
| 1250 | public void undoAllTagsAdding() {
|
|---|
| 1251 | UndoRedoHandler.getInstance().undo(commandCount);
|
|---|
| 1252 | }
|
|---|
| 1253 |
|
|---|
| 1254 | private void refreshRecentTags() {
|
|---|
| 1255 | switch (PROPERTY_REFRESH_RECENT.get()) {
|
|---|
| 1256 | case REFRESH:
|
|---|
| 1257 | cacheRecentTags();
|
|---|
| 1258 | suggestRecentlyAddedTags();
|
|---|
| 1259 | break;
|
|---|
| 1260 | case STATUS:
|
|---|
| 1261 | suggestRecentlyAddedTags();
|
|---|
| 1262 | break;
|
|---|
| 1263 | default: // Do nothing
|
|---|
| 1264 | }
|
|---|
| 1265 | }
|
|---|
| 1266 | }
|
|---|
| 1267 | }
|
|---|