source: josm/trunk/src/org/openstreetmap/josm/command/AddCommand.java@ 13664

Last change on this file since 13664 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.Collection;
8import java.util.Collections;
9import java.util.Objects;
10
11import javax.swing.Icon;
12
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
22 *
23 * See {@link ChangeCommand} for comments on relation back references.
24 *
25 * @author imi
26 */
27public class AddCommand extends Command {
28
29 /**
30 * The primitive to add to the dataset.
31 */
32 private final OsmPrimitive osm;
33
34 /**
35 * Creates the command and specify the element to add in the context of the given data set.
36 * @param data The data set. Must not be {@code null}
37 * @param osm The primitive to add
38 * @since 11240
39 */
40 public AddCommand(DataSet data, OsmPrimitive osm) {
41 super(data);
42 this.osm = Objects.requireNonNull(osm, "osm");
43 }
44
45 protected static final void checkNodeStyles(OsmPrimitive osm) {
46 if (osm instanceof Way) {
47 // Fix #10557 - node icon not updated after undoing/redoing addition of a way
48 ((Way) osm).clearCachedNodeStyles();
49 }
50 }
51
52 @Override
53 public boolean executeCommand() {
54 getAffectedDataSet().addPrimitive(osm);
55 osm.setModified(true);
56 checkNodeStyles(osm);
57 return true;
58 }
59
60 @Override
61 public void undoCommand() {
62 getAffectedDataSet().removePrimitive(osm);
63 checkNodeStyles(osm);
64 }
65
66 @Override
67 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
68 added.add(osm);
69 }
70
71 @Override
72 public String getDescriptionText() {
73 String msg;
74 switch(OsmPrimitiveType.from(osm)) {
75 case NODE: msg = marktr("Add node {0}"); break;
76 case WAY: msg = marktr("Add way {0}"); break;
77 case RELATION: msg = marktr("Add relation {0}"); break;
78 default: /* should not happen */msg = ""; break;
79 }
80 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
81 }
82
83 @Override
84 public Icon getDescriptionIcon() {
85 return ImageProvider.get(osm.getDisplayType());
86 }
87
88 @Override
89 public Collection<OsmPrimitive> getParticipatingPrimitives() {
90 return Collections.singleton(osm);
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(super.hashCode(), osm);
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 AddCommand that = (AddCommand) obj;
104 return Objects.equals(osm, that.osm);
105 }
106}
Note: See TracBrowser for help on using the repository browser.