source: josm/trunk/src/org/openstreetmap/josm/command/TransformNodesCommand.java@ 13608

Last change on this file since 13608 was 12726, checked in by Don-vip, 7 years ago

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.Map;
9import java.util.NoSuchElementException;
10import java.util.Objects;
11
12import javax.swing.Icon;
13
14import org.openstreetmap.josm.data.coor.EastNorth;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Abstract class with common services for nodes rotation and scaling commands.
22 *
23 * @author Olivier Croquette <ocroquette@free.fr>
24 */
25public abstract class TransformNodesCommand extends Command {
26
27 /**
28 * The nodes to transform.
29 */
30 protected final Collection<Node> nodes;
31
32 /**
33 * List of all old states of the nodes.
34 */
35 protected final Map<Node, OldNodeState> oldStates = new HashMap<>();
36
37 /**
38 * Stores the state of the nodes before the command.
39 */
40 protected final void storeOldState() {
41 for (Node n : this.nodes) {
42 oldStates.put(n, new OldNodeState(n));
43 }
44 }
45
46 /**
47 * Creates a TransformNodesObject.
48 * Find out the impacted nodes and store their initial state.
49 * @param objects objects to fetch nodes from. Must neither be null nor empty. Items must belong to a data set
50 * @throws NullPointerException if objects is null or contain null item
51 * @throws NoSuchElementException if objects is empty
52 */
53 public TransformNodesCommand(Collection<? extends OsmPrimitive> objects) {
54 super(objects.iterator().next().getDataSet());
55 this.nodes = AllNodesVisitor.getAllNodes(objects);
56 storeOldState();
57 }
58
59 /**
60 * Handling of a mouse event (e.g. dragging event).
61 * @param currentEN the current world position of the mouse
62 */
63 public abstract void handleEvent(EastNorth currentEN);
64
65 /**
66 * Implementation for the nodes transformation.
67 * No parameters are given here, you should handle the user input in handleEvent()
68 * and store it internally.
69 */
70 protected abstract void transformNodes();
71
72 /**
73 * Finally apply the transformation of the nodes.
74 * This is called when the user is happy with the current state of the command
75 * and its effects.
76 */
77 @Override
78 public boolean executeCommand() {
79 transformNodes();
80 flagNodesAsModified();
81 return true;
82 }
83
84 /**
85 * Flag all nodes as modified.
86 */
87 public void flagNodesAsModified() {
88 for (Node n : nodes) {
89 n.setModified(true);
90 }
91 }
92
93 /**
94 * Restore the state of the nodes from the backup.
95 */
96 @Override
97 public void undoCommand() {
98 for (Node n : nodes) {
99 OldNodeState os = oldStates.get(n);
100 n.setCoor(os.getLatLon());
101 n.setModified(os.isModified());
102 }
103 }
104
105 @Override
106 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
107 }
108
109 @Override
110 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
111 return nodes;
112 }
113
114 @Override
115 public String getDescriptionText() {
116 return trn("Transform {0} node", "Transform {0} nodes", nodes.size(), nodes.size());
117 }
118
119 @Override
120 public Icon getDescriptionIcon() {
121 return ImageProvider.get("data", "node");
122 }
123
124 /**
125 * Get the nodes with the current transformation applied.
126 * @return nodes with the current transformation applied
127 */
128 public Collection<Node> getTransformedNodes() {
129 return nodes;
130 }
131
132 /**
133 * Get the center of the nodes under modification.
134 * It's just the barycenter.
135 * @return center east/north of the nodes under modification
136 * @see org.openstreetmap.josm.tools.Geometry#getCentroid(java.util.List)
137 */
138 public EastNorth getNodesCenter() {
139 EastNorth sum = new EastNorth(0, 0);
140
141 for (Node n : nodes) {
142 EastNorth en = n.getEastNorth();
143 sum = sum.add(en.east(), en.north());
144 }
145 return new EastNorth(sum.east()/this.nodes.size(), sum.north()/this.nodes.size());
146
147 }
148
149 @Override
150 public int hashCode() {
151 return Objects.hash(super.hashCode(), nodes, oldStates);
152 }
153
154 @Override
155 public boolean equals(Object obj) {
156 if (this == obj) return true;
157 if (obj == null || getClass() != obj.getClass()) return false;
158 if (!super.equals(obj)) return false;
159 TransformNodesCommand that = (TransformNodesCommand) obj;
160 return Objects.equals(nodes, that.nodes) &&
161 Objects.equals(oldStates, that.oldStates);
162 }
163}
Note: See TracBrowser for help on using the repository browser.