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.data.osm.AbstractPrimitive; 010import org.openstreetmap.josm.data.osm.Tag; 011import org.openstreetmap.josm.gui.MainApplication; 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 MainApplication.getMainFrame(), 048 "<html>" + 049 I18n.tr("A tag with key <i>{0}</i> is already present on the selected OSM object.", tag.getKey()) + "<br>" + 050 I18n.tr( 051 "Do you really want to replace the current value <i>{0}</i> with the new value <i>{1}</i>?", 052 target.get(tag.getKey()), 053 tag.getValue() 054 ) + "</html>", 055 I18n.tr("Tag conflict"), 056 JOptionPane.YES_NO_OPTION, 057 JOptionPane.WARNING_MESSAGE 058 ); 059 } 060 if (JOptionPane.YES_OPTION == conflictResolution) { 061 target.put(tag); 062 target.setModified(true); 063 } 064 } 065 } 066}