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