source: josm/trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java@ 2671

Last change on this file since 2671 was 2671, checked in by jttt, 14 years ago

Make MapPaintVisitor independent of SimplePaintVisitor

  • Property svn:eol-style set to native
File size: 27.6 KB
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui.layer;
4
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6import static org.openstreetmap.josm.tools.I18n.marktr;
7import static org.openstreetmap.josm.tools.I18n.tr;
8import static org.openstreetmap.josm.tools.I18n.trn;
9
10import java.awt.AlphaComposite;
11import java.awt.Color;
12import java.awt.Component;
13import java.awt.Composite;
14import java.awt.Graphics2D;
15import java.awt.GridBagLayout;
16import java.awt.Point;
17import java.awt.Rectangle;
18import java.awt.TexturePaint;
19import java.awt.event.ActionEvent;
20import java.awt.geom.Area;
21import java.awt.image.BufferedImage;
22import java.io.File;
23import java.util.ArrayList;
24import java.util.Collection;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.LinkedList;
28import java.util.Set;
29
30import javax.swing.AbstractAction;
31import javax.swing.Icon;
32import javax.swing.JLabel;
33import javax.swing.JMenuItem;
34import javax.swing.JOptionPane;
35import javax.swing.JPanel;
36import javax.swing.JScrollPane;
37import javax.swing.JSeparator;
38import javax.swing.JTextArea;
39
40import org.openstreetmap.josm.Main;
41import org.openstreetmap.josm.actions.RenameLayerAction;
42import org.openstreetmap.josm.command.PurgePrimitivesCommand;
43import org.openstreetmap.josm.data.Bounds;
44import org.openstreetmap.josm.data.conflict.Conflict;
45import org.openstreetmap.josm.data.conflict.ConflictCollection;
46import org.openstreetmap.josm.data.coor.EastNorth;
47import org.openstreetmap.josm.data.coor.LatLon;
48import org.openstreetmap.josm.data.gpx.GpxData;
49import org.openstreetmap.josm.data.gpx.GpxTrack;
50import org.openstreetmap.josm.data.gpx.WayPoint;
51import org.openstreetmap.josm.data.osm.DataSet;
52import org.openstreetmap.josm.data.osm.DataSetMerger;
53import org.openstreetmap.josm.data.osm.DataSource;
54import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
55import org.openstreetmap.josm.data.osm.Node;
56import org.openstreetmap.josm.data.osm.OsmPrimitive;
57import org.openstreetmap.josm.data.osm.Relation;
58import org.openstreetmap.josm.data.osm.Way;
59import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
60import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
61import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintVisitor;
62import org.openstreetmap.josm.data.osm.visitor.paint.PaintVisitor;
63import org.openstreetmap.josm.data.osm.visitor.paint.SimplePaintVisitor;
64import org.openstreetmap.josm.gui.HelpAwareOptionPane;
65import org.openstreetmap.josm.gui.MapView;
66import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
67import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
68import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
69import org.openstreetmap.josm.tools.DateUtils;
70import org.openstreetmap.josm.tools.GBC;
71import org.openstreetmap.josm.tools.ImageProvider;
72
73/**
74 * A layer holding data from a specific dataset.
75 * The data can be fully edited.
76 *
77 * @author imi
78 */
79public class OsmDataLayer extends Layer {
80 static public final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
81 static public final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
82
83 private boolean requiresSaveToFile = false;
84 private boolean requiresUploadToServer = false;
85
86 protected void setRequiresSaveToFile(boolean newValue) {
87 boolean oldValue = requiresSaveToFile;
88 requiresSaveToFile = newValue;
89 if (oldValue != newValue) {
90 propertyChangeSupport.firePropertyChange(REQUIRES_SAVE_TO_DISK_PROP, oldValue, newValue);
91 }
92 }
93
94 protected void setRequiresUploadToServer(boolean newValue) {
95 boolean oldValue = requiresUploadToServer;
96 requiresUploadToServer = newValue;
97 if (oldValue != newValue) {
98 propertyChangeSupport.firePropertyChange(REQUIRES_UPLOAD_TO_SERVER_PROP, oldValue, newValue);
99 }
100 }
101
102 /** the global counter for created data layers */
103 static private int dataLayerCounter = 0;
104
105 /**
106 * Replies a new unique name for a data layer
107 *
108 * @return a new unique name for a data layer
109 */
110 static public String createNewName() {
111 dataLayerCounter++;
112 return tr("Data Layer {0}", dataLayerCounter);
113 }
114
115 public final static class DataCountVisitor extends AbstractVisitor {
116 public int nodes;
117 public int ways;
118 public int relations;
119 public int deletedNodes;
120 public int deletedWays;
121 public int deletedRelations;
122
123 public void visit(final Node n) {
124 nodes++;
125 if (n.isDeleted()) {
126 deletedNodes++;
127 }
128 }
129
130 public void visit(final Way w) {
131 ways++;
132 if (w.isDeleted()) {
133 deletedWays++;
134 }
135 }
136
137 public void visit(final Relation r) {
138 relations++;
139 if (r.isDeleted()) {
140 deletedRelations++;
141 }
142 }
143 }
144
145 public interface CommandQueueListener {
146 void commandChanged(int queueSize, int redoSize);
147 }
148
149 /**
150 * The data behind this layer.
151 */
152 public final DataSet data;
153
154 /**
155 * the collection of conflicts detected in this layer
156 */
157 private ConflictCollection conflicts;
158
159 public final LinkedList<DataChangeListener> listenerDataChanged = new LinkedList<DataChangeListener>();
160
161 /**
162 * a paint texture for non-downloaded area
163 */
164 private static TexturePaint hatched;
165
166 static {
167 createHatchTexture();
168 }
169
170 /**
171 * Initialize the hatch pattern used to paint the non-downloaded area
172 */
173 public static void createHatchTexture() {
174 BufferedImage bi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB);
175 Graphics2D big = bi.createGraphics();
176 big.setColor(Main.pref.getColor(marktr("background"), Color.BLACK));
177 Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
178 big.setComposite(comp);
179 big.fillRect(0,0,15,15);
180 big.setColor(Main.pref.getColor(marktr("outside downloaded area"), Color.YELLOW));
181 big.drawLine(0,15,15,0);
182 Rectangle r = new Rectangle(0, 0, 15,15);
183 hatched = new TexturePaint(bi, r);
184 }
185
186 /**
187 * Construct a OsmDataLayer.
188 */
189 public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
190 super(name);
191 this.data = data;
192 this.setAssociatedFile(associatedFile);
193 conflicts = new ConflictCollection();
194 }
195
196 /**
197 * TODO: @return Return a dynamic drawn icon of the map data. The icon is
198 * updated by a background thread to not disturb the running programm.
199 */
200 @Override public Icon getIcon() {
201 return ImageProvider.get("layer", "osmdata_small");
202 }
203
204 /**
205 * Draw all primitives in this layer but do not draw modified ones (they
206 * are drawn by the edit layer).
207 * Draw nodes last to overlap the ways they belong to.
208 */
209 @Override public void paint(final Graphics2D g, final MapView mv, Bounds box) {
210 boolean active = mv.getActiveLayer() == this;
211 boolean inactive = !active && Main.pref.getBoolean("draw.data.inactive_color", true);
212 boolean virtual = !inactive && mv.isVirtualNodesEnabled();
213
214 // draw the hatched area for non-downloaded region. only draw if we're the active
215 // and bounds are defined; don't draw for inactive layers or loaded GPX files etc
216 if (active && Main.pref.getBoolean("draw.data.downloaded_area", true) && !data.dataSources.isEmpty()) {
217 // initialize area with current viewport
218 Rectangle b = mv.getBounds();
219 // on some platforms viewport bounds seem to be offset from the left,
220 // over-grow it just to be sure
221 b.grow(100, 100);
222 Area a = new Area(b);
223
224 // now succesively subtract downloaded areas
225 for (DataSource src : data.dataSources) {
226 if (src.bounds != null && !src.bounds.getMin().equals(src.bounds.getMax())) {
227 EastNorth en1 = mv.getProjection().latlon2eastNorth(src.bounds.getMin());
228 EastNorth en2 = mv.getProjection().latlon2eastNorth(src.bounds.getMax());
229 Point p1 = mv.getPoint(en1);
230 Point p2 = mv.getPoint(en2);
231 Rectangle r = new Rectangle(Math.min(p1.x, p2.x),Math.min(p1.y, p2.y),Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
232 a.subtract(new Area(r));
233 }
234 }
235
236 // paint remainder
237 g.setPaint(hatched);
238 g.fill(a);
239 }
240
241 PaintVisitor painter;
242 if (Main.pref.getBoolean("draw.wireframe")) {
243 painter = new SimplePaintVisitor();
244 } else {
245 painter = new MapPaintVisitor();
246 }
247 painter.setGraphics(g);
248 painter.setNavigatableComponent(mv);
249 painter.setInactive(inactive);
250 painter.visitAll(data, virtual, box);
251 Main.map.conflictDialog.paintConflicts(g, mv);
252 }
253
254 @Override public String getToolTipText() {
255 int nodes = undeletedSize(data.getNodes());
256 int ways = undeletedSize(data.getWays());
257
258 String tool = trn("{0} node", "{0} nodes", nodes, nodes)+", ";
259 tool += trn("{0} way", "{0} ways", ways, ways);
260
261 if (data.getVersion() != null) {
262 tool += ", " + tr("version {0}", data.getVersion());
263 }
264 File f = getAssociatedFile();
265 if (f != null) {
266 tool = "<html>"+tool+"<br>"+f.getPath()+"</html>";
267 }
268 return tool;
269 }
270
271 @Override public void mergeFrom(final Layer from) {
272 mergeFrom(((OsmDataLayer)from).data);
273 }
274
275 /**
276 * merges the primitives in dataset <code>from</code> into the dataset of
277 * this layer
278 *
279 * @param from the source data set
280 */
281 public void mergeFrom(final DataSet from) {
282 final DataSetMerger visitor = new DataSetMerger(data,from);
283 visitor.merge();
284
285 Area a = data.getDataSourceArea();
286
287 // copy the merged layer's data source info;
288 // only add source rectangles if they are not contained in the
289 // layer already.
290 for (DataSource src : from.dataSources) {
291 if (a == null || !a.contains(src.bounds.asRect())) {
292 data.dataSources.add(src);
293 }
294 }
295
296 // copy the merged layer's API version, downgrade if required
297 if (data.getVersion() == null) {
298 data.setVersion(from.getVersion());
299 } else if ("0.5".equals(data.getVersion()) ^ "0.5".equals(from.getVersion())) {
300 System.err.println(tr("Warning: mixing 0.6 and 0.5 data results in version 0.5"));
301 data.setVersion("0.5");
302 }
303
304 int numNewConflicts = 0;
305 for (Conflict<?> c : visitor.getConflicts()) {
306 if (!conflicts.hasConflict(c)) {
307 numNewConflicts++;
308 conflicts.add(c);
309 }
310 }
311 PurgePrimitivesCommand cmd = buildPurgeCommand();
312 if (cmd != null) {
313 Main.main.undoRedo.add(cmd);
314 }
315 fireDataChange();
316 // repaint to make sure new data is displayed properly.
317 Main.map.mapView.repaint();
318 warnNumNewConflicts(
319 numNewConflicts,
320 cmd == null ? 0 : cmd.getPurgedPrimitives().size()
321 );
322 }
323
324 /**
325 * Warns the user about the number of detected conflicts
326 *
327 * @param numNewConflicts the number of detected conflicts
328 * @param numPurgedPrimitives the number of automatically purged objects
329 */
330 protected void warnNumNewConflicts(int numNewConflicts, int numPurgedPrimitives) {
331 if (numNewConflicts == 0 && numPurgedPrimitives == 0) return;
332
333 String msg1 = trn(
334 "There was {0} conflict detected.",
335 "There were {0} conflicts detected.",
336 numNewConflicts,
337 numNewConflicts
338 );
339 String msg2 = trn(
340 "{0} conflict has been <strong>resolved automatically</strong> by purging {0} object<br>from the local dataset because it is deleted on the server.",
341 "{0} conflicts have been <strong>resolved automatically</strong> by purging {0} objects<br> from the local dataset because they are deleted on the server.",
342 numPurgedPrimitives,
343 numPurgedPrimitives
344 );
345 int numRemainingConflicts = numNewConflicts - numPurgedPrimitives;
346 String msg3 = "";
347 if (numRemainingConflicts >0) {
348 msg3 = trn(
349 "{0} conflict remains to be resolved.<br><br>Please open the Conflict List Dialog and manually resolve it.",
350 "{0} conflicts remain to be resolved.<br><br>Please open the Conflict List Dialog and manually resolve them.",
351 numRemainingConflicts,
352 numRemainingConflicts
353 );
354 }
355
356 StringBuffer sb = new StringBuffer();
357 sb.append("<html>").append(msg1);
358 if (numPurgedPrimitives > 0) {
359 sb.append("<br>").append(msg2);
360 }
361 if (numRemainingConflicts > 0) {
362 sb.append("<br>").append(msg3);
363 }
364 sb.append("</html>");
365 if (numNewConflicts > 0) {
366 ButtonSpec[] options = new ButtonSpec[] {
367 new ButtonSpec(
368 tr("OK"),
369 ImageProvider.get("ok"),
370 tr("Click to close this dialog and continue editing"),
371 null /* no specific help */
372 )
373 };
374 HelpAwareOptionPane.showOptionDialog(
375 Main.parent,
376 sb.toString(),
377 tr("Conflicts detected"),
378 JOptionPane.WARNING_MESSAGE,
379 null, /* no icon */
380 options,
381 options[0],
382 ht("/Concepts/Conflict#WarningAboutDetectedConflicts")
383 );
384 }
385 }
386
387 /**
388 * Builds the purge command for primitives which can be purged automatically
389 * from the local dataset because they've been deleted on the
390 * server.
391 *
392 * @return the purge command. <code>null</code> if no primitives have to
393 * be purged
394 */
395 protected PurgePrimitivesCommand buildPurgeCommand() {
396 ArrayList<OsmPrimitive> toPurge = new ArrayList<OsmPrimitive>();
397 conflictLoop: for (Conflict<?> c: conflicts) {
398 if (c.getMy().isDeleted() && !c.getTheir().isVisible()) {
399 // Local and server version of the primitive are deleted. We
400 // can purge it from the local dataset.
401 //
402 toPurge.add(c.getMy());
403 } else if (!c.getMy().isModified() && ! c.getTheir().isVisible()) {
404 // We purge deleted *ways* and *relations* automatically if they are
405 // deleted on the server and if they aren't modified in the local
406 // dataset.
407 //
408 if (c.getMy() instanceof Way || c.getMy() instanceof Relation) {
409 toPurge.add(c.getMy());
410 continue conflictLoop;
411 }
412 // We only purge nodes if they aren't part of a modified way.
413 // Otherwise the number of nodes of a modified way could drop
414 // below 2 and we would lose the modified data when the way
415 // gets purged.
416 //
417 for (OsmPrimitive parent: c.getMy().getReferrers()) {
418 if (parent.isModified() && parent instanceof Way) {
419 continue conflictLoop;
420 }
421 }
422 toPurge.add(c.getMy());
423 }
424 }
425 if (toPurge.isEmpty()) return null;
426 PurgePrimitivesCommand cmd = new PurgePrimitivesCommand(this, toPurge);
427 return cmd;
428 }
429
430 @Override public boolean isMergable(final Layer other) {
431 return other instanceof OsmDataLayer;
432 }
433
434 @Override public void visitBoundingBox(final BoundingXYVisitor v) {
435 for (final Node n: data.getNodes()) {
436 if (n.isUsable()) {
437 v.visit(n);
438 }
439 }
440 }
441
442 /**
443 * Clean out the data behind the layer. This means clearing the redo/undo lists,
444 * really deleting all deleted objects and reset the modified flags. This should
445 * be done after an upload, even after a partial upload.
446 *
447 * @param processed A list of all objects that were actually uploaded.
448 * May be <code>null</code>, which means nothing has been uploaded
449 */
450 public void cleanupAfterUpload(final Collection<OsmPrimitive> processed) {
451 // return immediately if an upload attempt failed
452 if (processed == null || processed.isEmpty())
453 return;
454
455 Main.main.undoRedo.clean(this);
456
457 // if uploaded, clean the modified flags as well
458 final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
459 data.clenupDeletedPrimitives();
460 for (final Iterator<Node> it = data.getNodes().iterator(); it.hasNext();) {
461 cleanIterator(it, processedSet);
462 }
463 for (final Iterator<Way> it = data.getWays().iterator(); it.hasNext();) {
464 cleanIterator(it, processedSet);
465 }
466 for (final Iterator<Relation> it = data.getRelations().iterator(); it.hasNext();) {
467 cleanIterator(it, processedSet);
468 }
469 }
470
471 /**
472 * Clean the modified flag for the given iterator over a collection if it is in the
473 * list of processed entries.
474 *
475 * @param it The iterator to change the modified and remove the items if deleted.
476 * @param processed A list of all objects that have been successfully progressed.
477 * If the object in the iterator is not in the list, nothing will be changed on it.
478 */
479 private void cleanIterator(final Iterator<? extends OsmPrimitive> it, final Collection<OsmPrimitive> processed) {
480 final OsmPrimitive osm = it.next();
481 if (!processed.remove(osm))
482 return;
483 osm.setModified(false);
484 }
485
486 /**
487 * @return The number of not-deleted and visible primitives in the list.
488 */
489 private int undeletedSize(final Collection<? extends OsmPrimitive> list) {
490 int size = 0;
491 for (final OsmPrimitive osm : list)
492 if (!osm.isDeleted() && osm.isVisible()) {
493 size++;
494 }
495 return size;
496 }
497
498 @Override public Object getInfoComponent() {
499 final DataCountVisitor counter = new DataCountVisitor();
500 for (final OsmPrimitive osm : data.allPrimitives()) {
501 osm.visit(counter);
502 }
503 final JPanel p = new JPanel(new GridBagLayout());
504
505 String nodeText = trn("{0} node", "{0} nodes", counter.nodes, counter.nodes);
506 if (counter.deletedNodes > 0) {
507 nodeText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedNodes, counter.deletedNodes)+")";
508 }
509
510 String wayText = trn("{0} way", "{0} ways", counter.ways, counter.ways);
511 if (counter.deletedWays > 0) {
512 wayText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedWays, counter.deletedWays)+")";
513 }
514
515 String relationText = trn("{0} relation", "{0} relations", counter.relations, counter.relations);
516 if (counter.deletedRelations > 0) {
517 relationText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedRelations, counter.deletedRelations)+")";
518 }
519
520 p.add(new JLabel(tr("{0} consists of:", getName())), GBC.eol());
521 p.add(new JLabel(nodeText, ImageProvider.get("data", "node"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
522 p.add(new JLabel(wayText, ImageProvider.get("data", "way"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
523 p.add(new JLabel(relationText, ImageProvider.get("data", "relation"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
524 p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))));
525
526 return p;
527 }
528
529 @Override public Component[] getMenuEntries() {
530 if (Main.applet)
531 return new Component[]{
532 new JMenuItem(LayerListDialog.getInstance().createActivateLayerAction(this)),
533 new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
534 new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
535 new JSeparator(),
536 new JMenuItem(LayerListDialog.getInstance().createMergeLayerAction(this)),
537 new JSeparator(),
538 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)),
539 new JMenuItem(new ConsistencyTestAction()),
540 new JSeparator(),
541 new JMenuItem(new LayerListPopup.InfoAction(this))};
542 return new Component[]{
543 new JMenuItem(LayerListDialog.getInstance().createActivateLayerAction(this)),
544 new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
545 new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
546 new JSeparator(),
547 new JMenuItem(LayerListDialog.getInstance().createMergeLayerAction(this)),
548 new JMenuItem(new LayerSaveAction(this)),
549 new JMenuItem(new LayerSaveAsAction(this)),
550 new JMenuItem(new LayerGpxExportAction(this)),
551 new JMenuItem(new ConvertToGpxLayerAction()),
552 new JSeparator(),
553 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)),
554 new JMenuItem(new ConsistencyTestAction()),
555 new JSeparator(),
556 new JMenuItem(new LayerListPopup.InfoAction(this))};
557 }
558
559 public void fireDataChange() {
560 setRequiresSaveToFile(true);
561 setRequiresUploadToServer(true);
562 for (DataChangeListener dcl : listenerDataChanged) {
563 dcl.dataChanged(this);
564 }
565 }
566
567 public static GpxData toGpxData(DataSet data, File file) {
568 GpxData gpxData = new GpxData();
569 gpxData.storageFile = file;
570 HashSet<Node> doneNodes = new HashSet<Node>();
571 for (Way w : data.getWays()) {
572 if (!w.isUsable()) {
573 continue;
574 }
575 GpxTrack trk = new GpxTrack();
576 gpxData.tracks.add(trk);
577
578 if (w.get("name") != null) {
579 trk.attr.put("name", w.get("name"));
580 }
581
582 ArrayList<WayPoint> trkseg = null;
583 for (Node n : w.getNodes()) {
584 if (!n.isUsable()) {
585 trkseg = null;
586 continue;
587 }
588 if (trkseg == null) {
589 trkseg = new ArrayList<WayPoint>();
590 trk.trackSegs.add(trkseg);
591 }
592 if (!n.isTagged()) {
593 doneNodes.add(n);
594 }
595 WayPoint wpt = new WayPoint(n.getCoor());
596 if (!n.isTimestampEmpty()) {
597 wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
598 wpt.setTime();
599 }
600 trkseg.add(wpt);
601 }
602 }
603
604 // what is this loop meant to do? it creates waypoints but never
605 // records them?
606 for (Node n : data.getNodes()) {
607 if (n.isIncomplete() || n.isDeleted() || doneNodes.contains(n)) {
608 continue;
609 }
610 WayPoint wpt = new WayPoint(n.getCoor());
611 if (!n.isTimestampEmpty()) {
612 wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
613 wpt.setTime();
614 }
615 String name = n.get("name");
616 if (name != null) {
617 wpt.attr.put("name", name);
618 }
619 }
620 return gpxData;
621 }
622
623 public GpxData toGpxData() {
624 return toGpxData(data, getAssociatedFile());
625 }
626
627 public class ConvertToGpxLayerAction extends AbstractAction {
628 public ConvertToGpxLayerAction() {
629 super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx"));
630 }
631 public void actionPerformed(ActionEvent e) {
632 Main.main.addLayer(new GpxLayer(toGpxData(), tr("Converted from: {0}", getName())));
633 Main.main.removeLayer(OsmDataLayer.this);
634 }
635 }
636
637 public boolean containsPoint(LatLon coor) {
638 // we'll assume that if this has no data sources
639 // that it also has no borders
640 if (this.data.dataSources.isEmpty())
641 return true;
642
643 boolean layer_bounds_point = false;
644 for (DataSource src : this.data.dataSources) {
645 if (src.bounds.contains(coor)) {
646 layer_bounds_point = true;
647 break;
648 }
649 }
650 return layer_bounds_point;
651 }
652
653 /**
654 * replies the set of conflicts currently managed in this layer
655 *
656 * @return the set of conflicts currently managed in this layer
657 */
658 public ConflictCollection getConflicts() {
659 return conflicts;
660 }
661
662 /**
663 * Replies true if the data managed by this layer needs to be uploaded to
664 * the server because it contains at least one modified primitive.
665 *
666 * @return true if the data managed by this layer needs to be uploaded to
667 * the server because it contains at least one modified primitive; false,
668 * otherwise
669 */
670 public boolean requiresUploadToServer() {
671 return requiresUploadToServer;
672 }
673
674 /**
675 * Replies true if the data managed by this layer needs to be saved to
676 * a file. Only replies true if a file is assigned to this layer and
677 * if the data managed by this layer has been modified since the last
678 * save operation to the file.
679 *
680 * @return true if the data managed by this layer needs to be saved to
681 * a file
682 */
683 public boolean requiresSaveToFile() {
684 return getAssociatedFile() != null && requiresSaveToFile;
685 }
686
687 /**
688 * Initializes the layer after a successful load of OSM data from a file
689 *
690 */
691 public void onPostLoadFromFile() {
692 setRequiresSaveToFile(false);
693 setRequiresUploadToServer(data.isModified());
694 }
695
696 public void onPostDownloadFromServer() {
697 setRequiresSaveToFile(true);
698 setRequiresUploadToServer(data.isModified());
699 }
700
701 /**
702 * Initializes the layer after a successful save of OSM data to a file
703 *
704 */
705 public void onPostSaveToFile() {
706 setRequiresSaveToFile(false);
707 setRequiresUploadToServer(data.isModified());
708 }
709
710 /**
711 * Initializes the layer after a successful upload to the server
712 *
713 */
714 public void onPostUploadToServer() {
715 setRequiresUploadToServer(data.isModified());
716 // keep requiresSaveToDisk unchanged
717 }
718
719 private class ConsistencyTestAction extends AbstractAction {
720
721 public ConsistencyTestAction() {
722 super(tr("Dataset consistency test"));
723 }
724
725 public void actionPerformed(ActionEvent e) {
726 String result = DatasetConsistencyTest.runTests(data);
727 if (result.length() == 0) {
728 JOptionPane.showMessageDialog(Main.parent, tr("No problems found"));
729 } else {
730 JPanel p = new JPanel(new GridBagLayout());
731 p.add(new JLabel(tr("Following problems found:")), GBC.eol());
732 JTextArea info = new JTextArea(result, 20, 60);
733 info.setCaretPosition(0);
734 info.setEditable(false);
735 p.add(new JScrollPane(info), GBC.eop());
736
737 JOptionPane.showMessageDialog(Main.parent, p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
738 }
739 }
740
741 }
742}
Note: See TracBrowser for help on using the repository browser.