1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
17 | import 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 | */
|
---|
24 | public abstract class TransformNodesCommand extends Command {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The nodes to transform.
|
---|
28 | */
|
---|
29 | protected Collection<Node> nodes = new LinkedList<>();
|
---|
30 |
|
---|
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 | */
|
---|
50 | public TransformNodesCommand(Collection<OsmPrimitive> objects) {
|
---|
51 | this.nodes = AllNodesVisitor.getAllNodes(objects);
|
---|
52 | storeOldState();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Handling of a mouse event (e.g. dragging event).
|
---|
57 | * @param currentEN the current world position of the mouse
|
---|
58 | */
|
---|
59 | public abstract void handleEvent(EastNorth currentEN);
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Implementation for the nodes transformation.
|
---|
63 | * No parameters are given here, you should handle the user input in handleEvent()
|
---|
64 | * and store it internally.
|
---|
65 | */
|
---|
66 | protected abstract void transformNodes();
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Finally apply the transformation of the nodes.
|
---|
70 | * This is called when the user is happy with the current state of the command
|
---|
71 | * and its effects.
|
---|
72 | */
|
---|
73 | @Override
|
---|
74 | public boolean executeCommand() {
|
---|
75 | transformNodes();
|
---|
76 | flagNodesAsModified();
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Flag all nodes as modified.
|
---|
82 | */
|
---|
83 | public void flagNodesAsModified() {
|
---|
84 | for (Node n : nodes) {
|
---|
85 | n.setModified(true);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Restore the state of the nodes from the backup.
|
---|
91 | */
|
---|
92 | @Override
|
---|
93 | public void undoCommand() {
|
---|
94 | for (Node n : nodes) {
|
---|
95 | OldNodeState os = oldStates.get(n);
|
---|
96 | n.setCoor(os.latlon);
|
---|
97 | n.setModified(os.modified);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
107 | return nodes;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public String getDescriptionText() {
|
---|
112 | return trn("Transform {0} node", "Transform {0} nodes", nodes.size(), nodes.size());
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public Icon getDescriptionIcon() {
|
---|
117 | return ImageProvider.get("data", "node");
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Get the nodes with the current transformation applied.
|
---|
122 | */
|
---|
123 | public Collection<Node> getTransformedNodes() {
|
---|
124 | return nodes;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Get the center of the nodes under modification.
|
---|
129 | * It's just the barycenter.
|
---|
130 | * @see org.openstreetmap.josm.tools.Geometry#getCentroid(java.util.List)
|
---|
131 | */
|
---|
132 | public EastNorth getNodesCenter() {
|
---|
133 | EastNorth sum = new EastNorth(0,0);
|
---|
134 |
|
---|
135 | for (Node n : nodes ) {
|
---|
136 | EastNorth en = n.getEastNorth();
|
---|
137 | sum = sum.add(en.east(), en.north());
|
---|
138 | }
|
---|
139 | return new EastNorth(sum.east()/this.nodes.size(), sum.north()/this.nodes.size());
|
---|
140 |
|
---|
141 | }
|
---|
142 | }
|
---|