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

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

see #13036 - see #15229 - see #15182 - make Commands depends only on a DataSet, not a Layer. This removes a lot of GUI dependencies

  • Property svn:eol-style set to native
File size: 3.9 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.DataSet;
16import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
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 * @deprecated to be removed end of 2017. Use {@link #ConflictAddCommand(DataSet, Conflict)} instead
35 */
36 @Deprecated
37 public ConflictAddCommand(OsmDataLayer layer, Conflict<? extends OsmPrimitive> conflict) {
38 super(layer);
39 this.conflict = conflict;
40 }
41
42 /**
43 * Constructs a new {@code ConflictAddCommand}.
44 * @param ds the data set. Must not be null.
45 * @param conflict the conflict to add
46 * @since 12672
47 */
48 public ConflictAddCommand(DataSet ds, Conflict<? extends OsmPrimitive> conflict) {
49 super(ds);
50 this.conflict = conflict;
51 }
52
53 protected void warnBecauseOfDoubleConflict() {
54 JOptionPane.showMessageDialog(
55 Main.parent,
56 tr("<html>Layer ''{0}'' already has a conflict for object<br>"
57 + "''{1}''.<br>"
58 + "This conflict cannot be added.</html>",
59 Utils.escapeReservedCharactersHTML(getAffectedDataSet().getName()),
60 Utils.escapeReservedCharactersHTML(conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()))
61 ),
62 tr("Double conflict"),
63 JOptionPane.ERROR_MESSAGE
64 );
65 }
66
67 @Override
68 public boolean executeCommand() {
69 try {
70 getAffectedDataSet().getConflicts().add(conflict);
71 } catch (IllegalStateException e) {
72 Logging.error(e);
73 warnBecauseOfDoubleConflict();
74 }
75 return true;
76 }
77
78 @Override
79 public void undoCommand() {
80 DataSet ds = getAffectedDataSet();
81 if (!Main.main.containsDataSet(ds)) {
82 Logging.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
83 ds.getName(),
84 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
85 ));
86 return;
87 }
88 ds.getConflicts().remove(conflict);
89 }
90
91 @Override
92 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
93 // nothing to fill
94 }
95
96 @Override
97 public String getDescriptionText() {
98 return tr("Add conflict for ''{0}''",
99 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()));
100 }
101
102 @Override
103 public Icon getDescriptionIcon() {
104 return ImageProvider.get(conflict.getMy().getDisplayType());
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(super.hashCode(), conflict);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) return true;
115 if (obj == null || getClass() != obj.getClass()) return false;
116 if (!super.equals(obj)) return false;
117 ConflictAddCommand that = (ConflictAddCommand) obj;
118 return Objects.equals(conflict, that.conflict);
119 }
120}
Note: See TracBrowser for help on using the repository browser.