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

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