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