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.Container;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.Dimension;
|
---|
12 | import java.awt.FlowLayout;
|
---|
13 | import java.awt.Font;
|
---|
14 | import java.awt.GridBagConstraints;
|
---|
15 | import java.awt.GridBagLayout;
|
---|
16 | import java.awt.datatransfer.Clipboard;
|
---|
17 | import java.awt.datatransfer.Transferable;
|
---|
18 | import java.awt.event.ActionEvent;
|
---|
19 | import java.awt.event.FocusAdapter;
|
---|
20 | import java.awt.event.FocusEvent;
|
---|
21 | import java.awt.event.InputEvent;
|
---|
22 | import java.awt.event.KeyEvent;
|
---|
23 | import java.awt.event.MouseAdapter;
|
---|
24 | import java.awt.event.MouseEvent;
|
---|
25 | import java.awt.event.WindowAdapter;
|
---|
26 | import java.awt.event.WindowEvent;
|
---|
27 | import java.awt.image.BufferedImage;
|
---|
28 | import java.text.Normalizer;
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.Arrays;
|
---|
31 | import java.util.Collection;
|
---|
32 | import java.util.Collections;
|
---|
33 | import java.util.Comparator;
|
---|
34 | import java.util.HashMap;
|
---|
35 | import java.util.List;
|
---|
36 | import java.util.Map;
|
---|
37 | import java.util.Objects;
|
---|
38 | import java.util.TreeMap;
|
---|
39 | import java.util.stream.IntStream;
|
---|
40 |
|
---|
41 | import javax.swing.AbstractAction;
|
---|
42 | import javax.swing.Action;
|
---|
43 | import javax.swing.Box;
|
---|
44 | import javax.swing.ButtonGroup;
|
---|
45 | import javax.swing.DefaultListCellRenderer;
|
---|
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.table.DefaultTableModel;
|
---|
61 | import javax.swing.text.JTextComponent;
|
---|
62 |
|
---|
63 | import org.openstreetmap.josm.Main;
|
---|
64 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
65 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
66 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
67 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
68 | import org.openstreetmap.josm.command.Command;
|
---|
69 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
70 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
71 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
72 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
73 | import org.openstreetmap.josm.data.preferences.CollectionProperty;
|
---|
74 | import org.openstreetmap.josm.data.preferences.EnumProperty;
|
---|
75 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
76 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
77 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
78 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
79 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
80 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
|
---|
81 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
|
---|
82 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
83 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
84 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
85 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
86 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
87 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
88 | import org.openstreetmap.josm.tools.GBC;
|
---|
89 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
90 | import org.openstreetmap.josm.tools.Utils;
|
---|
91 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Class that helps PropertiesDialog add and edit tag values.
|
---|
95 | * @since 5633
|
---|
96 | */
|
---|
97 | public class TagEditHelper {
|
---|
98 |
|
---|
99 | private final JTable tagTable;
|
---|
100 | private final DefaultTableModel tagData;
|
---|
101 | private final Map<String, Map<String, Integer>> valueCount;
|
---|
102 |
|
---|
103 | // Selection that we are editing by using both dialogs
|
---|
104 | protected Collection<OsmPrimitive> sel;
|
---|
105 |
|
---|
106 | private String changedKey;
|
---|
107 | private String objKey;
|
---|
108 |
|
---|
109 | private final Comparator<AutoCompletionListItem> defaultACItemComparator =
|
---|
110 | (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
---|
111 |
|
---|
112 | /** Default number of recent tags */
|
---|
113 | public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
|
---|
114 | /** Maximum number of recent tags */
|
---|
115 | public static final int MAX_LRU_TAGS_NUMBER = 30;
|
---|
116 |
|
---|
117 | /** Use English language for tag by default */
|
---|
118 | public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false);
|
---|
119 | /** Whether recent tags must be remembered */
|
---|
120 | public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true);
|
---|
121 | /** Number of recent tags */
|
---|
122 | public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags",
|
---|
123 | DEFAULT_LRU_TAGS_NUMBER);
|
---|
124 | /** The preference storage of recent tags */
|
---|
125 | public static final CollectionProperty PROPERTY_RECENT_TAGS = new CollectionProperty("properties.recent-tags",
|
---|
126 | Collections.<String>emptyList());
|
---|
127 | public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
|
---|
128 | new SearchAction.SearchSetting().writeToString());
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * What to do with recent tags where keys already exist
|
---|
132 | */
|
---|
133 | private enum RecentExisting {
|
---|
134 | ENABLE,
|
---|
135 | DISABLE,
|
---|
136 | HIDE
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Preference setting for popup menu item "Recent tags with existing key"
|
---|
141 | */
|
---|
142 | public static final EnumProperty<RecentExisting> PROPERTY_RECENT_EXISTING = new EnumProperty<>(
|
---|
143 | "properties.recently-added-tags-existing-key", RecentExisting.class, RecentExisting.DISABLE);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * What to do after applying tag
|
---|
147 | */
|
---|
148 | private enum RefreshRecent {
|
---|
149 | NO,
|
---|
150 | STATUS,
|
---|
151 | REFRESH
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Preference setting for popup menu item "Refresh recent tags list after applying tag"
|
---|
156 | */
|
---|
157 | public static final EnumProperty<RefreshRecent> PROPERTY_REFRESH_RECENT = new EnumProperty<>(
|
---|
158 | "properties.refresh-recently-added-tags", RefreshRecent.class, RefreshRecent.STATUS);
|
---|
159 |
|
---|
160 | final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
|
---|
161 | SearchAction.SearchSetting tagsToIgnore;
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Copy of recently added tags in sorted from newest to oldest order.
|
---|
165 | *
|
---|
166 | * We store the maximum number of recent tags to allow dynamic change of number of tags shown in the preferences.
|
---|
167 | * Used to cache initial status.
|
---|
168 | */
|
---|
169 | private List<Tag> tags;
|
---|
170 |
|
---|
171 | static {
|
---|
172 | // init user input based on recent tags
|
---|
173 | final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
|
---|
174 | recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
|
---|
175 | recentTags.toList().forEach(tag -> AutoCompletionManager.rememberUserInput(tag.getKey(), tag.getValue(), false));
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Constructs a new {@code TagEditHelper}.
|
---|
180 | * @param tagTable tag table
|
---|
181 | * @param propertyData table model
|
---|
182 | * @param valueCount tag value count
|
---|
183 | */
|
---|
184 | public TagEditHelper(JTable tagTable, DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) {
|
---|
185 | this.tagTable = tagTable;
|
---|
186 | this.tagData = propertyData;
|
---|
187 | this.valueCount = valueCount;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Finds the key from given row of tag editor.
|
---|
192 | * @param viewRow index of row
|
---|
193 | * @return key of tag
|
---|
194 | */
|
---|
195 | public final String getDataKey(int viewRow) {
|
---|
196 | return tagData.getValueAt(tagTable.convertRowIndexToModel(viewRow), 0).toString();
|
---|
197 | }
|
---|
198 |
|
---|
199 | private boolean containsDataKey(String key) {
|
---|
200 | return IntStream.range(0, tagData.getRowCount())
|
---|
201 | .mapToObj(i -> tagData.getValueAt(i, 0) /* sic! do not use getDataKey*/)
|
---|
202 | .anyMatch(key::equals);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Finds the values from given row of tag editor.
|
---|
207 | * @param viewRow index of row
|
---|
208 | * @return map of values and number of occurrences
|
---|
209 | */
|
---|
210 | @SuppressWarnings("unchecked")
|
---|
211 | public final Map<String, Integer> getDataValues(int viewRow) {
|
---|
212 | return (Map<String, Integer>) tagData.getValueAt(tagTable.convertRowIndexToModel(viewRow), 1);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Open the add selection dialog and add a new key/value to the table (and
|
---|
217 | * to the dataset, of course).
|
---|
218 | */
|
---|
219 | public void addTag() {
|
---|
220 | changedKey = null;
|
---|
221 | sel = Main.main.getInProgressSelection();
|
---|
222 | if (sel == null || sel.isEmpty())
|
---|
223 | return;
|
---|
224 |
|
---|
225 | final AddTagsDialog addDialog = getAddTagsDialog();
|
---|
226 |
|
---|
227 | addDialog.showDialog();
|
---|
228 |
|
---|
229 | addDialog.destroyActions();
|
---|
230 | if (addDialog.getValue() == 1)
|
---|
231 | addDialog.performTagAdding();
|
---|
232 | else
|
---|
233 | addDialog.undoAllTagsAdding();
|
---|
234 | }
|
---|
235 |
|
---|
236 | protected AddTagsDialog getAddTagsDialog() {
|
---|
237 | return new AddTagsDialog();
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Edit the value in the tags table row.
|
---|
242 | * @param row The row of the table from which the value is edited.
|
---|
243 | * @param focusOnKey Determines if the initial focus should be set on key instead of value
|
---|
244 | * @since 5653
|
---|
245 | */
|
---|
246 | public void editTag(final int row, boolean focusOnKey) {
|
---|
247 | changedKey = null;
|
---|
248 | sel = Main.main.getInProgressSelection();
|
---|
249 | if (sel == null || sel.isEmpty())
|
---|
250 | return;
|
---|
251 |
|
---|
252 | String key = getDataKey(row);
|
---|
253 | objKey = key;
|
---|
254 |
|
---|
255 | final IEditTagDialog editDialog = getEditTagDialog(row, focusOnKey, key);
|
---|
256 | editDialog.showDialog();
|
---|
257 | if (editDialog.getValue() != 1)
|
---|
258 | return;
|
---|
259 | editDialog.performTagEdit();
|
---|
260 | }
|
---|
261 |
|
---|
262 | protected interface IEditTagDialog {
|
---|
263 | ExtendedDialog showDialog();
|
---|
264 |
|
---|
265 | int getValue();
|
---|
266 |
|
---|
267 | void performTagEdit();
|
---|
268 | }
|
---|
269 |
|
---|
270 | protected IEditTagDialog getEditTagDialog(int row, boolean focusOnKey, String key) {
|
---|
271 | return new EditTagDialog(key, getDataValues(row), focusOnKey);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * If during last editProperty call user changed the key name, this key will be returned
|
---|
276 | * Elsewhere, returns null.
|
---|
277 | * @return The modified key, or {@code null}
|
---|
278 | */
|
---|
279 | public String getChangedKey() {
|
---|
280 | return changedKey;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Reset last changed key.
|
---|
285 | */
|
---|
286 | public void resetChangedKey() {
|
---|
287 | changedKey = null;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * For a given key k, return a list of keys which are used as keys for
|
---|
292 | * auto-completing values to increase the search space.
|
---|
293 | * @param key the key k
|
---|
294 | * @return a list of keys
|
---|
295 | */
|
---|
296 | private static List<String> getAutocompletionKeys(String key) {
|
---|
297 | if ("name".equals(key) || "addr:street".equals(key))
|
---|
298 | return Arrays.asList("addr:street", "name");
|
---|
299 | else
|
---|
300 | return Arrays.asList(key);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Load recently used tags from preferences if needed.
|
---|
305 | */
|
---|
306 | public void loadTagsIfNeeded() {
|
---|
307 | loadTagsToIgnore();
|
---|
308 | if (PROPERTY_REMEMBER_TAGS.get() && recentTags.isEmpty()) {
|
---|
309 | recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | void loadTagsToIgnore() {
|
---|
314 | final SearchAction.SearchSetting searchSetting = Utils.firstNonNull(
|
---|
315 | SearchAction.SearchSetting.readFromString(PROPERTY_TAGS_TO_IGNORE.get()), new SearchAction.SearchSetting());
|
---|
316 | if (!Objects.equals(tagsToIgnore, searchSetting)) {
|
---|
317 | try {
|
---|
318 | tagsToIgnore = searchSetting;
|
---|
319 | recentTags.setTagsToIgnore(tagsToIgnore);
|
---|
320 | } catch (SearchCompiler.ParseError parseError) {
|
---|
321 | warnAboutParseError(parseError);
|
---|
322 | tagsToIgnore = new SearchAction.SearchSetting();
|
---|
323 | recentTags.setTagsToIgnore(SearchCompiler.Never.INSTANCE);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | private static void warnAboutParseError(SearchCompiler.ParseError parseError) {
|
---|
329 | Main.warn(parseError);
|
---|
330 | JOptionPane.showMessageDialog(
|
---|
331 | Main.parent,
|
---|
332 | parseError.getMessage(),
|
---|
333 | tr("Error"),
|
---|
334 | JOptionPane.ERROR_MESSAGE
|
---|
335 | );
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Store recently used tags in preferences if needed.
|
---|
340 | */
|
---|
341 | public void saveTagsIfNeeded() {
|
---|
342 | if (PROPERTY_REMEMBER_TAGS.get() && !recentTags.isEmpty()) {
|
---|
343 | recentTags.saveToPreference(PROPERTY_RECENT_TAGS);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Update cache of recent tags used for displaying tags.
|
---|
349 | */
|
---|
350 | private void cacheRecentTags() {
|
---|
351 | tags = recentTags.toList();
|
---|
352 | Collections.reverse(tags);
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Warns user about a key being overwritten.
|
---|
357 | * @param action The action done by the user. Must state what key is changed
|
---|
358 | * @param togglePref The preference to save the checkbox state to
|
---|
359 | * @return {@code true} if the user accepts to overwrite key, {@code false} otherwise
|
---|
360 | */
|
---|
361 | private static boolean warnOverwriteKey(String action, String togglePref) {
|
---|
362 | ExtendedDialog ed = new ExtendedDialog(
|
---|
363 | Main.parent,
|
---|
364 | tr("Overwrite key"),
|
---|
365 | new String[]{tr("Replace"), tr("Cancel")});
|
---|
366 | ed.setButtonIcons(new String[]{"purge", "cancel"});
|
---|
367 | ed.setContent(action+'\n'+ tr("The new key is already used, overwrite values?"));
|
---|
368 | ed.setCancelButton(2);
|
---|
369 | ed.toggleEnable(togglePref);
|
---|
370 | ed.showDialog();
|
---|
371 |
|
---|
372 | return ed.getValue() == 1;
|
---|
373 | }
|
---|
374 |
|
---|
375 | protected class EditTagDialog extends AbstractTagsDialog implements IEditTagDialog {
|
---|
376 | private final String key;
|
---|
377 | private final transient Map<String, Integer> m;
|
---|
378 | private final transient Comparator<AutoCompletionListItem> usedValuesAwareComparator;
|
---|
379 |
|
---|
380 | private final transient ListCellRenderer<AutoCompletionListItem> cellRenderer = new ListCellRenderer<AutoCompletionListItem>() {
|
---|
381 | private final DefaultListCellRenderer def = new DefaultListCellRenderer();
|
---|
382 | @Override
|
---|
383 | public Component getListCellRendererComponent(JList<? extends AutoCompletionListItem> list,
|
---|
384 | AutoCompletionListItem value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
385 | Component c = def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
---|
386 | if (c instanceof JLabel) {
|
---|
387 | String str = value.getValue();
|
---|
388 | if (valueCount.containsKey(objKey)) {
|
---|
389 | Map<String, Integer> map = valueCount.get(objKey);
|
---|
390 | if (map.containsKey(str)) {
|
---|
391 | str = tr("{0} ({1})", str, map.get(str));
|
---|
392 | c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD));
|
---|
393 | }
|
---|
394 | }
|
---|
395 | ((JLabel) c).setText(str);
|
---|
396 | }
|
---|
397 | return c;
|
---|
398 | }
|
---|
399 | };
|
---|
400 |
|
---|
401 | protected EditTagDialog(String key, Map<String, Integer> map, final boolean initialFocusOnKey) {
|
---|
402 | super(Main.parent, trn("Change value?", "Change values?", map.size()), new String[] {tr("OK"), tr("Cancel")});
|
---|
403 | setButtonIcons(new String[] {"ok", "cancel"});
|
---|
404 | setCancelButton(2);
|
---|
405 | configureContextsensitiveHelp("/Dialog/EditValue", true /* show help button */);
|
---|
406 | this.key = key;
|
---|
407 | this.m = map;
|
---|
408 |
|
---|
409 | usedValuesAwareComparator = (o1, o2) -> {
|
---|
410 | boolean c1 = m.containsKey(o1.getValue());
|
---|
411 | boolean c2 = m.containsKey(o2.getValue());
|
---|
412 | if (c1 == c2)
|
---|
413 | return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
---|
414 | else if (c1)
|
---|
415 | return -1;
|
---|
416 | else
|
---|
417 | return +1;
|
---|
418 | };
|
---|
419 |
|
---|
420 | JPanel mainPanel = new JPanel(new BorderLayout());
|
---|
421 |
|
---|
422 | String msg = "<html>"+trn("This will change {0} object.",
|
---|
423 | "This will change up to {0} objects.", sel.size(), sel.size())
|
---|
424 | +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
|
---|
425 |
|
---|
426 | mainPanel.add(new JLabel(msg), BorderLayout.NORTH);
|
---|
427 |
|
---|
428 | JPanel p = new JPanel(new GridBagLayout());
|
---|
429 | mainPanel.add(p, BorderLayout.CENTER);
|
---|
430 |
|
---|
431 | AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager();
|
---|
432 | List<AutoCompletionListItem> keyList = autocomplete.getKeys();
|
---|
433 | keyList.sort(defaultACItemComparator);
|
---|
434 |
|
---|
435 | keys = new AutoCompletingComboBox(key);
|
---|
436 | keys.setPossibleACItems(keyList);
|
---|
437 | keys.setEditable(true);
|
---|
438 | keys.setSelectedItem(key);
|
---|
439 |
|
---|
440 | p.add(Box.createVerticalStrut(5), GBC.eol());
|
---|
441 | p.add(new JLabel(tr("Key")), GBC.std());
|
---|
442 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
443 | p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
444 |
|
---|
445 | List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
|
---|
446 | valueList.sort(usedValuesAwareComparator);
|
---|
447 |
|
---|
448 | final String selection = m.size() != 1 ? tr("<different>") : m.entrySet().iterator().next().getKey();
|
---|
449 |
|
---|
450 | values = new AutoCompletingComboBox(selection);
|
---|
451 | values.setRenderer(cellRenderer);
|
---|
452 |
|
---|
453 | values.setEditable(true);
|
---|
454 | values.setPossibleACItems(valueList);
|
---|
455 | values.setSelectedItem(selection);
|
---|
456 | values.getEditor().setItem(selection);
|
---|
457 | p.add(Box.createVerticalStrut(5), GBC.eol());
|
---|
458 | p.add(new JLabel(tr("Value")), GBC.std());
|
---|
459 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
460 | p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
461 | values.getEditor().addActionListener(e -> buttonAction(0, null));
|
---|
462 | addFocusAdapter(autocomplete, usedValuesAwareComparator);
|
---|
463 |
|
---|
464 | setContent(mainPanel, false);
|
---|
465 |
|
---|
466 | addWindowListener(new WindowAdapter() {
|
---|
467 | @Override
|
---|
468 | public void windowOpened(WindowEvent e) {
|
---|
469 | if (initialFocusOnKey) {
|
---|
470 | selectKeysComboBox();
|
---|
471 | } else {
|
---|
472 | selectValuesCombobox();
|
---|
473 | }
|
---|
474 | }
|
---|
475 | });
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Edit tags of multiple selected objects according to selected ComboBox values
|
---|
480 | * If value == "", tag will be deleted
|
---|
481 | * Confirmations may be needed.
|
---|
482 | */
|
---|
483 | @Override
|
---|
484 | public void performTagEdit() {
|
---|
485 | String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
|
---|
486 | value = Normalizer.normalize(value, Normalizer.Form.NFC);
|
---|
487 | if (value.isEmpty()) {
|
---|
488 | value = null; // delete the key
|
---|
489 | }
|
---|
490 | String newkey = Tag.removeWhiteSpaces(keys.getEditor().getItem().toString());
|
---|
491 | newkey = Normalizer.normalize(newkey, Normalizer.Form.NFC);
|
---|
492 | if (newkey.isEmpty()) {
|
---|
493 | newkey = key;
|
---|
494 | value = null; // delete the key instead
|
---|
495 | }
|
---|
496 | if (key.equals(newkey) && tr("<different>").equals(value))
|
---|
497 | return;
|
---|
498 | if (key.equals(newkey) || value == null) {
|
---|
499 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
500 | AutoCompletionManager.rememberUserInput(newkey, value, true);
|
---|
501 | } else {
|
---|
502 | for (OsmPrimitive osm: sel) {
|
---|
503 | if (osm.get(newkey) != null) {
|
---|
504 | if (!warnOverwriteKey(tr("You changed the key from ''{0}'' to ''{1}''.", key, newkey),
|
---|
505 | "overwriteEditKey"))
|
---|
506 | return;
|
---|
507 | break;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | Collection<Command> commands = new ArrayList<>();
|
---|
511 | commands.add(new ChangePropertyCommand(sel, key, null));
|
---|
512 | if (value.equals(tr("<different>"))) {
|
---|
513 | Map<String, List<OsmPrimitive>> map = new HashMap<>();
|
---|
514 | for (OsmPrimitive osm: sel) {
|
---|
515 | String val = osm.get(key);
|
---|
516 | if (val != null) {
|
---|
517 | if (map.containsKey(val)) {
|
---|
518 | map.get(val).add(osm);
|
---|
519 | } else {
|
---|
520 | List<OsmPrimitive> v = new ArrayList<>();
|
---|
521 | v.add(osm);
|
---|
522 | map.put(val, v);
|
---|
523 | }
|
---|
524 | }
|
---|
525 | }
|
---|
526 | for (Map.Entry<String, List<OsmPrimitive>> e: map.entrySet()) {
|
---|
527 | commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
|
---|
528 | }
|
---|
529 | } else {
|
---|
530 | commands.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
531 | AutoCompletionManager.rememberUserInput(newkey, value, false);
|
---|
532 | }
|
---|
533 | Main.main.undoRedo.add(new SequenceCommand(
|
---|
534 | trn("Change properties of up to {0} object",
|
---|
535 | "Change properties of up to {0} objects", sel.size(), sel.size()),
|
---|
536 | commands));
|
---|
537 | }
|
---|
538 |
|
---|
539 | changedKey = newkey;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | protected abstract class AbstractTagsDialog extends ExtendedDialog {
|
---|
544 | protected AutoCompletingComboBox keys;
|
---|
545 | protected AutoCompletingComboBox values;
|
---|
546 |
|
---|
547 | AbstractTagsDialog(Component parent, String title, String ... buttonTexts) {
|
---|
548 | super(parent, title, buttonTexts);
|
---|
549 | addMouseListener(new PopupMenuLauncher(popupMenu));
|
---|
550 | }
|
---|
551 |
|
---|
552 | @Override
|
---|
553 | public void setupDialog() {
|
---|
554 | super.setupDialog();
|
---|
555 | final Dimension size = getSize();
|
---|
556 | // Set resizable only in width
|
---|
557 | setMinimumSize(size);
|
---|
558 | setPreferredSize(size);
|
---|
559 | // setMaximumSize does not work, and never worked, but still it seems not to bother Oracle to fix this 10-year-old bug
|
---|
560 | // https://bugs.openjdk.java.net/browse/JDK-6200438
|
---|
561 | // https://bugs.openjdk.java.net/browse/JDK-6464548
|
---|
562 |
|
---|
563 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
564 | WindowGeometry.centerInWindow(Main.parent, size));
|
---|
565 | }
|
---|
566 |
|
---|
567 | @Override
|
---|
568 | public void setVisible(boolean visible) {
|
---|
569 | // Do not want dialog to be resizable in height, as its size may increase each time because of the recently added tags
|
---|
570 | // So need to modify the stored geometry (size part only) in order to use the automatic positioning mechanism
|
---|
571 | if (visible) {
|
---|
572 | WindowGeometry geometry = initWindowGeometry();
|
---|
573 | Dimension storedSize = geometry.getSize();
|
---|
574 | Dimension size = getSize();
|
---|
575 | if (!storedSize.equals(size)) {
|
---|
576 | if (storedSize.width < size.width) {
|
---|
577 | storedSize.width = size.width;
|
---|
578 | }
|
---|
579 | if (storedSize.height != size.height) {
|
---|
580 | storedSize.height = size.height;
|
---|
581 | }
|
---|
582 | rememberWindowGeometry(geometry);
|
---|
583 | }
|
---|
584 | keys.setFixedLocale(PROPERTY_FIX_TAG_LOCALE.get());
|
---|
585 | }
|
---|
586 | super.setVisible(visible);
|
---|
587 | }
|
---|
588 |
|
---|
589 | private void selectACComboBoxSavingUnixBuffer(AutoCompletingComboBox cb) {
|
---|
590 | // select combobox with saving unix system selection (middle mouse paste)
|
---|
591 | Clipboard sysSel = ClipboardUtils.getSystemSelection();
|
---|
592 | if (sysSel != null) {
|
---|
593 | Transferable old = ClipboardUtils.getClipboardContent(sysSel);
|
---|
594 | cb.requestFocusInWindow();
|
---|
595 | cb.getEditor().selectAll();
|
---|
596 | if (old != null) {
|
---|
597 | sysSel.setContents(old, null);
|
---|
598 | }
|
---|
599 | } else {
|
---|
600 | cb.requestFocusInWindow();
|
---|
601 | cb.getEditor().selectAll();
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | public void selectKeysComboBox() {
|
---|
606 | selectACComboBoxSavingUnixBuffer(keys);
|
---|
607 | }
|
---|
608 |
|
---|
609 | public void selectValuesCombobox() {
|
---|
610 | selectACComboBoxSavingUnixBuffer(values);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Create a focus handling adapter and apply in to the editor component of value
|
---|
615 | * autocompletion box.
|
---|
616 | * @param autocomplete Manager handling the autocompletion
|
---|
617 | * @param comparator Class to decide what values are offered on autocompletion
|
---|
618 | * @return The created adapter
|
---|
619 | */
|
---|
620 | protected FocusAdapter addFocusAdapter(final AutoCompletionManager autocomplete, final Comparator<AutoCompletionListItem> comparator) {
|
---|
621 | // get the combo box' editor component
|
---|
622 | final JTextComponent editor = values.getEditorComponent();
|
---|
623 | // Refresh the values model when focus is gained
|
---|
624 | FocusAdapter focus = new FocusAdapter() {
|
---|
625 | @Override
|
---|
626 | public void focusGained(FocusEvent e) {
|
---|
627 | String key = keys.getEditor().getItem().toString();
|
---|
628 |
|
---|
629 | List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
|
---|
630 | valueList.sort(comparator);
|
---|
631 | if (Main.isTraceEnabled()) {
|
---|
632 | Main.trace("Focus gained by {0}, e={1}", values, e);
|
---|
633 | }
|
---|
634 | values.setPossibleACItems(valueList);
|
---|
635 | values.getEditor().selectAll();
|
---|
636 | objKey = key;
|
---|
637 | }
|
---|
638 | };
|
---|
639 | editor.addFocusListener(focus);
|
---|
640 | return focus;
|
---|
641 | }
|
---|
642 |
|
---|
643 | protected JPopupMenu popupMenu = new JPopupMenu() {
|
---|
644 | private final JCheckBoxMenuItem fixTagLanguageCb = new JCheckBoxMenuItem(
|
---|
645 | new AbstractAction(tr("Use English language for tag by default")) {
|
---|
646 | @Override
|
---|
647 | public void actionPerformed(ActionEvent e) {
|
---|
648 | boolean use = ((JCheckBoxMenuItem) e.getSource()).getState();
|
---|
649 | PROPERTY_FIX_TAG_LOCALE.put(use);
|
---|
650 | keys.setFixedLocale(use);
|
---|
651 | }
|
---|
652 | });
|
---|
653 | {
|
---|
654 | add(fixTagLanguageCb);
|
---|
655 | fixTagLanguageCb.setState(PROPERTY_FIX_TAG_LOCALE.get());
|
---|
656 | }
|
---|
657 | };
|
---|
658 | }
|
---|
659 |
|
---|
660 | protected class AddTagsDialog extends AbstractTagsDialog {
|
---|
661 | private final List<JosmAction> recentTagsActions = new ArrayList<>();
|
---|
662 | protected final transient FocusAdapter focus;
|
---|
663 | private final JPanel mainPanel;
|
---|
664 | private JPanel recentTagsPanel;
|
---|
665 |
|
---|
666 | // Counter of added commands for possible undo
|
---|
667 | private int commandCount;
|
---|
668 |
|
---|
669 | protected AddTagsDialog() {
|
---|
670 | super(Main.parent, tr("Add value?"), new String[] {tr("OK"), tr("Cancel")});
|
---|
671 | setButtonIcons(new String[] {"ok", "cancel"});
|
---|
672 | setCancelButton(2);
|
---|
673 | configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */);
|
---|
674 |
|
---|
675 | mainPanel = new JPanel(new GridBagLayout());
|
---|
676 | keys = new AutoCompletingComboBox();
|
---|
677 | values = new AutoCompletingComboBox();
|
---|
678 |
|
---|
679 | mainPanel.add(new JLabel("<html>"+trn("This will change up to {0} object.",
|
---|
680 | "This will change up to {0} objects.", sel.size(), sel.size())
|
---|
681 | +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
682 |
|
---|
683 | cacheRecentTags();
|
---|
684 | AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager();
|
---|
685 | List<AutoCompletionListItem> keyList = autocomplete.getKeys();
|
---|
686 |
|
---|
687 | // remove the object's tag keys from the list
|
---|
688 | keyList.removeIf(item -> containsDataKey(item.getValue()));
|
---|
689 |
|
---|
690 | keyList.sort(defaultACItemComparator);
|
---|
691 | keys.setPossibleACItems(keyList);
|
---|
692 | keys.setEditable(true);
|
---|
693 |
|
---|
694 | mainPanel.add(keys, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
695 |
|
---|
696 | mainPanel.add(new JLabel(tr("Please select a value")), GBC.eol());
|
---|
697 | values.setEditable(true);
|
---|
698 | mainPanel.add(values, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
699 |
|
---|
700 | // pre-fill first recent tag for which the key is not already present
|
---|
701 | tags.stream()
|
---|
702 | .filter(tag -> !containsDataKey(tag.getKey()))
|
---|
703 | .findFirst()
|
---|
704 | .ifPresent(tag -> {
|
---|
705 | keys.setSelectedItem(tag.getKey());
|
---|
706 | values.setSelectedItem(tag.getValue());
|
---|
707 | });
|
---|
708 |
|
---|
709 | focus = addFocusAdapter(autocomplete, defaultACItemComparator);
|
---|
710 | // fire focus event in advance or otherwise the popup list will be too small at first
|
---|
711 | focus.focusGained(null);
|
---|
712 |
|
---|
713 | // Add tag on Shift-Enter
|
---|
714 | mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
---|
715 | KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK), "addAndContinue");
|
---|
716 | mainPanel.getActionMap().put("addAndContinue", new AbstractAction() {
|
---|
717 | @Override
|
---|
718 | public void actionPerformed(ActionEvent e) {
|
---|
719 | performTagAdding();
|
---|
720 | refreshRecentTags();
|
---|
721 | selectKeysComboBox();
|
---|
722 | }
|
---|
723 | });
|
---|
724 |
|
---|
725 | suggestRecentlyAddedTags();
|
---|
726 |
|
---|
727 | mainPanel.add(Box.createVerticalGlue(), GBC.eop().fill());
|
---|
728 | setContent(mainPanel, false);
|
---|
729 |
|
---|
730 | selectKeysComboBox();
|
---|
731 |
|
---|
732 | popupMenu.add(new AbstractAction(tr("Set number of recently added tags")) {
|
---|
733 | @Override
|
---|
734 | public void actionPerformed(ActionEvent e) {
|
---|
735 | selectNumberOfTags();
|
---|
736 | suggestRecentlyAddedTags();
|
---|
737 | }
|
---|
738 | });
|
---|
739 |
|
---|
740 | popupMenu.add(buildMenuRecentExisting());
|
---|
741 | popupMenu.add(buildMenuRefreshRecent());
|
---|
742 |
|
---|
743 | JCheckBoxMenuItem rememberLastTags = new JCheckBoxMenuItem(
|
---|
744 | new AbstractAction(tr("Remember last used tags after a restart")) {
|
---|
745 | @Override
|
---|
746 | public void actionPerformed(ActionEvent e) {
|
---|
747 | boolean state = ((JCheckBoxMenuItem) e.getSource()).getState();
|
---|
748 | PROPERTY_REMEMBER_TAGS.put(state);
|
---|
749 | if (state)
|
---|
750 | saveTagsIfNeeded();
|
---|
751 | }
|
---|
752 | });
|
---|
753 | rememberLastTags.setState(PROPERTY_REMEMBER_TAGS.get());
|
---|
754 | popupMenu.add(rememberLastTags);
|
---|
755 | }
|
---|
756 |
|
---|
757 | private JMenu buildMenuRecentExisting() {
|
---|
758 | JMenu menu = new JMenu(tr("Recent tags with existing key"));
|
---|
759 | TreeMap<RecentExisting, String> radios = new TreeMap<>();
|
---|
760 | radios.put(RecentExisting.ENABLE, tr("Enable"));
|
---|
761 | radios.put(RecentExisting.DISABLE, tr("Disable"));
|
---|
762 | radios.put(RecentExisting.HIDE, tr("Hide"));
|
---|
763 | ButtonGroup buttonGroup = new ButtonGroup();
|
---|
764 | for (final Map.Entry<RecentExisting, String> entry : radios.entrySet()) {
|
---|
765 | JRadioButtonMenuItem radio = new JRadioButtonMenuItem(new AbstractAction(entry.getValue()) {
|
---|
766 | @Override
|
---|
767 | public void actionPerformed(ActionEvent e) {
|
---|
768 | PROPERTY_RECENT_EXISTING.put(entry.getKey());
|
---|
769 | suggestRecentlyAddedTags();
|
---|
770 | }
|
---|
771 | });
|
---|
772 | buttonGroup.add(radio);
|
---|
773 | radio.setSelected(PROPERTY_RECENT_EXISTING.get() == entry.getKey());
|
---|
774 | menu.add(radio);
|
---|
775 | }
|
---|
776 | return menu;
|
---|
777 | }
|
---|
778 |
|
---|
779 | private JMenu buildMenuRefreshRecent() {
|
---|
780 | JMenu menu = new JMenu(tr("Refresh recent tags list after applying tag"));
|
---|
781 | TreeMap<RefreshRecent, String> radios = new TreeMap<>();
|
---|
782 | radios.put(RefreshRecent.NO, tr("No refresh"));
|
---|
783 | radios.put(RefreshRecent.STATUS, tr("Refresh tag status only (enabled / disabled)"));
|
---|
784 | radios.put(RefreshRecent.REFRESH, tr("Refresh tag status and list of recently added tags"));
|
---|
785 | ButtonGroup buttonGroup = new ButtonGroup();
|
---|
786 | for (final Map.Entry<RefreshRecent, String> entry : radios.entrySet()) {
|
---|
787 | JRadioButtonMenuItem radio = new JRadioButtonMenuItem(new AbstractAction(entry.getValue()) {
|
---|
788 | @Override
|
---|
789 | public void actionPerformed(ActionEvent e) {
|
---|
790 | PROPERTY_REFRESH_RECENT.put(entry.getKey());
|
---|
791 | }
|
---|
792 | });
|
---|
793 | buttonGroup.add(radio);
|
---|
794 | radio.setSelected(PROPERTY_REFRESH_RECENT.get() == entry.getKey());
|
---|
795 | menu.add(radio);
|
---|
796 | }
|
---|
797 | return menu;
|
---|
798 | }
|
---|
799 |
|
---|
800 | @Override
|
---|
801 | public void setContentPane(Container contentPane) {
|
---|
802 | final int commandDownMask = GuiHelper.getMenuShortcutKeyMaskEx();
|
---|
803 | List<String> lines = new ArrayList<>();
|
---|
804 | Shortcut.findShortcut(KeyEvent.VK_1, commandDownMask).ifPresent(sc ->
|
---|
805 | lines.add(sc.getKeyText() + ' ' + tr("to apply first suggestion"))
|
---|
806 | );
|
---|
807 | lines.add(KeyEvent.getKeyModifiersText(KeyEvent.SHIFT_MASK)+'+'+KeyEvent.getKeyText(KeyEvent.VK_ENTER) + ' '
|
---|
808 | +tr("to add without closing the dialog"));
|
---|
809 | Shortcut.findShortcut(KeyEvent.VK_1, commandDownMask | KeyEvent.SHIFT_DOWN_MASK).ifPresent(sc ->
|
---|
810 | lines.add(sc.getKeyText() + ' ' + tr("to add first suggestion without closing the dialog"))
|
---|
811 | );
|
---|
812 | final JLabel helpLabel = new JLabel("<html>" + Utils.join("<br>", lines) + "</html>");
|
---|
813 | helpLabel.setFont(helpLabel.getFont().deriveFont(Font.PLAIN));
|
---|
814 | contentPane.add(helpLabel, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(5, 5, 5, 5));
|
---|
815 | super.setContentPane(contentPane);
|
---|
816 | }
|
---|
817 |
|
---|
818 | protected void selectNumberOfTags() {
|
---|
819 | String s = String.format("%d", PROPERTY_RECENT_TAGS_NUMBER.get());
|
---|
820 | while (true) {
|
---|
821 | s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display"), s);
|
---|
822 | if (s == null || s.isEmpty()) {
|
---|
823 | return;
|
---|
824 | }
|
---|
825 | try {
|
---|
826 | int v = Integer.parseInt(s);
|
---|
827 | if (v >= 0 && v <= MAX_LRU_TAGS_NUMBER) {
|
---|
828 | PROPERTY_RECENT_TAGS_NUMBER.put(v);
|
---|
829 | return;
|
---|
830 | }
|
---|
831 | } catch (NumberFormatException ex) {
|
---|
832 | Main.warn(ex);
|
---|
833 | }
|
---|
834 | JOptionPane.showMessageDialog(this, tr("Please enter integer number between 0 and {0}", MAX_LRU_TAGS_NUMBER));
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | protected void suggestRecentlyAddedTags() {
|
---|
839 | if (recentTagsPanel == null) {
|
---|
840 | recentTagsPanel = new JPanel(new GridBagLayout());
|
---|
841 | buildRecentTagsPanel();
|
---|
842 | mainPanel.add(recentTagsPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
843 | } else {
|
---|
844 | Dimension panelOldSize = recentTagsPanel.getPreferredSize();
|
---|
845 | recentTagsPanel.removeAll();
|
---|
846 | buildRecentTagsPanel();
|
---|
847 | Dimension panelNewSize = recentTagsPanel.getPreferredSize();
|
---|
848 | Dimension dialogOldSize = getMinimumSize();
|
---|
849 | Dimension dialogNewSize = new Dimension(dialogOldSize.width, dialogOldSize.height-panelOldSize.height+panelNewSize.height);
|
---|
850 | setMinimumSize(dialogNewSize);
|
---|
851 | setPreferredSize(dialogNewSize);
|
---|
852 | setSize(dialogNewSize);
|
---|
853 | revalidate();
|
---|
854 | repaint();
|
---|
855 | }
|
---|
856 | }
|
---|
857 |
|
---|
858 | protected void buildRecentTagsPanel() {
|
---|
859 | final int tagsToShow = Math.min(PROPERTY_RECENT_TAGS_NUMBER.get(), MAX_LRU_TAGS_NUMBER);
|
---|
860 | if (!(tagsToShow > 0 && !recentTags.isEmpty()))
|
---|
861 | return;
|
---|
862 | recentTagsPanel.add(new JLabel(tr("Recently added tags")), GBC.eol());
|
---|
863 |
|
---|
864 | int count = 0;
|
---|
865 | destroyActions();
|
---|
866 | for (int i = 0; i < tags.size() && count < tagsToShow; i++) {
|
---|
867 | final Tag t = tags.get(i);
|
---|
868 | boolean keyExists = keyExists(t);
|
---|
869 | if (keyExists && PROPERTY_RECENT_EXISTING.get() == RecentExisting.HIDE)
|
---|
870 | continue;
|
---|
871 | count++;
|
---|
872 | // Create action for reusing the tag, with keyboard shortcut
|
---|
873 | /* POSSIBLE SHORTCUTS: 1,2,3,4,5,6,7,8,9,0=10 */
|
---|
874 | final Shortcut sc = count > 10 ? null : Shortcut.registerShortcut("properties:recent:" + count,
|
---|
875 | tr("Choose recent tag {0}", count), KeyEvent.VK_0 + (count % 10), Shortcut.CTRL);
|
---|
876 | final JosmAction action = new JosmAction(
|
---|
877 | tr("Choose recent tag {0}", count), null, tr("Use this tag again"), sc, false) {
|
---|
878 | @Override
|
---|
879 | public void actionPerformed(ActionEvent e) {
|
---|
880 | keys.setSelectedItem(t.getKey());
|
---|
881 | // fix #7951, #8298 - update list of values before setting value (?)
|
---|
882 | focus.focusGained(null);
|
---|
883 | values.setSelectedItem(t.getValue());
|
---|
884 | selectValuesCombobox();
|
---|
885 | }
|
---|
886 | };
|
---|
887 | /* POSSIBLE SHORTCUTS: 1,2,3,4,5,6,7,8,9,0=10 */
|
---|
888 | final Shortcut scShift = count > 10 ? null : Shortcut.registerShortcut("properties:recent:apply:" + count,
|
---|
889 | tr("Apply recent tag {0}", count), KeyEvent.VK_0 + (count % 10), Shortcut.CTRL_SHIFT);
|
---|
890 | final JosmAction actionShift = new JosmAction(
|
---|
891 | tr("Apply recent tag {0}", count), null, tr("Use this tag again"), scShift, false) {
|
---|
892 | @Override
|
---|
893 | public void actionPerformed(ActionEvent e) {
|
---|
894 | action.actionPerformed(null);
|
---|
895 | performTagAdding();
|
---|
896 | refreshRecentTags();
|
---|
897 | selectKeysComboBox();
|
---|
898 | }
|
---|
899 | };
|
---|
900 | recentTagsActions.add(action);
|
---|
901 | recentTagsActions.add(actionShift);
|
---|
902 | if (keyExists && PROPERTY_RECENT_EXISTING.get() == RecentExisting.DISABLE) {
|
---|
903 | action.setEnabled(false);
|
---|
904 | }
|
---|
905 | // Find and display icon
|
---|
906 | ImageIcon icon = MapPaintStyles.getNodeIcon(t, false); // Filters deprecated icon
|
---|
907 | if (icon == null) {
|
---|
908 | // If no icon found in map style look at presets
|
---|
909 | Map<String, String> map = new HashMap<>();
|
---|
910 | map.put(t.getKey(), t.getValue());
|
---|
911 | for (TaggingPreset tp : TaggingPresets.getMatchingPresets(null, map, false)) {
|
---|
912 | icon = tp.getIcon();
|
---|
913 | if (icon != null) {
|
---|
914 | break;
|
---|
915 | }
|
---|
916 | }
|
---|
917 | // If still nothing display an empty icon
|
---|
918 | if (icon == null) {
|
---|
919 | icon = new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
|
---|
920 | }
|
---|
921 | }
|
---|
922 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
923 | gbc.ipadx = 5;
|
---|
924 | recentTagsPanel.add(new JLabel(action.isEnabled() ? icon : GuiHelper.getDisabledIcon(icon)), gbc);
|
---|
925 | // Create tag label
|
---|
926 | final String color = action.isEnabled() ? "" : "; color:gray";
|
---|
927 | final JLabel tagLabel = new JLabel("<html>"
|
---|
928 | + "<style>td{" + color + "}</style>"
|
---|
929 | + "<table><tr>"
|
---|
930 | + "<td>" + count + ".</td>"
|
---|
931 | + "<td style='border:1px solid gray'>" + XmlWriter.encode(t.toString(), true) + '<' +
|
---|
932 | "/td></tr></table></html>");
|
---|
933 | tagLabel.setFont(tagLabel.getFont().deriveFont(Font.PLAIN));
|
---|
934 | if (action.isEnabled() && sc != null && scShift != null) {
|
---|
935 | // Register action
|
---|
936 | recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "choose"+count);
|
---|
937 | recentTagsPanel.getActionMap().put("choose"+count, action);
|
---|
938 | recentTagsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scShift.getKeyStroke(), "apply"+count);
|
---|
939 | recentTagsPanel.getActionMap().put("apply"+count, actionShift);
|
---|
940 | }
|
---|
941 | if (action.isEnabled()) {
|
---|
942 | // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
|
---|
943 | tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
|
---|
944 | tagLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
945 | tagLabel.addMouseListener(new MouseAdapter() {
|
---|
946 | @Override
|
---|
947 | public void mouseClicked(MouseEvent e) {
|
---|
948 | action.actionPerformed(null);
|
---|
949 | if (SwingUtilities.isRightMouseButton(e)) {
|
---|
950 | new TagPopupMenu(t).show(e.getComponent(), e.getX(), e.getY());
|
---|
951 | } else if (e.isShiftDown()) {
|
---|
952 | // add tags on Shift-Click
|
---|
953 | performTagAdding();
|
---|
954 | refreshRecentTags();
|
---|
955 | selectKeysComboBox();
|
---|
956 | } else if (e.getClickCount() > 1) {
|
---|
957 | // add tags and close window on double-click
|
---|
958 | buttonAction(0, null); // emulate OK click and close the dialog
|
---|
959 | }
|
---|
960 | }
|
---|
961 | });
|
---|
962 | } else {
|
---|
963 | // Disable tag label
|
---|
964 | tagLabel.setEnabled(false);
|
---|
965 | // Explain in the tooltip why
|
---|
966 | tagLabel.setToolTipText(tr("The key ''{0}'' is already used", t.getKey()));
|
---|
967 | }
|
---|
968 | // Finally add label to the resulting panel
|
---|
969 | JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
---|
970 | tagPanel.add(tagLabel);
|
---|
971 | recentTagsPanel.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
972 | }
|
---|
973 | // Clear label if no tags were added
|
---|
974 | if (count == 0) {
|
---|
975 | recentTagsPanel.removeAll();
|
---|
976 | }
|
---|
977 | }
|
---|
978 |
|
---|
979 | class TagPopupMenu extends JPopupMenu {
|
---|
980 |
|
---|
981 | TagPopupMenu(Tag t) {
|
---|
982 | add(new IgnoreTagAction(tr("Ignore key ''{0}''", t.getKey()), new Tag(t.getKey(), "")));
|
---|
983 | add(new IgnoreTagAction(tr("Ignore tag ''{0}''", t), t));
|
---|
984 | add(new EditIgnoreTagsAction());
|
---|
985 | }
|
---|
986 | }
|
---|
987 |
|
---|
988 | class IgnoreTagAction extends AbstractAction {
|
---|
989 | final transient Tag tag;
|
---|
990 |
|
---|
991 | IgnoreTagAction(String name, Tag tag) {
|
---|
992 | super(name);
|
---|
993 | this.tag = tag;
|
---|
994 | }
|
---|
995 |
|
---|
996 | @Override
|
---|
997 | public void actionPerformed(ActionEvent e) {
|
---|
998 | try {
|
---|
999 | if (tagsToIgnore != null) {
|
---|
1000 | recentTags.ignoreTag(tag, tagsToIgnore);
|
---|
1001 | PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
|
---|
1002 | }
|
---|
1003 | } catch (SearchCompiler.ParseError parseError) {
|
---|
1004 | throw new IllegalStateException(parseError);
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | class EditIgnoreTagsAction extends AbstractAction {
|
---|
1010 |
|
---|
1011 | EditIgnoreTagsAction() {
|
---|
1012 | super(tr("Edit ignore list"));
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | @Override
|
---|
1016 | public void actionPerformed(ActionEvent e) {
|
---|
1017 | final SearchAction.SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
|
---|
1018 | if (newTagsToIngore == null) {
|
---|
1019 | return;
|
---|
1020 | }
|
---|
1021 | try {
|
---|
1022 | tagsToIgnore = newTagsToIngore;
|
---|
1023 | recentTags.setTagsToIgnore(tagsToIgnore);
|
---|
1024 | PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
|
---|
1025 | } catch (SearchCompiler.ParseError parseError) {
|
---|
1026 | warnAboutParseError(parseError);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | /**
|
---|
1032 | * Destroy the recentTagsActions.
|
---|
1033 | */
|
---|
1034 | public void destroyActions() {
|
---|
1035 | for (JosmAction action : recentTagsActions) {
|
---|
1036 | action.destroy();
|
---|
1037 | }
|
---|
1038 | recentTagsActions.clear();
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | /**
|
---|
1042 | * Read tags from comboboxes and add it to all selected objects
|
---|
1043 | */
|
---|
1044 | public final void performTagAdding() {
|
---|
1045 | String key = Tag.removeWhiteSpaces(keys.getEditor().getItem().toString());
|
---|
1046 | String value = Tag.removeWhiteSpaces(values.getEditor().getItem().toString());
|
---|
1047 | if (key.isEmpty() || value.isEmpty())
|
---|
1048 | return;
|
---|
1049 | for (OsmPrimitive osm : sel) {
|
---|
1050 | String val = osm.get(key);
|
---|
1051 | if (val != null && !val.equals(value)) {
|
---|
1052 | if (!warnOverwriteKey(tr("You changed the value of ''{0}'' from ''{1}'' to ''{2}''.", key, val, value),
|
---|
1053 | "overwriteAddKey"))
|
---|
1054 | return;
|
---|
1055 | break;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 | recentTags.add(new Tag(key, value));
|
---|
1059 | valueCount.put(key, new TreeMap<String, Integer>());
|
---|
1060 | AutoCompletionManager.rememberUserInput(key, value, false);
|
---|
1061 | commandCount++;
|
---|
1062 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
|
---|
1063 | changedKey = key;
|
---|
1064 | clearEntries();
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | protected void clearEntries() {
|
---|
1068 | keys.getEditor().setItem("");
|
---|
1069 | values.getEditor().setItem("");
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | public void undoAllTagsAdding() {
|
---|
1073 | Main.main.undoRedo.undo(commandCount);
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | private boolean keyExists(final Tag t) {
|
---|
1077 | return valueCount.containsKey(t.getKey());
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | private void refreshRecentTags() {
|
---|
1081 | switch (PROPERTY_REFRESH_RECENT.get()) {
|
---|
1082 | case REFRESH: cacheRecentTags(); // break missing intentionally
|
---|
1083 | case STATUS: suggestRecentlyAddedTags(); break;
|
---|
1084 | default: // Do nothing
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 | }
|
---|