1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.io.File;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.EnumSet;
|
---|
18 | import java.util.HashSet;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Set;
|
---|
23 | import java.util.function.Predicate;
|
---|
24 |
|
---|
25 | import javax.swing.AbstractAction;
|
---|
26 | import javax.swing.Action;
|
---|
27 | import javax.swing.ImageIcon;
|
---|
28 | import javax.swing.JLabel;
|
---|
29 | import javax.swing.JOptionPane;
|
---|
30 | import javax.swing.JPanel;
|
---|
31 | import javax.swing.JToggleButton;
|
---|
32 | import javax.swing.SwingUtilities;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.actions.AdaptableAction;
|
---|
36 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
37 | import org.openstreetmap.josm.command.Command;
|
---|
38 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
39 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
40 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
41 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
42 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
43 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
44 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
45 | import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
|
---|
46 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
47 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
48 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
49 | import org.openstreetmap.josm.gui.Notification;
|
---|
50 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
51 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
52 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
53 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
54 | import org.openstreetmap.josm.gui.tagging.presets.items.Key;
|
---|
55 | import org.openstreetmap.josm.gui.tagging.presets.items.Link;
|
---|
56 | import org.openstreetmap.josm.gui.tagging.presets.items.Optional;
|
---|
57 | import org.openstreetmap.josm.gui.tagging.presets.items.PresetLink;
|
---|
58 | import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
|
---|
59 | import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
|
---|
60 | import org.openstreetmap.josm.gui.tagging.presets.items.Space;
|
---|
61 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
62 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
63 | import org.openstreetmap.josm.tools.GBC;
|
---|
64 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
65 | import org.openstreetmap.josm.tools.Logging;
|
---|
66 | import org.openstreetmap.josm.tools.Utils;
|
---|
67 | import org.openstreetmap.josm.tools.template_engine.ParseError;
|
---|
68 | import org.openstreetmap.josm.tools.template_engine.TemplateEntry;
|
---|
69 | import org.openstreetmap.josm.tools.template_engine.TemplateParser;
|
---|
70 | import org.xml.sax.SAXException;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * This class read encapsulate one tagging preset. A class method can
|
---|
74 | * read in all predefined presets, either shipped with JOSM or that are
|
---|
75 | * in the config directory.
|
---|
76 | *
|
---|
77 | * It is also able to construct dialogs out of preset definitions.
|
---|
78 | * @since 294
|
---|
79 | */
|
---|
80 | public class TaggingPreset extends AbstractAction implements ActiveLayerChangeListener, AdaptableAction, Predicate<OsmPrimitive> {
|
---|
81 |
|
---|
82 | public static final int DIALOG_ANSWER_APPLY = 1;
|
---|
83 | public static final int DIALOG_ANSWER_NEW_RELATION = 2;
|
---|
84 | public static final int DIALOG_ANSWER_CANCEL = 3;
|
---|
85 |
|
---|
86 | public static final String OPTIONAL_TOOLTIP_TEXT = "Optional tooltip text";
|
---|
87 |
|
---|
88 | /** Prefix of preset icon loading failure error message */
|
---|
89 | public static final String PRESET_ICON_ERROR_MSG_PREFIX = "Could not get presets icon ";
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * The preset group this preset belongs to.
|
---|
93 | */
|
---|
94 | public TaggingPresetMenu group;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * The name of the tagging preset.
|
---|
98 | * @see #getRawName()
|
---|
99 | */
|
---|
100 | public String name;
|
---|
101 | /**
|
---|
102 | * The icon name assigned to this preset.
|
---|
103 | */
|
---|
104 | public String iconName;
|
---|
105 | public String name_context;
|
---|
106 | /**
|
---|
107 | * A cache for the local name. Should never be accessed directly.
|
---|
108 | * @see #getLocaleName()
|
---|
109 | */
|
---|
110 | public String locale_name;
|
---|
111 | public boolean preset_name_label;
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * The types as preparsed collection.
|
---|
115 | */
|
---|
116 | public transient Set<TaggingPresetType> types;
|
---|
117 | public final transient List<TaggingPresetItem> data = new LinkedList<>();
|
---|
118 | public transient Roles roles;
|
---|
119 | public transient TemplateEntry nameTemplate;
|
---|
120 | public transient Match nameTemplateFilter;
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * True whenever the original selection given into createSelection was empty
|
---|
124 | */
|
---|
125 | private boolean originalSelectionEmpty;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Create an empty tagging preset. This will not have any items and
|
---|
129 | * will be an empty string as text. createPanel will return null.
|
---|
130 | * Use this as default item for "do not select anything".
|
---|
131 | */
|
---|
132 | public TaggingPreset() {
|
---|
133 | MainApplication.getLayerManager().addActiveLayerChangeListener(this);
|
---|
134 | updateEnabledState();
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Change the display name without changing the toolbar value.
|
---|
139 | */
|
---|
140 | public void setDisplayName() {
|
---|
141 | putValue(Action.NAME, getName());
|
---|
142 | putValue("toolbar", "tagging_" + getRawName());
|
---|
143 | putValue(OPTIONAL_TOOLTIP_TEXT, group != null ?
|
---|
144 | tr("Use preset ''{0}'' of group ''{1}''", getLocaleName(), group.getName()) :
|
---|
145 | tr("Use preset ''{0}''", getLocaleName()));
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Gets the localized version of the name
|
---|
150 | * @return The name that should be displayed to the user.
|
---|
151 | */
|
---|
152 | public String getLocaleName() {
|
---|
153 | if (locale_name == null) {
|
---|
154 | if (name_context != null) {
|
---|
155 | locale_name = trc(name_context, TaggingPresetItem.fixPresetString(name));
|
---|
156 | } else {
|
---|
157 | locale_name = tr(TaggingPresetItem.fixPresetString(name));
|
---|
158 | }
|
---|
159 | }
|
---|
160 | return locale_name;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the translated name of this preset, prefixed with the group names it belongs to.
|
---|
165 | * @return the translated name of this preset, prefixed with the group names it belongs to
|
---|
166 | */
|
---|
167 | public String getName() {
|
---|
168 | return group != null ? group.getName() + '/' + getLocaleName() : getLocaleName();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Returns the non translated name of this preset, prefixed with the (non translated) group names it belongs to.
|
---|
173 | * @return the non translated name of this preset, prefixed with the (non translated) group names it belongs to
|
---|
174 | */
|
---|
175 | public String getRawName() {
|
---|
176 | return group != null ? group.getRawName() + '/' + name : name;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Returns the preset icon (16px).
|
---|
181 | * @return The preset icon, or {@code null} if none defined
|
---|
182 | * @since 6403
|
---|
183 | */
|
---|
184 | public final ImageIcon getIcon() {
|
---|
185 | return getIcon(Action.SMALL_ICON);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Returns the preset icon (16 or 24px).
|
---|
190 | * @param key Key determining icon size: {@code Action.SMALL_ICON} for 16x, {@code Action.LARGE_ICON_KEY} for 24px
|
---|
191 | * @return The preset icon, or {@code null} if none defined
|
---|
192 | * @since 10849
|
---|
193 | */
|
---|
194 | public final ImageIcon getIcon(String key) {
|
---|
195 | Object icon = getValue(key);
|
---|
196 | if (icon instanceof ImageIcon) {
|
---|
197 | return (ImageIcon) icon;
|
---|
198 | }
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Called from the XML parser to set the icon.
|
---|
204 | * The loading task is performed in the background in order to speedup startup.
|
---|
205 | * @param iconName icon name
|
---|
206 | */
|
---|
207 | public void setIcon(final String iconName) {
|
---|
208 | this.iconName = iconName;
|
---|
209 | if (!TaggingPresetReader.isLoadIcons()) {
|
---|
210 | return;
|
---|
211 | }
|
---|
212 | File arch = TaggingPresetReader.getZipIcons();
|
---|
213 | final Collection<String> s = Config.getPref().getList("taggingpreset.icon.sources", null);
|
---|
214 | ImageProvider imgProv = new ImageProvider(iconName);
|
---|
215 | imgProv.setDirs(s);
|
---|
216 | imgProv.setId("presets");
|
---|
217 | imgProv.setArchive(arch);
|
---|
218 | imgProv.setOptional(true);
|
---|
219 | imgProv.getResourceAsync(result -> {
|
---|
220 | if (result != null) {
|
---|
221 | GuiHelper.runInEDT(() -> result.attachImageIcon(this));
|
---|
222 | } else {
|
---|
223 | Logging.warn(toString() + ": " + PRESET_ICON_ERROR_MSG_PREFIX + iconName);
|
---|
224 | }
|
---|
225 | });
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Called from the XML parser to set the types this preset affects.
|
---|
230 | * @param types comma-separated primitive types ("node", "way", "relation" or "closedway")
|
---|
231 | * @throws SAXException if any SAX error occurs
|
---|
232 | * @see TaggingPresetType#fromString
|
---|
233 | */
|
---|
234 | public void setType(String types) throws SAXException {
|
---|
235 | this.types = TaggingPresetItem.getType(types);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public void setName_template(String pattern) throws SAXException {
|
---|
239 | try {
|
---|
240 | this.nameTemplate = new TemplateParser(pattern).parse();
|
---|
241 | } catch (ParseError e) {
|
---|
242 | Logging.error("Error while parsing " + pattern + ": " + e.getMessage());
|
---|
243 | throw new SAXException(e);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | public void setName_template_filter(String filter) throws SAXException {
|
---|
248 | try {
|
---|
249 | this.nameTemplateFilter = SearchCompiler.compile(filter);
|
---|
250 | } catch (SearchParseError e) {
|
---|
251 | Logging.error("Error while parsing" + filter + ": " + e.getMessage());
|
---|
252 | throw new SAXException(e);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | private static class PresetPanel extends JPanel {
|
---|
257 | private boolean hasElements;
|
---|
258 |
|
---|
259 | PresetPanel() {
|
---|
260 | super(new GridBagLayout());
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Returns the tags being directly applied (without UI element) by {@link Key} items
|
---|
266 | *
|
---|
267 | * @return a list of tags
|
---|
268 | */
|
---|
269 | private List<Tag> getDirectlyAppliedTags() {
|
---|
270 | List<Tag> tags = new ArrayList<>();
|
---|
271 | for (TaggingPresetItem item : data) {
|
---|
272 | if (item instanceof Key) {
|
---|
273 | tags.add(((Key) item).asTag());
|
---|
274 | }
|
---|
275 | }
|
---|
276 | return tags;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Creates a panel for this preset. This includes general information such as name and supported {@link TaggingPresetType types}.
|
---|
281 | * This includes the elements from the individual {@link TaggingPresetItem items}.
|
---|
282 | *
|
---|
283 | * @param selected the selected primitives
|
---|
284 | * @return the newly created panel
|
---|
285 | */
|
---|
286 | public PresetPanel createPanel(Collection<OsmPrimitive> selected) {
|
---|
287 | PresetPanel p = new PresetPanel();
|
---|
288 | List<Link> l = new LinkedList<>();
|
---|
289 | List<PresetLink> presetLink = new LinkedList<>();
|
---|
290 |
|
---|
291 | final JPanel pp = new JPanel();
|
---|
292 | if (types != null) {
|
---|
293 | for (TaggingPresetType t : types) {
|
---|
294 | JLabel la = new JLabel(ImageProvider.get(t.getIconName()));
|
---|
295 | la.setToolTipText(tr("Elements of type {0} are supported.", tr(t.getName())));
|
---|
296 | pp.add(la);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | final List<Tag> directlyAppliedTags = getDirectlyAppliedTags();
|
---|
300 | if (!directlyAppliedTags.isEmpty()) {
|
---|
301 | final JLabel label = new JLabel(ImageProvider.get("pastetags"));
|
---|
302 | label.setToolTipText("<html>" + tr("This preset also sets: {0}", Utils.joinAsHtmlUnorderedList(directlyAppliedTags)));
|
---|
303 | pp.add(label);
|
---|
304 | }
|
---|
305 | final int count = pp.getComponentCount();
|
---|
306 | if (preset_name_label) {
|
---|
307 | p.add(new JLabel(getIcon(Action.LARGE_ICON_KEY)), GBC.std(0, 0).span(1, count > 0 ? 2 : 1).insets(0, 0, 5, 0));
|
---|
308 | }
|
---|
309 | if (count > 0) {
|
---|
310 | p.add(pp, GBC.std(1, 0).span(GBC.REMAINDER));
|
---|
311 | }
|
---|
312 | if (preset_name_label) {
|
---|
313 | p.add(new JLabel(getName()), GBC.std(1, count > 0 ? 1 : 0).insets(5, 0, 0, 0).span(GBC.REMAINDER).fill(GBC.HORIZONTAL));
|
---|
314 | }
|
---|
315 |
|
---|
316 | boolean presetInitiallyMatches = !selected.isEmpty() && selected.stream().allMatch(this);
|
---|
317 | JPanel items = new JPanel(new GridBagLayout());
|
---|
318 | for (TaggingPresetItem i : data) {
|
---|
319 | if (i instanceof Link) {
|
---|
320 | l.add((Link) i);
|
---|
321 | p.hasElements = true;
|
---|
322 | } else if (i instanceof PresetLink) {
|
---|
323 | presetLink.add((PresetLink) i);
|
---|
324 | } else {
|
---|
325 | if (i.addToPanel(items, selected, presetInitiallyMatches)) {
|
---|
326 | p.hasElements = true;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | p.add(items, GBC.eol().fill());
|
---|
331 | if (selected.isEmpty() && !supportsRelation()) {
|
---|
332 | GuiHelper.setEnabledRec(items, false);
|
---|
333 | }
|
---|
334 |
|
---|
335 | // add PresetLink
|
---|
336 | if (!presetLink.isEmpty()) {
|
---|
337 | p.add(new JLabel(tr("Edit also …")), GBC.eol().insets(0, 8, 0, 0));
|
---|
338 | for (PresetLink link : presetLink) {
|
---|
339 | link.addToPanel(p, selected, presetInitiallyMatches);
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | // add Link
|
---|
344 | for (Link link : l) {
|
---|
345 | link.addToPanel(p, selected, presetInitiallyMatches);
|
---|
346 | }
|
---|
347 |
|
---|
348 | // "Add toolbar button"
|
---|
349 | JToggleButton tb = new JToggleButton(new ToolbarButtonAction());
|
---|
350 | tb.setFocusable(false);
|
---|
351 | p.add(tb, GBC.std(1, 0).anchor(GBC.LINE_END));
|
---|
352 | return p;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Determines whether a dialog can be shown for this preset, i.e., at least one tag can/must be set by the user.
|
---|
357 | *
|
---|
358 | * @return {@code true} if a dialog can be shown for this preset
|
---|
359 | */
|
---|
360 | public boolean isShowable() {
|
---|
361 | for (TaggingPresetItem i : data) {
|
---|
362 | if (!(i instanceof Optional || i instanceof Space || i instanceof Key))
|
---|
363 | return true;
|
---|
364 | }
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 |
|
---|
368 | public String suggestRoleForOsmPrimitive(OsmPrimitive osm) {
|
---|
369 | if (roles != null && osm != null) {
|
---|
370 | for (Role i : roles.roles) {
|
---|
371 | if (i.memberExpression != null && i.memberExpression.match(osm)
|
---|
372 | && (i.types == null || i.types.isEmpty() || i.types.contains(TaggingPresetType.forPrimitive(osm)))) {
|
---|
373 | return i.key;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 | return null;
|
---|
378 | }
|
---|
379 |
|
---|
380 | @Override
|
---|
381 | public void actionPerformed(ActionEvent e) {
|
---|
382 | if (Main.main == null) {
|
---|
383 | return;
|
---|
384 | }
|
---|
385 | DataSet ds = Main.main.getEditDataSet();
|
---|
386 | Collection<OsmPrimitive> participants = Collections.emptyList();
|
---|
387 | if (ds != null) {
|
---|
388 | participants = ds.getSelected();
|
---|
389 | }
|
---|
390 |
|
---|
391 | // Display dialog even if no data layer (used by preset-tagging-tester plugin)
|
---|
392 | Collection<OsmPrimitive> sel = createSelection(participants);
|
---|
393 | int answer = showDialog(sel, supportsRelation());
|
---|
394 |
|
---|
395 | if (ds == null) {
|
---|
396 | return;
|
---|
397 | }
|
---|
398 |
|
---|
399 | if (!sel.isEmpty() && answer == DIALOG_ANSWER_APPLY) {
|
---|
400 | Command cmd = createCommand(sel, getChangedTags());
|
---|
401 | if (cmd != null) {
|
---|
402 | MainApplication.undoRedo.add(cmd);
|
---|
403 | }
|
---|
404 | } else if (answer == DIALOG_ANSWER_NEW_RELATION) {
|
---|
405 | final Relation r = new Relation();
|
---|
406 | final Collection<RelationMember> members = new HashSet<>();
|
---|
407 | for (Tag t : getChangedTags()) {
|
---|
408 | r.put(t.getKey(), t.getValue());
|
---|
409 | }
|
---|
410 | for (OsmPrimitive osm : ds.getSelected()) {
|
---|
411 | String role = suggestRoleForOsmPrimitive(osm);
|
---|
412 | RelationMember rm = new RelationMember(role == null ? "" : role, osm);
|
---|
413 | r.addMember(rm);
|
---|
414 | members.add(rm);
|
---|
415 | }
|
---|
416 | SwingUtilities.invokeLater(() -> RelationEditor.getEditor(
|
---|
417 | MainApplication.getLayerManager().getEditLayer(), r, members).setVisible(true));
|
---|
418 | }
|
---|
419 | ds.setSelected(ds.getSelected()); // force update
|
---|
420 | }
|
---|
421 |
|
---|
422 | private static class PresetDialog extends ExtendedDialog {
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Constructs a new {@code PresetDialog}.
|
---|
426 | * @param content the content that will be displayed in this dialog
|
---|
427 | * @param title the text that will be shown in the window titlebar
|
---|
428 | * @param icon the image to be displayed as the icon for this window
|
---|
429 | * @param disableApply whether to disable "Apply" button
|
---|
430 | * @param showNewRelation whether to display "New relation" button
|
---|
431 | */
|
---|
432 | PresetDialog(Component content, String title, ImageIcon icon, boolean disableApply, boolean showNewRelation) {
|
---|
433 | super(Main.parent, title,
|
---|
434 | showNewRelation ?
|
---|
435 | (new String[] {tr("Apply Preset"), tr("New relation"), tr("Cancel")}) :
|
---|
436 | (new String[] {tr("Apply Preset"), tr("Cancel")}),
|
---|
437 | true);
|
---|
438 | if (icon != null)
|
---|
439 | setIconImage(icon.getImage());
|
---|
440 | contentInsets = new Insets(10, 5, 0, 5);
|
---|
441 | if (showNewRelation) {
|
---|
442 | setButtonIcons("ok", "dialogs/addrelation", "cancel");
|
---|
443 | } else {
|
---|
444 | setButtonIcons("ok", "cancel");
|
---|
445 | }
|
---|
446 | setContent(content);
|
---|
447 | setDefaultButton(1);
|
---|
448 | setupDialog();
|
---|
449 | buttons.get(0).setEnabled(!disableApply);
|
---|
450 | buttons.get(0).setToolTipText(title);
|
---|
451 | // Prevent dialogs of being too narrow (fix #6261)
|
---|
452 | Dimension d = getSize();
|
---|
453 | if (d.width < 350) {
|
---|
454 | d.width = 350;
|
---|
455 | setSize(d);
|
---|
456 | }
|
---|
457 | super.showDialog();
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Shows the preset dialog.
|
---|
463 | * @param sel selection
|
---|
464 | * @param showNewRelation whether to display "New relation" button
|
---|
465 | * @return the user choice after the dialog has been closed
|
---|
466 | */
|
---|
467 | public int showDialog(Collection<OsmPrimitive> sel, boolean showNewRelation) {
|
---|
468 | PresetPanel p = createPanel(sel);
|
---|
469 |
|
---|
470 | int answer = 1;
|
---|
471 | boolean canCreateRelation = types == null || types.contains(TaggingPresetType.RELATION);
|
---|
472 | if (originalSelectionEmpty && !canCreateRelation) {
|
---|
473 | new Notification(
|
---|
474 | tr("The preset <i>{0}</i> cannot be applied since nothing has been selected!", getLocaleName()))
|
---|
475 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
476 | .show();
|
---|
477 | return DIALOG_ANSWER_CANCEL;
|
---|
478 | } else if (sel.isEmpty() && !canCreateRelation) {
|
---|
479 | new Notification(
|
---|
480 | tr("The preset <i>{0}</i> cannot be applied since the selection is unsuitable!", getLocaleName()))
|
---|
481 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
482 | .show();
|
---|
483 | return DIALOG_ANSWER_CANCEL;
|
---|
484 | } else if (p.getComponentCount() != 0 && (sel.isEmpty() || p.hasElements)) {
|
---|
485 | String title = trn("Change {0} object", "Change {0} objects", sel.size(), sel.size());
|
---|
486 | if (!showNewRelation && sel.isEmpty()) {
|
---|
487 | if (originalSelectionEmpty) {
|
---|
488 | title = tr("Nothing selected!");
|
---|
489 | } else {
|
---|
490 | title = tr("Selection unsuitable!");
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | answer = new PresetDialog(p, title, preset_name_label ? null : (ImageIcon) getValue(Action.SMALL_ICON),
|
---|
495 | sel.isEmpty(), showNewRelation).getValue();
|
---|
496 | }
|
---|
497 | if (!showNewRelation && answer == 2)
|
---|
498 | return DIALOG_ANSWER_CANCEL;
|
---|
499 | else
|
---|
500 | return answer;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Removes all unsuitable OsmPrimitives from the given list
|
---|
505 | * @param participants List of possible OsmPrimitives to tag
|
---|
506 | * @return Cleaned list with suitable OsmPrimitives only
|
---|
507 | */
|
---|
508 | public Collection<OsmPrimitive> createSelection(Collection<OsmPrimitive> participants) {
|
---|
509 | originalSelectionEmpty = participants.isEmpty();
|
---|
510 | Collection<OsmPrimitive> sel = new LinkedList<>();
|
---|
511 | for (OsmPrimitive osm : participants) {
|
---|
512 | if (typeMatches(EnumSet.of(TaggingPresetType.forPrimitive(osm)))) {
|
---|
513 | sel.add(osm);
|
---|
514 | }
|
---|
515 | }
|
---|
516 | return sel;
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Gets a list of tags that are set by this preset.
|
---|
521 | * @return The list of tags.
|
---|
522 | */
|
---|
523 | public List<Tag> getChangedTags() {
|
---|
524 | List<Tag> result = new ArrayList<>();
|
---|
525 | for (TaggingPresetItem i: data) {
|
---|
526 | i.addCommands(result);
|
---|
527 | }
|
---|
528 | return result;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Create a command to change the given list of tags.
|
---|
533 | * @param sel The primitives to change the tags for
|
---|
534 | * @param changedTags The tags to change
|
---|
535 | * @return A command that changes the tags.
|
---|
536 | */
|
---|
537 | public static Command createCommand(Collection<OsmPrimitive> sel, List<Tag> changedTags) {
|
---|
538 | List<Command> cmds = new ArrayList<>();
|
---|
539 | for (Tag tag: changedTags) {
|
---|
540 | ChangePropertyCommand cmd = new ChangePropertyCommand(sel, tag.getKey(), tag.getValue());
|
---|
541 | if (cmd.getObjectsNumber() > 0) {
|
---|
542 | cmds.add(cmd);
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (cmds.isEmpty())
|
---|
547 | return null;
|
---|
548 | else if (cmds.size() == 1)
|
---|
549 | return cmds.get(0);
|
---|
550 | else
|
---|
551 | return new SequenceCommand(tr("Change Tags"), cmds);
|
---|
552 | }
|
---|
553 |
|
---|
554 | private boolean supportsRelation() {
|
---|
555 | return types == null || types.contains(TaggingPresetType.RELATION);
|
---|
556 | }
|
---|
557 |
|
---|
558 | protected final void updateEnabledState() {
|
---|
559 | setEnabled(Main.main != null && Main.main.getEditDataSet() != null);
|
---|
560 | }
|
---|
561 |
|
---|
562 | @Override
|
---|
563 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
564 | updateEnabledState();
|
---|
565 | }
|
---|
566 |
|
---|
567 | @Override
|
---|
568 | public String toString() {
|
---|
569 | return (types == null ? "" : types.toString()) + ' ' + name;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Determines whether this preset matches the types.
|
---|
574 | * @param t The types that must match
|
---|
575 | * @return <code>true</code> if all types match.
|
---|
576 | */
|
---|
577 | public boolean typeMatches(Collection<TaggingPresetType> t) {
|
---|
578 | return t == null || types == null || types.containsAll(t);
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Determines whether this preset matches the given primitive, i.e.,
|
---|
583 | * whether the {@link #typeMatches(Collection) type matches} and the {@link TaggingPresetItem#matches(Map) tags match}.
|
---|
584 | *
|
---|
585 | * @param p the primitive
|
---|
586 | * @return {@code true} if this preset matches the primitive
|
---|
587 | */
|
---|
588 | @Override
|
---|
589 | public boolean test(OsmPrimitive p) {
|
---|
590 | return matches(EnumSet.of(TaggingPresetType.forPrimitive(p)), p.getKeys(), false);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Determines whether this preset matches the parameters.
|
---|
595 | *
|
---|
596 | * @param t the preset types to include, see {@link #typeMatches(Collection)}
|
---|
597 | * @param tags the tags to perform matching on, see {@link TaggingPresetItem#matches(Map)}
|
---|
598 | * @param onlyShowable whether the preset must be {@link #isShowable() showable}
|
---|
599 | * @return {@code true} if this preset matches the parameters.
|
---|
600 | */
|
---|
601 | public boolean matches(Collection<TaggingPresetType> t, Map<String, String> tags, boolean onlyShowable) {
|
---|
602 | if ((onlyShowable && !isShowable()) || !typeMatches(t)) {
|
---|
603 | return false;
|
---|
604 | } else {
|
---|
605 | return TaggingPresetItem.matches(data, tags);
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | /**
|
---|
610 | * Action that adds or removes the button on main toolbar
|
---|
611 | */
|
---|
612 | public class ToolbarButtonAction extends AbstractAction {
|
---|
613 | private final int toolbarIndex;
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Constructs a new {@code ToolbarButtonAction}.
|
---|
617 | */
|
---|
618 | public ToolbarButtonAction() {
|
---|
619 | super("");
|
---|
620 | new ImageProvider("dialogs", "pin").getResource().attachImageIcon(this, true);
|
---|
621 | putValue(SHORT_DESCRIPTION, tr("Add or remove toolbar button"));
|
---|
622 | List<String> t = new LinkedList<>(ToolbarPreferences.getToolString());
|
---|
623 | toolbarIndex = t.indexOf(getToolbarString());
|
---|
624 | putValue(SELECTED_KEY, toolbarIndex >= 0);
|
---|
625 | }
|
---|
626 |
|
---|
627 | @Override
|
---|
628 | public void actionPerformed(ActionEvent ae) {
|
---|
629 | String res = getToolbarString();
|
---|
630 | MainApplication.getToolbar().addCustomButton(res, toolbarIndex, true);
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | /**
|
---|
635 | * Gets a string describing this preset that can be used for the toolbar
|
---|
636 | * @return A String that can be passed on to the toolbar
|
---|
637 | * @see ToolbarPreferences#addCustomButton(String, int, boolean)
|
---|
638 | */
|
---|
639 | public String getToolbarString() {
|
---|
640 | ToolbarPreferences.ActionParser actionParser = new ToolbarPreferences.ActionParser(null);
|
---|
641 | return actionParser.saveAction(new ToolbarPreferences.ActionDefinition(this));
|
---|
642 | }
|
---|
643 | }
|
---|