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.layer.OsmDataLayer;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Command used to add a new conflict.
|
---|
22 | * @since 1857
|
---|
23 | */
|
---|
24 | public class ConflictAddCommand extends Command {
|
---|
25 | private final Conflict<? extends OsmPrimitive> conflict;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code ConflictAddCommand}.
|
---|
29 | * @param layer the data layer. Must not be null.
|
---|
30 | * @param conflict the conflict to add
|
---|
31 | */
|
---|
32 | public ConflictAddCommand(OsmDataLayer layer, Conflict<? extends OsmPrimitive> conflict) {
|
---|
33 | super(layer);
|
---|
34 | this.conflict = conflict;
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected void warnBecauseOfDoubleConflict() {
|
---|
38 | JOptionPane.showMessageDialog(
|
---|
39 | Main.parent,
|
---|
40 | tr("<html>Layer ''{0}'' already has a conflict for object<br>"
|
---|
41 | + "''{1}''.<br>"
|
---|
42 | + "This conflict cannot be added.</html>",
|
---|
43 | getLayer().getName(),
|
---|
44 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
|
---|
45 | ),
|
---|
46 | tr("Double conflict"),
|
---|
47 | JOptionPane.ERROR_MESSAGE
|
---|
48 | );
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public boolean executeCommand() {
|
---|
53 | try {
|
---|
54 | getLayer().getConflicts().add(conflict);
|
---|
55 | } catch (IllegalStateException e) {
|
---|
56 | Main.error(e);
|
---|
57 | warnBecauseOfDoubleConflict();
|
---|
58 | }
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void undoCommand() {
|
---|
64 | if (Main.isDisplayingMapView() && !Main.map.mapView.hasLayer(getLayer())) {
|
---|
65 | Main.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
|
---|
66 | getLayer().getName(),
|
---|
67 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
|
---|
68 | ));
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | getLayer().getConflicts().remove(conflict);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
76 | // nothing to fill
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String getDescriptionText() {
|
---|
81 | return tr("Add conflict for ''{0}''",
|
---|
82 | conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public Icon getDescriptionIcon() {
|
---|
87 | return ImageProvider.get(conflict.getMy().getDisplayType());
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public int hashCode() {
|
---|
92 | return Objects.hash(super.hashCode(), conflict);
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public boolean equals(Object obj) {
|
---|
97 | if (this == obj) return true;
|
---|
98 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
99 | if (!super.equals(obj)) return false;
|
---|
100 | ConflictAddCommand that = (ConflictAddCommand) obj;
|
---|
101 | return Objects.equals(conflict, that.conflict);
|
---|
102 | }
|
---|
103 | }
|
---|