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
RevLine 
[2512]1// License: GPL. For details, see LICENSE file.
[6887]2package org.openstreetmap.josm.command.conflict;
[2512]3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
[9371]7import java.util.Objects;
[5077]8
[4918]9import javax.swing.Icon;
[2512]10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
[6887]13import org.openstreetmap.josm.command.Command;
[2512]14import org.openstreetmap.josm.data.conflict.Conflict;
[12672]15import org.openstreetmap.josm.data.osm.DataSet;
[12663]16import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
[2512]17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.ImageProvider;
[12620]20import org.openstreetmap.josm.tools.Logging;
[11848]21import org.openstreetmap.josm.tools.Utils;
[2512]22
[6887]23/**
24 * Command used to add a new conflict.
25 * @since 1857
26 */
[2512]27public class ConflictAddCommand extends Command {
[8910]28 private final Conflict<? extends OsmPrimitive> conflict;
[2512]29
[6887]30 /**
31 * Constructs a new {@code ConflictAddCommand}.
32 * @param layer the data layer. Must not be null.
33 * @param conflict the conflict to add
[12718]34 * @deprecated to be removed end of 2017. Use {@link #ConflictAddCommand(DataSet, Conflict)} instead
[6887]35 */
[12718]36 @Deprecated
[2512]37 public ConflictAddCommand(OsmDataLayer layer, Conflict<? extends OsmPrimitive> conflict) {
38 super(layer);
[10378]39 this.conflict = conflict;
[2512]40 }
41
[12672]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
[2512]53 protected void warnBecauseOfDoubleConflict() {
54 JOptionPane.showMessageDialog(
55 Main.parent,
[3995]56 tr("<html>Layer ''{0}'' already has a conflict for object<br>"
[2512]57 + "''{1}''.<br>"
[2844]58 + "This conflict cannot be added.</html>",
[12718]59 Utils.escapeReservedCharactersHTML(getAffectedDataSet().getName()),
[11848]60 Utils.escapeReservedCharactersHTML(conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()))
[2512]61 ),
62 tr("Double conflict"),
63 JOptionPane.ERROR_MESSAGE
64 );
65 }
[6887]66
67 @Override
68 public boolean executeCommand() {
[2512]69 try {
[12672]70 getAffectedDataSet().getConflicts().add(conflict);
[8510]71 } catch (IllegalStateException e) {
[12620]72 Logging.error(e);
[2512]73 warnBecauseOfDoubleConflict();
74 }
75 return true;
76 }
77
[6887]78 @Override
79 public void undoCommand() {
[12718]80 DataSet ds = getAffectedDataSet();
81 if (!Main.main.containsDataSet(ds)) {
[12620]82 Logging.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
[12718]83 ds.getName(),
[2512]84 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
85 ));
86 return;
87 }
[12718]88 ds.getConflicts().remove(conflict);
[2512]89 }
90
[6887]91 @Override
92 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
[2512]93 // nothing to fill
94 }
95
[4918]96 @Override
97 public String getDescriptionText() {
98 return tr("Add conflict for ''{0}''",
99 conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()));
[2512]100 }
[4918]101
102 @Override
103 public Icon getDescriptionIcon() {
[5077]104 return ImageProvider.get(conflict.getMy().getDisplayType());
[4918]105 }
[8456]106
107 @Override
108 public int hashCode() {
[9371]109 return Objects.hash(super.hashCode(), conflict);
[8456]110 }
111
112 @Override
113 public boolean equals(Object obj) {
[9371]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);
[8456]119 }
[2512]120}
Note: See TracBrowser for help on using the repository browser.