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

Last change on this file since 9917 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 3.3 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.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.gui.DefaultNameFormatter;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
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 current edit layer, if any.
36 * @param osm The primitive to add
37 */
38 public AddCommand(OsmPrimitive osm) {
39 this.osm = osm;
40 }
41
42 /**
43 * Creates the command and specify the element to add in the context of the given data layer.
44 * @param layer The data layer. Must not be {@code null}
45 * @param osm The primitive to add
46 */
47 public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
48 super(layer);
49 this.osm = osm;
50 }
51
52 protected static final void checkNodeStyles(OsmPrimitive osm) {
53 if (osm instanceof Way) {
54 // Fix #10557 - node icon not updated after undoing/redoing addition of a way
55 ((Way) osm).clearCachedNodeStyles();
56 }
57 }
58
59 @Override
60 public boolean executeCommand() {
61 getLayer().data.addPrimitive(osm);
62 osm.setModified(true);
63 checkNodeStyles(osm);
64 return true;
65 }
66
67 @Override
68 public void undoCommand() {
69 getLayer().data.removePrimitive(osm);
70 checkNodeStyles(osm);
71 }
72
73 @Override
74 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
75 added.add(osm);
76 }
77
78 @Override
79 public String getDescriptionText() {
80 String msg;
81 switch(OsmPrimitiveType.from(osm)) {
82 case NODE: msg = marktr("Add node {0}"); break;
83 case WAY: msg = marktr("Add way {0}"); break;
84 case RELATION: msg = marktr("Add relation {0}"); break;
85 default: /* should not happen */msg = ""; break;
86 }
87 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
88 }
89
90 @Override
91 public Icon getDescriptionIcon() {
92 return ImageProvider.get(osm.getDisplayType());
93 }
94
95 @Override
96 public Collection<OsmPrimitive> getParticipatingPrimitives() {
97 return Collections.singleton(osm);
98 }
99
100 @Override
101 public int hashCode() {
102 return Objects.hash(super.hashCode(), osm);
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) return true;
108 if (obj == null || getClass() != obj.getClass()) return false;
109 if (!super.equals(obj)) return false;
110 AddCommand that = (AddCommand) obj;
111 return Objects.equals(osm, that.osm);
112 }
113}
Note: See TracBrowser for help on using the repository browser.