1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Graphics;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Point;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.io.File;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.Iterator;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.Set;
|
---|
19 |
|
---|
20 | import javax.swing.Icon;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JMenuItem;
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JSeparator;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
29 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
30 | import org.openstreetmap.josm.actions.SaveAction;
|
---|
31 | import org.openstreetmap.josm.actions.SaveAsAction;
|
---|
32 | import org.openstreetmap.josm.command.Command;
|
---|
33 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
34 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
35 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
36 | import org.openstreetmap.josm.data.osm.Node;
|
---|
37 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
38 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
39 | import org.openstreetmap.josm.data.osm.Way;
|
---|
40 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
41 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
42 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
43 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
44 | import org.openstreetmap.josm.gui.MapView;
|
---|
45 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
46 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
47 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
48 | import org.openstreetmap.josm.tools.GBC;
|
---|
49 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * A layer holding data from a specific dataset.
|
---|
53 | * The data can be fully edited.
|
---|
54 | *
|
---|
55 | * @author imi
|
---|
56 | */
|
---|
57 | public class OsmDataLayer extends Layer {
|
---|
58 |
|
---|
59 | public final static class DataCountVisitor implements Visitor {
|
---|
60 | public final int[] normal = new int[3];
|
---|
61 | public final int[] deleted = new int[3];
|
---|
62 | public final String[] names = {"node", "segment", "way"};
|
---|
63 |
|
---|
64 | private void inc(final OsmPrimitive osm, final int i) {
|
---|
65 | normal[i]++;
|
---|
66 | if (osm.deleted)
|
---|
67 | deleted[i]++;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void visit(final Node n) {
|
---|
71 | inc(n, 0);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void visit(final Segment ls) {
|
---|
75 | inc(ls, 1);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void visit(final Way w) {
|
---|
79 | inc(w, 2);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public interface ModifiedChangedListener {
|
---|
84 | void modifiedChanged(boolean value, OsmDataLayer source);
|
---|
85 | }
|
---|
86 | public interface CommandQueueListener {
|
---|
87 | void commandChanged(int queueSize, int redoSize);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * @deprecated Use Main.main.undoRedo.add(...) instead.
|
---|
92 | */
|
---|
93 | @Deprecated public void add(final Command c) {
|
---|
94 | Main.main.undoRedo.add(c);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * The data behind this layer.
|
---|
99 | */
|
---|
100 | public final DataSet data;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Whether the data of this layer was modified during the session.
|
---|
104 | */
|
---|
105 | private boolean modified = false;
|
---|
106 | /**
|
---|
107 | * Whether the data was modified due an upload of the data to the server.
|
---|
108 | */
|
---|
109 | public boolean uploadedModified = false;
|
---|
110 |
|
---|
111 | public final LinkedList<ModifiedChangedListener> listenerModified = new LinkedList<ModifiedChangedListener>();
|
---|
112 |
|
---|
113 | private SimplePaintVisitor mapPainter = new SimplePaintVisitor();
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Construct a OsmDataLayer.
|
---|
117 | */
|
---|
118 | public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
|
---|
119 | super(name);
|
---|
120 | this.data = data;
|
---|
121 | this.associatedFile = associatedFile;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * TODO: @return Return a dynamic drawn icon of the map data. The icon is
|
---|
126 | * updated by a background thread to not disturb the running programm.
|
---|
127 | */
|
---|
128 | @Override public Icon getIcon() {
|
---|
129 | return ImageProvider.get("layer", "osmdata_small");
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
134 | * are drawn by the edit layer).
|
---|
135 | * Draw nodes last to overlap the segments they belong to.
|
---|
136 | */
|
---|
137 | @Override public void paint(final Graphics g, final MapView mv) {
|
---|
138 | boolean inactive = Main.map.mapView.getActiveLayer() != this && Main.pref.getBoolean("draw.data.inactive_color", true);
|
---|
139 | if (Main.pref.getBoolean("draw.data.downloaded_area", false)) {
|
---|
140 | // FIXME this is inefficient; instead a proper polygon has to be built, and instead
|
---|
141 | // of drawing the outline, the outlying areas should perhaps be shaded.
|
---|
142 | for (DataSource src : data.dataSources) {
|
---|
143 | if (src.bounds != null) {
|
---|
144 | EastNorth en1 = Main.proj.latlon2eastNorth(src.bounds.min);
|
---|
145 | EastNorth en2 = Main.proj.latlon2eastNorth(src.bounds.max);
|
---|
146 | Point p1 = mv.getPoint(en1);
|
---|
147 | Point p2 = mv.getPoint(en2);
|
---|
148 | Color color = inactive ? SimplePaintVisitor.getPreferencesColor("inactive", Color.DARK_GRAY) :
|
---|
149 | SimplePaintVisitor.getPreferencesColor("downloaded Area", Color.YELLOW);
|
---|
150 | g.setColor(color);
|
---|
151 | g.drawRect(Math.min(p1.x,p2.x), Math.min(p1.y, p2.y), Math.abs(p2.x-p1.x), Math.abs(p2.y-p1.y));
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | mapPainter.setGraphics(g);
|
---|
156 | mapPainter.setNavigatableComponent(mv);
|
---|
157 | mapPainter.inactive = inactive;
|
---|
158 | mapPainter.visitAll(data);
|
---|
159 | Main.map.conflictDialog.paintConflicts(g, mv);
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override public String getToolTipText() {
|
---|
163 | String tool = "";
|
---|
164 | tool += undeletedSize(data.nodes)+" "+trn("node", "nodes", undeletedSize(data.nodes))+", ";
|
---|
165 | tool += undeletedSize(data.segments)+" "+trn("segment", "segments", undeletedSize(data.segments))+", ";
|
---|
166 | tool += undeletedSize(data.ways)+" "+trn("way", "ways", undeletedSize(data.ways));
|
---|
167 | if (associatedFile != null)
|
---|
168 | tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>";
|
---|
169 | return tool;
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override public void mergeFrom(final Layer from) {
|
---|
173 | final MergeVisitor visitor = new MergeVisitor(data,((OsmDataLayer)from).data);
|
---|
174 | for (final OsmPrimitive osm : ((OsmDataLayer)from).data.allPrimitives())
|
---|
175 | osm.visit(visitor);
|
---|
176 | visitor.fixReferences();
|
---|
177 |
|
---|
178 | // copy the merged layer's data source info
|
---|
179 | for (DataSource src : ((OsmDataLayer)from).data.dataSources)
|
---|
180 | data.dataSources.add(src);
|
---|
181 |
|
---|
182 | if (visitor.conflicts.isEmpty())
|
---|
183 | return;
|
---|
184 | final ConflictDialog dlg = Main.map.conflictDialog;
|
---|
185 | dlg.add(visitor.conflicts);
|
---|
186 | JOptionPane.showMessageDialog(Main.parent,tr("There were conflicts during import."));
|
---|
187 | if (!dlg.isVisible())
|
---|
188 | dlg.action.actionPerformed(new ActionEvent(this, 0, ""));
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override public boolean isMergable(final Layer other) {
|
---|
192 | return other instanceof OsmDataLayer;
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override public void visitBoundingBox(final BoundingXYVisitor v) {
|
---|
196 | for (final Node n : data.nodes)
|
---|
197 | if (!n.deleted)
|
---|
198 | v.visit(n);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Clean out the data behind the layer. This means clearing the redo/undo lists,
|
---|
203 | * really deleting all deleted objects and reset the modified flags. This is done
|
---|
204 | * after a successfull upload.
|
---|
205 | *
|
---|
206 | * @param processed A list of all objects, that were actually uploaded.
|
---|
207 | * May be <code>null</code>, which means nothing has been uploaded but
|
---|
208 | * saved to disk instead. Note that an empty collection for "processed"
|
---|
209 | * means that an upload has been attempted but failed.
|
---|
210 | */
|
---|
211 | public void cleanData(final Collection<OsmPrimitive> processed, boolean dataAdded) {
|
---|
212 |
|
---|
213 | // return immediately if an upload attempt failed
|
---|
214 | if (processed != null && processed.isEmpty() && !dataAdded)
|
---|
215 | return;
|
---|
216 |
|
---|
217 | Main.main.undoRedo.clean();
|
---|
218 |
|
---|
219 | // if uploaded, clean the modified flags as well
|
---|
220 | if (processed != null) {
|
---|
221 | final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
|
---|
222 | for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();)
|
---|
223 | cleanIterator(it, processedSet);
|
---|
224 | for (final Iterator<Segment> it = data.segments.iterator(); it.hasNext();)
|
---|
225 | cleanIterator(it, processedSet);
|
---|
226 | for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();)
|
---|
227 | cleanIterator(it, processedSet);
|
---|
228 | }
|
---|
229 |
|
---|
230 | // update the modified flag
|
---|
231 | if (associatedFile != null && processed != null && !dataAdded)
|
---|
232 | return; // do nothing when uploading non-harmful changes.
|
---|
233 |
|
---|
234 | // modified if server changed the data (esp. the id).
|
---|
235 | uploadedModified = associatedFile != null && processed != null && dataAdded;
|
---|
236 | setModified(uploadedModified);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Clean the modified flag for the given iterator over a collection if it is in the
|
---|
241 | * list of processed entries.
|
---|
242 | *
|
---|
243 | * @param it The iterator to change the modified and remove the items if deleted.
|
---|
244 | * @param processed A list of all objects that have been successfully progressed.
|
---|
245 | * If the object in the iterator is not in the list, nothing will be changed on it.
|
---|
246 | */
|
---|
247 | private void cleanIterator(final Iterator<? extends OsmPrimitive> it, final Collection<OsmPrimitive> processed) {
|
---|
248 | final OsmPrimitive osm = it.next();
|
---|
249 | if (!processed.remove(osm))
|
---|
250 | return;
|
---|
251 | osm.modified = false;
|
---|
252 | if (osm.deleted)
|
---|
253 | it.remove();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public boolean isModified() {
|
---|
257 | return modified;
|
---|
258 | }
|
---|
259 |
|
---|
260 | public void setModified(final boolean modified) {
|
---|
261 | if (modified == this.modified)
|
---|
262 | return;
|
---|
263 | this.modified = modified;
|
---|
264 | for (final ModifiedChangedListener l : listenerModified)
|
---|
265 | l.modifiedChanged(modified, this);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @return The number of not-deleted primitives in the list.
|
---|
270 | */
|
---|
271 | private int undeletedSize(final Collection<? extends OsmPrimitive> list) {
|
---|
272 | int size = 0;
|
---|
273 | for (final OsmPrimitive osm : list)
|
---|
274 | if (!osm.deleted)
|
---|
275 | size++;
|
---|
276 | return size;
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Override public Object getInfoComponent() {
|
---|
280 | final DataCountVisitor counter = new DataCountVisitor();
|
---|
281 | for (final OsmPrimitive osm : data.allPrimitives())
|
---|
282 | osm.visit(counter);
|
---|
283 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
284 | p.add(new JLabel(tr("{0} consists of:", name)), GBC.eol());
|
---|
285 | for (int i = 0; i < counter.normal.length; ++i) {
|
---|
286 | String s = counter.normal[i]+" "+trn(counter.names[i],counter.names[i]+"s",counter.normal[i]);
|
---|
287 | if (counter.deleted[i] > 0)
|
---|
288 | s += tr(" ({0} deleted.)",counter.deleted[i]);
|
---|
289 | p.add(new JLabel(s, ImageProvider.get("data", counter.names[i]), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
|
---|
290 | }
|
---|
291 | return p;
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override public Component[] getMenuEntries() {
|
---|
295 | if (Main.applet) {
|
---|
296 | return new Component[]{
|
---|
297 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
298 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
299 | new JSeparator(),
|
---|
300 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
301 | new JSeparator(),
|
---|
302 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
303 | }
|
---|
304 | return new Component[]{
|
---|
305 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
306 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
307 | new JSeparator(),
|
---|
308 | new JMenuItem(new SaveAction(this)),
|
---|
309 | new JMenuItem(new SaveAsAction(this)),
|
---|
310 | new JMenuItem(new GpxExportAction(this)),
|
---|
311 | new JSeparator(),
|
---|
312 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
313 | new JSeparator(),
|
---|
314 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | public void setMapPainter(SimplePaintVisitor mapPainter) {
|
---|
319 | this.mapPainter = mapPainter;
|
---|
320 | }
|
---|
321 | }
|
---|