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

Last change on this file since 10804 was 10663, checked in by Don-vip, 8 years ago

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 4.5 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.LinkedList;
9import java.util.Map;
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 Collection<Node> nodes = new LinkedList<>();
31
32 /**
33 * List of all old states of the nodes.
34 */
35 protected 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
50 */
51 public TransformNodesCommand(Collection<? extends OsmPrimitive> objects) {
52 this.nodes = AllNodesVisitor.getAllNodes(objects);
53 storeOldState();
54 }
55
56 /**
57 * Handling of a mouse event (e.g. dragging event).
58 * @param currentEN the current world position of the mouse
59 */
60 public abstract void handleEvent(EastNorth currentEN);
61
62 /**
63 * Implementation for the nodes transformation.
64 * No parameters are given here, you should handle the user input in handleEvent()
65 * and store it internally.
66 */
67 protected abstract void transformNodes();
68
69 /**
70 * Finally apply the transformation of the nodes.
71 * This is called when the user is happy with the current state of the command
72 * and its effects.
73 */
74 @Override
75 public boolean executeCommand() {
76 transformNodes();
77 flagNodesAsModified();
78 return true;
79 }
80
81 /**
82 * Flag all nodes as modified.
83 */
84 public void flagNodesAsModified() {
85 for (Node n : nodes) {
86 n.setModified(true);
87 }
88 }
89
90 /**
91 * Restore the state of the nodes from the backup.
92 */
93 @Override
94 public void undoCommand() {
95 for (Node n : nodes) {
96 OldNodeState os = oldStates.get(n);
97 n.setCoor(os.getLatLon());
98 n.setModified(os.isModified());
99 }
100 }
101
102 @Override
103 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
104 }
105
106 @Override
107 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
108 return nodes;
109 }
110
111 @Override
112 public String getDescriptionText() {
113 return trn("Transform {0} node", "Transform {0} nodes", nodes.size(), nodes.size());
114 }
115
116 @Override
117 public Icon getDescriptionIcon() {
118 return ImageProvider.get("data", "node");
119 }
120
121 /**
122 * Get the nodes with the current transformation applied.
123 * @return nodes with the current transformation applied
124 */
125 public Collection<Node> getTransformedNodes() {
126 return nodes;
127 }
128
129 /**
130 * Get the center of the nodes under modification.
131 * It's just the barycenter.
132 * @return center east/north of the nodes under modification
133 * @see org.openstreetmap.josm.tools.Geometry#getCentroid(java.util.List)
134 */
135 public EastNorth getNodesCenter() {
136 EastNorth sum = new EastNorth(0, 0);
137
138 for (Node n : nodes) {
139 EastNorth en = n.getEastNorth();
140 sum = sum.add(en.east(), en.north());
141 }
142 return new EastNorth(sum.east()/this.nodes.size(), sum.north()/this.nodes.size());
143
144 }
145
146 @Override
147 public int hashCode() {
148 return Objects.hash(super.hashCode(), nodes, oldStates);
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) return true;
154 if (obj == null || getClass() != obj.getClass()) return false;
155 if (!super.equals(obj)) return false;
156 TransformNodesCommand that = (TransformNodesCommand) obj;
157 return Objects.equals(nodes, that.nodes) &&
158 Objects.equals(oldStates, that.oldStates);
159 }
160}
Note: See TracBrowser for help on using the repository browser.