| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.command; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.util.Collection; |
|---|
| 7 | |
|---|
| 8 | import javax.swing.Icon; |
|---|
| 9 | import javax.swing.JOptionPane; |
|---|
| 10 | |
|---|
| 11 | import org.openstreetmap.josm.Main; |
|---|
| 12 | import org.openstreetmap.josm.data.conflict.Conflict; |
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 14 | import org.openstreetmap.josm.gui.DefaultNameFormatter; |
|---|
| 15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
|---|
| 16 | import org.openstreetmap.josm.tools.ImageProvider; |
|---|
| 17 | |
|---|
| 18 | public class ConflictAddCommand extends Command { |
|---|
| 19 | private Conflict<? extends OsmPrimitive> conflict; |
|---|
| 20 | |
|---|
| 21 | public ConflictAddCommand(OsmDataLayer layer, Conflict<? extends OsmPrimitive> conflict) { |
|---|
| 22 | super(layer); |
|---|
| 23 | this.conflict = conflict; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | protected void warnBecauseOfDoubleConflict() { |
|---|
| 27 | JOptionPane.showMessageDialog( |
|---|
| 28 | Main.parent, |
|---|
| 29 | tr("<html>Layer ''{0}'' already has a conflict for object<br>" |
|---|
| 30 | + "''{1}''.<br>" |
|---|
| 31 | + "This conflict cannot be added.</html>", |
|---|
| 32 | getLayer().getName(), |
|---|
| 33 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()) |
|---|
| 34 | ), |
|---|
| 35 | tr("Double conflict"), |
|---|
| 36 | JOptionPane.ERROR_MESSAGE |
|---|
| 37 | ); |
|---|
| 38 | } |
|---|
| 39 | @Override public boolean executeCommand() { |
|---|
| 40 | try { |
|---|
| 41 | getLayer().getConflicts().add(conflict); |
|---|
| 42 | } catch(IllegalStateException e) { |
|---|
| 43 | e.printStackTrace(); |
|---|
| 44 | warnBecauseOfDoubleConflict(); |
|---|
| 45 | } |
|---|
| 46 | return true; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | @Override public void undoCommand() { |
|---|
| 50 | if (! Main.map.mapView.hasLayer(getLayer())) { |
|---|
| 51 | System.out.println(tr("Warning: Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.", |
|---|
| 52 | getLayer().getName(), |
|---|
| 53 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()) |
|---|
| 54 | )); |
|---|
| 55 | return; |
|---|
| 56 | } |
|---|
| 57 | getLayer().getConflicts().remove(conflict); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { |
|---|
| 61 | // nothing to fill |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | @Override |
|---|
| 65 | public String getDescriptionText() { |
|---|
| 66 | return tr("Add conflict for ''{0}''", |
|---|
| 67 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | @Override |
|---|
| 71 | public Icon getDescriptionIcon() { |
|---|
| 72 | return ImageProvider.get(conflict.getMy().getDisplayType()); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|