001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.gui.imageinfo;
003
004import java.awt.event.ActionEvent;
005
006import javax.swing.AbstractAction;
007import javax.swing.JOptionPane;
008
009import org.openstreetmap.josm.Main;
010import org.openstreetmap.josm.data.osm.AbstractPrimitive;
011import org.openstreetmap.josm.data.osm.Tag;
012import org.openstreetmap.josm.tools.I18n;
013import org.openstreetmap.josm.tools.ImageProvider;
014import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
015
016public class AddTagToPrimitiveAction extends AbstractAction {
017
018  private static final long serialVersionUID = -2134831346322019333L;
019
020  private Tag tag;
021  private AbstractPrimitive target;
022
023  public AddTagToPrimitiveAction(final String name) {
024    super(name, ImageProvider.get("dialogs/add", ImageSizes.SMALLICON));
025  }
026
027  public void setTag(Tag tag) {
028    this.tag = tag;
029    updateEnabled();
030  }
031
032  public void setTarget(AbstractPrimitive target) {
033    this.target = target;
034    updateEnabled();
035  }
036
037  private void updateEnabled() {
038    setEnabled(tag != null && target != null);
039  }
040
041  @Override
042  public void actionPerformed(ActionEvent e) {
043    if (target != null && tag != null) {
044      int conflictResolution = JOptionPane.YES_OPTION;
045      if (target.hasKey(tag.getKey()) && !target.hasTag(tag.getKey(), tag.getValue())) {
046        conflictResolution = JOptionPane.showConfirmDialog(
047          Main.parent,
048          "<html>" +
049            // TODO: tr( RRH
050            I18n.tr("A tag with key <i>{0}</i> is already present on the selected OSM object.", tag.getKey()) + "<br>" +
051            I18n.tr(
052              "Do you really want to replace the current value <i>{0}</i> with the new value <i>{1}</i>?",
053              target.get(tag.getKey()),
054              tag.getValue()
055            ) + "</html>",
056          I18n.tr("Tag conflict"),
057          JOptionPane.YES_NO_OPTION,
058          JOptionPane.WARNING_MESSAGE
059        );
060      }
061      if (JOptionPane.YES_OPTION == conflictResolution) {
062        target.put(tag);
063        target.setModified(true);
064      }
065    }
066  }
067}