source: josm/trunk/src/org/openstreetmap/josm/command/conflict/ConflictAddCommand.java@ 12630

Last change on this file since 12630 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command.conflict;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.Objects;
8
9import javax.swing.Icon;
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.data.conflict.Conflict;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.gui.DefaultNameFormatter;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.openstreetmap.josm.tools.Logging;
21import org.openstreetmap.josm.tools.Utils;
22
23/**
24 * Command used to add a new conflict.
25 * @since 1857
26 */
27public class ConflictAddCommand extends Command {
28 private final Conflict<? extends OsmPrimitive> conflict;
29
30 /**
31 * Constructs a new {@code ConflictAddCommand}.
32 * @param layer the data layer. Must not be null.
33 * @param conflict the conflict to add
34 */
35 public ConflictAddCommand(OsmDataLayer layer, Conflict<? extends OsmPrimitive> conflict) {
36 super(layer);
37 this.conflict = conflict;
38 }
39
40 protected void warnBecauseOfDoubleConflict() {
41 JOptionPane.showMessageDialog(
42 Main.parent,
43 tr("<html>Layer ''{0}'' already has a conflict for object<br>"
44 + "''{1}''.<br>"
45 + "This conflict cannot be added.</html>",
46 Utils.escapeReservedCharactersHTML(getLayer().getName()),
47 Utils.escapeReservedCharactersHTML(conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()))
48 ),
49 tr("Double conflict"),
50 JOptionPane.ERROR_MESSAGE
51 );
52 }
53
54 @Override
55 public boolean executeCommand() {
56 try {
57 getLayer().getConflicts().add(conflict);
58 } catch (IllegalStateException e) {
59 Logging.error(e);
60 warnBecauseOfDoubleConflict();
61 }
62 return true;
63 }
64
65 @Override
66 public void undoCommand() {
67 if (MainApplication.isDisplayingMapView() && !Main.getLayerManager().containsLayer(getLayer())) {
68 Logging.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
69 getLayer().getName(),
70 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
71 ));
72 return;
73 }
74 getLayer().getConflicts().remove(conflict);
75 }
76
77 @Override
78 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
79 // nothing to fill
80 }
81
82 @Override
83 public String getDescriptionText() {
84 return tr("Add conflict for ''{0}''",
85 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()));
86 }
87
88 @Override
89 public Icon getDescriptionIcon() {
90 return ImageProvider.get(conflict.getMy().getDisplayType());
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(super.hashCode(), conflict);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) return true;
101 if (obj == null || getClass() != obj.getClass()) return false;
102 if (!super.equals(obj)) return false;
103 ConflictAddCommand that = (ConflictAddCommand) obj;
104 return Objects.equals(conflict, that.conflict);
105 }
106}
Note: See TracBrowser for help on using the repository browser.