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

Last change on this file since 5297 was 5297, checked in by Don-vip, 12 years ago

fix #4493, fix #7750: conflict dialog, nodes and members tab: allow selection via double-click, allow zoom via contexual menu, plus various fix in EDT violations

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