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

Last change on this file since 9070 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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