1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.Objects;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
|
---|
23 | *
|
---|
24 | * See {@link ChangeCommand} for comments on relation back references.
|
---|
25 | *
|
---|
26 | * @author imi
|
---|
27 | */
|
---|
28 | public class AddCommand extends Command {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The primitive to add to the dataset.
|
---|
32 | */
|
---|
33 | private final OsmPrimitive osm;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Creates the command and specify the element to add in the context of the current edit layer, if any.
|
---|
37 | * @param osm The primitive to add
|
---|
38 | */
|
---|
39 | public AddCommand(OsmPrimitive osm) {
|
---|
40 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates the command and specify the element to add in the context of the given data layer.
|
---|
45 | * @param layer The data layer. Must not be {@code null}
|
---|
46 | * @param osm The primitive to add
|
---|
47 | * @deprecated to be removed end of 2017. Use {@link #AddCommand(DataSet, OsmPrimitive)} instead
|
---|
48 | */
|
---|
49 | @Deprecated
|
---|
50 | public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
|
---|
51 | super(layer);
|
---|
52 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Creates the command and specify the element to add in the context of the given data set.
|
---|
57 | * @param data The data set. Must not be {@code null}
|
---|
58 | * @param osm The primitive to add
|
---|
59 | * @since 11240
|
---|
60 | */
|
---|
61 | public AddCommand(DataSet data, OsmPrimitive osm) {
|
---|
62 | super(data);
|
---|
63 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected static final void checkNodeStyles(OsmPrimitive osm) {
|
---|
67 | if (osm instanceof Way) {
|
---|
68 | // Fix #10557 - node icon not updated after undoing/redoing addition of a way
|
---|
69 | ((Way) osm).clearCachedNodeStyles();
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public boolean executeCommand() {
|
---|
75 | getAffectedDataSet().addPrimitive(osm);
|
---|
76 | osm.setModified(true);
|
---|
77 | checkNodeStyles(osm);
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public void undoCommand() {
|
---|
83 | getAffectedDataSet().removePrimitive(osm);
|
---|
84 | checkNodeStyles(osm);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
89 | added.add(osm);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public String getDescriptionText() {
|
---|
94 | String msg;
|
---|
95 | switch(OsmPrimitiveType.from(osm)) {
|
---|
96 | case NODE: msg = marktr("Add node {0}"); break;
|
---|
97 | case WAY: msg = marktr("Add way {0}"); break;
|
---|
98 | case RELATION: msg = marktr("Add relation {0}"); break;
|
---|
99 | default: /* should not happen */msg = ""; break;
|
---|
100 | }
|
---|
101 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Icon getDescriptionIcon() {
|
---|
106 | return ImageProvider.get(osm.getDisplayType());
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public Collection<OsmPrimitive> getParticipatingPrimitives() {
|
---|
111 | return Collections.singleton(osm);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public int hashCode() {
|
---|
116 | return Objects.hash(super.hashCode(), osm);
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public boolean equals(Object obj) {
|
---|
121 | if (this == obj) return true;
|
---|
122 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
123 | if (!super.equals(obj)) return false;
|
---|
124 | AddCommand that = (AddCommand) obj;
|
---|
125 | return Objects.equals(osm, that.osm);
|
---|
126 | }
|
---|
127 | }
|
---|