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

Revision 4918, 4.2 KB checked in by simon04, 3 months ago (diff)

fix #7370 - Refactor Command.getDescription

  • Property svn:eol-style set to native
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.coor.LatLon;
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<Node>();
31
32    /**
33     * Small helper for holding the interesting part of the old data state of the
34     * nodes.
35     */
36    public static class OldState {
37        LatLon latlon;
38        EastNorth eastNorth;
39        boolean modified;
40    }
41
42    /**
43     * List of all old states of the nodes.
44     */
45    protected Map<Node, OldState> oldStates = new HashMap<Node, OldState>();
46
47    /**
48     * Stores the state of the nodes before the command.
49     */
50    protected void storeOldState() {
51        for (Node n : this.nodes) {
52            OldState os = new OldState();
53            os.latlon = new LatLon(n.getCoor());
54            os.eastNorth = n.getEastNorth();
55            os.modified = n.isModified();
56            oldStates.put(n, os);
57        }
58    }
59
60    /**
61     * Creates a TransformNodesObject.
62     * Find out the impacted nodes and store their initial state.
63     */
64    public TransformNodesCommand(Collection<OsmPrimitive> objects) {
65        this.nodes = AllNodesVisitor.getAllNodes(objects);
66        storeOldState();
67    }
68
69    /**
70     * Handling of a mouse event (e.g. dragging event).
71     * @param currentEN the current world position of the mouse
72     */
73    public abstract void handleEvent(EastNorth currentEN);
74
75    /**
76     * Implementation for the nodes transformation.
77     * No parameters are given here, you should handle the user input in handleEvent()
78     * and store it internally.
79     */
80    protected abstract void transformNodes();
81
82    /**
83     * Finally apply the transformation of the nodes.
84     * This is called when the user is happy with the current state of the command
85     * and its effects.
86     */
87    @Override
88    public boolean executeCommand() {
89        transformNodes();
90        flagNodesAsModified();
91        return true;
92    }
93
94    /**
95     * Flag all nodes as modified.
96     */
97    public void flagNodesAsModified() {
98        for (Node n : nodes) {
99            n.setModified(true);
100        }
101    }
102
103    /**
104     * Restore the state of the nodes from the backup.
105     */
106    @Override
107    public void undoCommand() {
108        for (Node n : nodes) {
109            OldState os = oldStates.get(n);
110            n.setCoor(os.latlon);
111            n.setModified(os.modified);
112        }
113    }
114
115    @Override
116    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
117    }
118
119    @Override
120    public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
121        return nodes;
122    }
123
124    @Override
125    public String getDescriptionText() {
126        return trn("Transform {0} node", "Transform {0} nodes", nodes.size(), nodes.size());
127    }
128
129    @Override
130    public Icon getDescriptionIcon() {
131        return ImageProvider.get("data", "node");
132    }
133
134    /**
135     * Get the nodes with the current transformation applied.
136     */
137    public Collection<Node> getTransformedNodes() {
138        return nodes;
139    }
140
141    /**
142     * Get the center of the nodes under modification.
143     * It's just the barycenter.
144     */
145    public EastNorth getNodesCenter() {
146        EastNorth sum = new EastNorth(0,0);
147
148        for (Node n : nodes ) {
149            EastNorth en = n.getEastNorth();
150            sum = sum.add(en.east(), en.north());
151        }
152        return new EastNorth(sum.east()/this.nodes.size(), sum.north()/this.nodes.size());
153
154    }
155}
Note: See TracBrowser for help on using the repository browser.