1 | package org.openstreetmap.josm.gui.layer;
|
---|
2 |
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.util.Iterator;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.Stack;
|
---|
7 |
|
---|
8 | import javax.swing.Icon;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.command.Command;
|
---|
12 | import org.openstreetmap.josm.data.Bounds;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Track;
|
---|
18 | import org.openstreetmap.josm.data.osm.visitor.BoundingVisitor;
|
---|
19 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
20 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
21 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
22 | import org.openstreetmap.josm.gui.ImageProvider;
|
---|
23 | import org.openstreetmap.josm.gui.MapView;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A layer holding data from a specific dataset.
|
---|
27 | * The data can be fully edited.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class OsmDataLayer extends Layer {
|
---|
32 |
|
---|
33 | private static Icon icon;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * The data behind this layer.
|
---|
37 | */
|
---|
38 | public final DataSet data;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * All commands that were made on the dataset.
|
---|
42 | */
|
---|
43 | private LinkedList<Command> commands = new LinkedList<Command>();
|
---|
44 | /**
|
---|
45 | * The stack for redoing commands
|
---|
46 | */
|
---|
47 | private Stack<Command> redoCommands = new Stack<Command>();
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Construct a OsmDataLayer.
|
---|
51 | */
|
---|
52 | public OsmDataLayer(DataSet data, String name) {
|
---|
53 | super(name);
|
---|
54 | this.data = data;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * TODO: @return Return a dynamic drawn icon of the map data. The icon is
|
---|
59 | * updated by a background thread to not disturb the running programm.
|
---|
60 | */
|
---|
61 | @Override
|
---|
62 | public Icon getIcon() {
|
---|
63 | if (icon == null)
|
---|
64 | icon = ImageProvider.get("layer", "osmdata");
|
---|
65 | return icon;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
70 | * are drawn by the edit layer).
|
---|
71 | * Draw nodes last to overlap the line segments they belong to.
|
---|
72 | */
|
---|
73 | @Override
|
---|
74 | public void paint(Graphics g, MapView mv) {
|
---|
75 | SimplePaintVisitor visitor = new SimplePaintVisitor(g, mv);
|
---|
76 | for (OsmPrimitive osm : data.lineSegments)
|
---|
77 | if (!osm.isDeleted())
|
---|
78 | osm.visit(visitor);
|
---|
79 | for (OsmPrimitive osm : data.tracks)
|
---|
80 | if (!osm.isDeleted())
|
---|
81 | osm.visit(visitor);
|
---|
82 | for (OsmPrimitive osm : data.nodes)
|
---|
83 | if (!osm.isDeleted())
|
---|
84 | osm.visit(visitor);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String getToolTipText() {
|
---|
89 | return data.nodes.size()+" nodes, "+data.lineSegments.size()+" segments, "+data.tracks.size()+" streets.";
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void mergeFrom(Layer from) {
|
---|
94 | MergeVisitor visitor = new MergeVisitor(data);
|
---|
95 | for (OsmPrimitive osm : ((OsmDataLayer)from).data.allPrimitives())
|
---|
96 | osm.visit(visitor);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public boolean isMergable(Layer other) {
|
---|
101 | return other instanceof OsmDataLayer;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Bounds getBoundsLatLon() {
|
---|
106 | BoundingVisitor b = new BoundingVisitor(BoundingVisitor.Type.LATLON);
|
---|
107 | for (Node n : data.nodes)
|
---|
108 | b.visit(n);
|
---|
109 | return b.bounds;
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public Bounds getBoundsXY() {
|
---|
114 | BoundingVisitor b = new BoundingVisitor(BoundingVisitor.Type.XY);
|
---|
115 | for (Node n : data.nodes)
|
---|
116 | b.visit(n);
|
---|
117 | return b.bounds;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void init(Projection projection) {
|
---|
122 | for (Node n : data.nodes)
|
---|
123 | projection.latlon2xy(n.coor);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * @return the last command added or <code>null</code> if no command in queue.
|
---|
128 | */
|
---|
129 | public Command lastCommand() {
|
---|
130 | return commands.isEmpty() ? null : commands.getLast();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Execute the command and add it to the intern command queue. Also mark all
|
---|
135 | * primitives in the command as modified.
|
---|
136 | */
|
---|
137 | public void add(Command c) {
|
---|
138 | c.executeCommand();
|
---|
139 | commands.add(c);
|
---|
140 | redoCommands.clear();
|
---|
141 | // TODO: Replace with listener scheme
|
---|
142 | Main.main.undoAction.setEnabled(true);
|
---|
143 | Main.main.redoAction.setEnabled(false);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Undoes the last added command.
|
---|
148 | */
|
---|
149 | public void undo() {
|
---|
150 | if (commands.isEmpty())
|
---|
151 | return;
|
---|
152 | Command c = commands.removeLast();
|
---|
153 | c.undoCommand();
|
---|
154 | redoCommands.push(c);
|
---|
155 | //TODO: Replace with listener scheme
|
---|
156 | Main.main.undoAction.setEnabled(!commands.isEmpty());
|
---|
157 | Main.main.redoAction.setEnabled(true);
|
---|
158 | }
|
---|
159 | /**
|
---|
160 | * Redoes the last undoed command.
|
---|
161 | */
|
---|
162 | public void redo() {
|
---|
163 | if (redoCommands.isEmpty())
|
---|
164 | return;
|
---|
165 | Command c = redoCommands.pop();
|
---|
166 | c.executeCommand();
|
---|
167 | commands.add(c);
|
---|
168 | //TODO: Replace with listener scheme
|
---|
169 | Main.main.undoAction.setEnabled(true);
|
---|
170 | Main.main.redoAction.setEnabled(!redoCommands.isEmpty());
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Clean out the data behind the layer. This means clearing the redo/undo lists,
|
---|
175 | * really deleting all deleted objects and reset the modified flags. This is done
|
---|
176 | * after a successfull upload.
|
---|
177 | */
|
---|
178 | public void cleanData() {
|
---|
179 | redoCommands.clear();
|
---|
180 | commands.clear();
|
---|
181 | for (Iterator<Node> it = data.nodes.iterator(); it.hasNext();)
|
---|
182 | cleanIterator(it);
|
---|
183 | for (Iterator<LineSegment> it = data.lineSegments.iterator(); it.hasNext();)
|
---|
184 | cleanIterator(it);
|
---|
185 | for (Iterator<Track> it = data.tracks.iterator(); it.hasNext();)
|
---|
186 | cleanIterator(it);
|
---|
187 | }
|
---|
188 |
|
---|
189 | private void cleanIterator(Iterator<? extends OsmPrimitive> it) {
|
---|
190 | OsmPrimitive osm = it.next();
|
---|
191 | osm.modified = false;
|
---|
192 | osm.modifiedProperties = false;
|
---|
193 | if (osm.isDeleted())
|
---|
194 | it.remove();
|
---|
195 | }
|
---|
196 | }
|
---|