1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command.conflict;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Objects;
|
---|
8 |
|
---|
9 | import javax.swing.Icon;
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.command.Command;
|
---|
14 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
20 | import org.openstreetmap.josm.tools.Logging;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Command used to add a new conflict.
|
---|
25 | * @since 1857
|
---|
26 | */
|
---|
27 | public 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 | }
|
---|