1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.AlphaComposite;
|
---|
10 | import java.awt.Color;
|
---|
11 | import java.awt.Composite;
|
---|
12 | import java.awt.Graphics2D;
|
---|
13 | import java.awt.GraphicsEnvironment;
|
---|
14 | import java.awt.GridBagLayout;
|
---|
15 | import java.awt.Rectangle;
|
---|
16 | import java.awt.TexturePaint;
|
---|
17 | import java.awt.event.ActionEvent;
|
---|
18 | import java.awt.geom.Area;
|
---|
19 | import java.awt.geom.Path2D;
|
---|
20 | import java.awt.geom.Rectangle2D;
|
---|
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.Collections;
|
---|
27 | import java.util.HashMap;
|
---|
28 | import java.util.HashSet;
|
---|
29 | import java.util.LinkedHashMap;
|
---|
30 | import java.util.List;
|
---|
31 | import java.util.Map;
|
---|
32 | import java.util.Set;
|
---|
33 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
34 | import java.util.concurrent.atomic.AtomicBoolean;
|
---|
35 | import java.util.concurrent.atomic.AtomicInteger;
|
---|
36 | import java.util.regex.Pattern;
|
---|
37 |
|
---|
38 | import javax.swing.AbstractAction;
|
---|
39 | import javax.swing.Action;
|
---|
40 | import javax.swing.Icon;
|
---|
41 | import javax.swing.JLabel;
|
---|
42 | import javax.swing.JOptionPane;
|
---|
43 | import javax.swing.JPanel;
|
---|
44 | import javax.swing.JScrollPane;
|
---|
45 |
|
---|
46 | import org.openstreetmap.josm.Main;
|
---|
47 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
48 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
49 | import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction;
|
---|
50 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
51 | import org.openstreetmap.josm.data.Bounds;
|
---|
52 | import org.openstreetmap.josm.data.DataSource;
|
---|
53 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
54 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
55 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
56 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
57 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
58 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
59 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
60 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
61 | import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
|
---|
62 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
63 | import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
|
---|
64 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
65 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
66 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
67 | import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
|
---|
68 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
---|
69 | import org.openstreetmap.josm.data.osm.HighlightUpdateListener;
|
---|
70 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
71 | import org.openstreetmap.josm.data.osm.Node;
|
---|
72 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
73 | import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
|
---|
74 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
75 | import org.openstreetmap.josm.data.osm.UploadPolicy;
|
---|
76 | import org.openstreetmap.josm.data.osm.Way;
|
---|
77 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
78 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
|
---|
79 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
|
---|
80 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
81 | import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
|
---|
82 | import org.openstreetmap.josm.data.osm.visitor.paint.MapRendererFactory;
|
---|
83 | import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
|
---|
84 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
85 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
86 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
87 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
88 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
89 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
90 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
91 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
92 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
93 | import org.openstreetmap.josm.gui.MapView;
|
---|
94 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
95 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
96 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
97 | import org.openstreetmap.josm.gui.io.AbstractIOTask;
|
---|
98 | import org.openstreetmap.josm.gui.io.AbstractUploadDialog;
|
---|
99 | import org.openstreetmap.josm.gui.io.UploadDialog;
|
---|
100 | import org.openstreetmap.josm.gui.io.UploadLayerTask;
|
---|
101 | import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
|
---|
102 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
103 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
104 | import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
---|
105 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
106 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
---|
107 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
108 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
109 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
110 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
111 | import org.openstreetmap.josm.tools.GBC;
|
---|
112 | import org.openstreetmap.josm.tools.ImageOverlay;
|
---|
113 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
114 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
115 | import org.openstreetmap.josm.tools.Logging;
|
---|
116 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * A layer that holds OSM data from a specific dataset.
|
---|
120 | * The data can be fully edited.
|
---|
121 | *
|
---|
122 | * @author imi
|
---|
123 | * @since 17
|
---|
124 | */
|
---|
125 | public class OsmDataLayer extends AbstractModifiableLayer implements Listener, DataSelectionListener, HighlightUpdateListener {
|
---|
126 | private static final int HATCHED_SIZE = 15;
|
---|
127 | /** Property used to know if this layer has to be saved on disk */
|
---|
128 | public static final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
|
---|
129 | /** Property used to know if this layer has to be uploaded */
|
---|
130 | public static final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
|
---|
131 |
|
---|
132 | private boolean requiresSaveToFile;
|
---|
133 | private boolean requiresUploadToServer;
|
---|
134 | /** Flag used to know if the layer is being uploaded */
|
---|
135 | private final AtomicBoolean isUploadInProgress = new AtomicBoolean(false);
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * List of validation errors in this layer.
|
---|
139 | * @since 3669
|
---|
140 | */
|
---|
141 | public final List<TestError> validationErrors = new ArrayList<>();
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * The default number of relations in the recent relations cache.
|
---|
145 | * @see #getRecentRelations()
|
---|
146 | */
|
---|
147 | public static final int DEFAULT_RECENT_RELATIONS_NUMBER = 20;
|
---|
148 | /**
|
---|
149 | * The number of relations to use in the recent relations cache.
|
---|
150 | * @see #getRecentRelations()
|
---|
151 | */
|
---|
152 | public static final IntegerProperty PROPERTY_RECENT_RELATIONS_NUMBER = new IntegerProperty("properties.last-closed-relations-size",
|
---|
153 | DEFAULT_RECENT_RELATIONS_NUMBER);
|
---|
154 | /**
|
---|
155 | * The extension that should be used when saving the OSM file.
|
---|
156 | */
|
---|
157 | public static final StringProperty PROPERTY_SAVE_EXTENSION = new StringProperty("save.extension.osm", "osm");
|
---|
158 |
|
---|
159 | private static final NamedColorProperty PROPERTY_BACKGROUND_COLOR = new NamedColorProperty(marktr("background"), Color.BLACK);
|
---|
160 | private static final NamedColorProperty PROPERTY_OUTSIDE_COLOR = new NamedColorProperty(marktr("outside downloaded area"), Color.YELLOW);
|
---|
161 |
|
---|
162 | /** List of recent relations */
|
---|
163 | private final Map<Relation, Void> recentRelations = new LruCache(PROPERTY_RECENT_RELATIONS_NUMBER.get()+1);
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Returns list of recently closed relations or null if none.
|
---|
167 | * @return list of recently closed relations or <code>null</code> if none
|
---|
168 | * @since 12291 (signature)
|
---|
169 | * @since 9668
|
---|
170 | */
|
---|
171 | public List<Relation> getRecentRelations() {
|
---|
172 | ArrayList<Relation> list = new ArrayList<>(recentRelations.keySet());
|
---|
173 | Collections.reverse(list);
|
---|
174 | return list;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Adds recently closed relation.
|
---|
179 | * @param relation new entry for the list of recently closed relations
|
---|
180 | * @see #PROPERTY_RECENT_RELATIONS_NUMBER
|
---|
181 | * @since 9668
|
---|
182 | */
|
---|
183 | public void setRecentRelation(Relation relation) {
|
---|
184 | recentRelations.put(relation, null);
|
---|
185 | MapFrame map = MainApplication.getMap();
|
---|
186 | if (map != null && map.relationListDialog != null) {
|
---|
187 | map.relationListDialog.enableRecentRelations();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Remove relation from list of recent relations.
|
---|
193 | * @param relation relation to remove
|
---|
194 | * @since 9668
|
---|
195 | */
|
---|
196 | public void removeRecentRelation(Relation relation) {
|
---|
197 | recentRelations.remove(relation);
|
---|
198 | MapFrame map = MainApplication.getMap();
|
---|
199 | if (map != null && map.relationListDialog != null) {
|
---|
200 | map.relationListDialog.enableRecentRelations();
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | protected void setRequiresSaveToFile(boolean newValue) {
|
---|
205 | boolean oldValue = requiresSaveToFile;
|
---|
206 | requiresSaveToFile = newValue;
|
---|
207 | if (oldValue != newValue) {
|
---|
208 | GuiHelper.runInEDT(() ->
|
---|
209 | propertyChangeSupport.firePropertyChange(REQUIRES_SAVE_TO_DISK_PROP, oldValue, newValue)
|
---|
210 | );
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | protected void setRequiresUploadToServer(boolean newValue) {
|
---|
215 | boolean oldValue = requiresUploadToServer;
|
---|
216 | requiresUploadToServer = newValue;
|
---|
217 | if (oldValue != newValue) {
|
---|
218 | GuiHelper.runInEDT(() ->
|
---|
219 | propertyChangeSupport.firePropertyChange(REQUIRES_UPLOAD_TO_SERVER_PROP, oldValue, newValue)
|
---|
220 | );
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** the global counter for created data layers */
|
---|
225 | private static final AtomicInteger dataLayerCounter = new AtomicInteger();
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Replies a new unique name for a data layer
|
---|
229 | *
|
---|
230 | * @return a new unique name for a data layer
|
---|
231 | */
|
---|
232 | public static String createNewName() {
|
---|
233 | return createLayerName(dataLayerCounter.incrementAndGet());
|
---|
234 | }
|
---|
235 |
|
---|
236 | static String createLayerName(Object arg) {
|
---|
237 | return tr("Data Layer {0}", arg);
|
---|
238 | }
|
---|
239 |
|
---|
240 | static final class LruCache extends LinkedHashMap<Relation, Void> {
|
---|
241 | private static final long serialVersionUID = 1L;
|
---|
242 | LruCache(int initialCapacity) {
|
---|
243 | super(initialCapacity, 1.1f, true);
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | protected boolean removeEldestEntry(Map.Entry<Relation, Void> eldest) {
|
---|
248 | return size() > PROPERTY_RECENT_RELATIONS_NUMBER.get();
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * A listener that counts the number of primitives it encounters
|
---|
254 | */
|
---|
255 | public static final class DataCountVisitor implements OsmPrimitiveVisitor {
|
---|
256 | /**
|
---|
257 | * Nodes that have been visited
|
---|
258 | */
|
---|
259 | public int nodes;
|
---|
260 | /**
|
---|
261 | * Ways that have been visited
|
---|
262 | */
|
---|
263 | public int ways;
|
---|
264 | /**
|
---|
265 | * Relations that have been visited
|
---|
266 | */
|
---|
267 | public int relations;
|
---|
268 | /**
|
---|
269 | * Deleted nodes that have been visited
|
---|
270 | */
|
---|
271 | public int deletedNodes;
|
---|
272 | /**
|
---|
273 | * Deleted ways that have been visited
|
---|
274 | */
|
---|
275 | public int deletedWays;
|
---|
276 | /**
|
---|
277 | * Deleted relations that have been visited
|
---|
278 | */
|
---|
279 | public int deletedRelations;
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | public void visit(final Node n) {
|
---|
283 | nodes++;
|
---|
284 | if (n.isDeleted()) {
|
---|
285 | deletedNodes++;
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Override
|
---|
290 | public void visit(final Way w) {
|
---|
291 | ways++;
|
---|
292 | if (w.isDeleted()) {
|
---|
293 | deletedWays++;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public void visit(final Relation r) {
|
---|
299 | relations++;
|
---|
300 | if (r.isDeleted()) {
|
---|
301 | deletedRelations++;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Listener called when a state of this layer has changed.
|
---|
308 | * @since 10600 (functional interface)
|
---|
309 | */
|
---|
310 | @FunctionalInterface
|
---|
311 | public interface LayerStateChangeListener {
|
---|
312 | /**
|
---|
313 | * Notifies that the "upload discouraged" (upload=no) state has changed.
|
---|
314 | * @param layer The layer that has been modified
|
---|
315 | * @param newValue The new value of the state
|
---|
316 | */
|
---|
317 | void uploadDiscouragedChanged(OsmDataLayer layer, boolean newValue);
|
---|
318 | }
|
---|
319 |
|
---|
320 | private final CopyOnWriteArrayList<LayerStateChangeListener> layerStateChangeListeners = new CopyOnWriteArrayList<>();
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Adds a layer state change listener
|
---|
324 | *
|
---|
325 | * @param listener the listener. Ignored if null or already registered.
|
---|
326 | * @since 5519
|
---|
327 | */
|
---|
328 | public void addLayerStateChangeListener(LayerStateChangeListener listener) {
|
---|
329 | if (listener != null) {
|
---|
330 | layerStateChangeListeners.addIfAbsent(listener);
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Removes a layer state change listener
|
---|
336 | *
|
---|
337 | * @param listener the listener. Ignored if null or already registered.
|
---|
338 | * @since 10340
|
---|
339 | */
|
---|
340 | public void removeLayerStateChangeListener(LayerStateChangeListener listener) {
|
---|
341 | layerStateChangeListeners.remove(listener);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * The data behind this layer.
|
---|
346 | */
|
---|
347 | public final DataSet data;
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * a texture for non-downloaded area
|
---|
351 | */
|
---|
352 | private static volatile BufferedImage hatched;
|
---|
353 |
|
---|
354 | static {
|
---|
355 | createHatchTexture();
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Replies background color for downloaded areas.
|
---|
360 | * @return background color for downloaded areas. Black by default
|
---|
361 | */
|
---|
362 | public static Color getBackgroundColor() {
|
---|
363 | return PROPERTY_BACKGROUND_COLOR.get();
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Replies background color for non-downloaded areas.
|
---|
368 | * @return background color for non-downloaded areas. Yellow by default
|
---|
369 | */
|
---|
370 | public static Color getOutsideColor() {
|
---|
371 | return PROPERTY_OUTSIDE_COLOR.get();
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Initialize the hatch pattern used to paint the non-downloaded area
|
---|
376 | */
|
---|
377 | public static void createHatchTexture() {
|
---|
378 | BufferedImage bi = new BufferedImage(HATCHED_SIZE, HATCHED_SIZE, BufferedImage.TYPE_INT_ARGB);
|
---|
379 | Graphics2D big = bi.createGraphics();
|
---|
380 | big.setColor(getBackgroundColor());
|
---|
381 | Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
|
---|
382 | big.setComposite(comp);
|
---|
383 | big.fillRect(0, 0, HATCHED_SIZE, HATCHED_SIZE);
|
---|
384 | big.setColor(getOutsideColor());
|
---|
385 | big.drawLine(-1, 6, 6, -1);
|
---|
386 | big.drawLine(4, 16, 16, 4);
|
---|
387 | hatched = bi;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Construct a new {@code OsmDataLayer}.
|
---|
392 | * @param data OSM data
|
---|
393 | * @param name Layer name
|
---|
394 | * @param associatedFile Associated .osm file (can be null)
|
---|
395 | */
|
---|
396 | public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
|
---|
397 | super(name);
|
---|
398 | CheckParameterUtil.ensureParameterNotNull(data, "data");
|
---|
399 | this.data = data;
|
---|
400 | this.data.setName(name);
|
---|
401 | this.setAssociatedFile(associatedFile);
|
---|
402 | data.addDataSetListener(new DataSetListenerAdapter(this));
|
---|
403 | data.addDataSetListener(MultipolygonCache.getInstance());
|
---|
404 | data.addHighlightUpdateListener(this);
|
---|
405 | data.addSelectionListener(this);
|
---|
406 | if (name != null && name.startsWith(createLayerName("")) && Character.isDigit(
|
---|
407 | (name.substring(createLayerName("").length()) + "XX" /*avoid StringIndexOutOfBoundsException*/).charAt(1))) {
|
---|
408 | while (AlphanumComparator.getInstance().compare(createLayerName(dataLayerCounter), name) < 0) {
|
---|
409 | final int i = dataLayerCounter.incrementAndGet();
|
---|
410 | if (i > 1_000_000) {
|
---|
411 | break; // to avoid looping in unforeseen case
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Returns the {@link DataSet} behind this layer.
|
---|
419 | * @return the {@link DataSet} behind this layer.
|
---|
420 | * @since 13558
|
---|
421 | */
|
---|
422 | public DataSet getDataSet() {
|
---|
423 | return data;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Return the image provider to get the base icon
|
---|
428 | * @return image provider class which can be modified
|
---|
429 | * @since 8323
|
---|
430 | */
|
---|
431 | protected ImageProvider getBaseIconProvider() {
|
---|
432 | return new ImageProvider("layer", "osmdata_small");
|
---|
433 | }
|
---|
434 |
|
---|
435 | @Override
|
---|
436 | public Icon getIcon() {
|
---|
437 | ImageProvider base = getBaseIconProvider().setMaxSize(ImageSizes.LAYER);
|
---|
438 | if (data.getDownloadPolicy() != null && data.getDownloadPolicy() != DownloadPolicy.NORMAL) {
|
---|
439 | base.addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.0, 1.0, 0.5));
|
---|
440 | }
|
---|
441 | if (data.getUploadPolicy() != null && data.getUploadPolicy() != UploadPolicy.NORMAL) {
|
---|
442 | base.addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0));
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (isUploadInProgress()) {
|
---|
446 | // If the layer is being uploaded then change the default icon to a clock
|
---|
447 | base = new ImageProvider("clock").setMaxSize(ImageSizes.LAYER);
|
---|
448 | } else if (isLocked()) {
|
---|
449 | // If the layer is read only then change the default icon to a lock
|
---|
450 | base = new ImageProvider("lock").setMaxSize(ImageSizes.LAYER);
|
---|
451 | }
|
---|
452 | return base.get();
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
457 | * are drawn by the edit layer).
|
---|
458 | * Draw nodes last to overlap the ways they belong to.
|
---|
459 | */
|
---|
460 | @Override public void paint(final Graphics2D g, final MapView mv, Bounds box) {
|
---|
461 | boolean active = mv.getLayerManager().getActiveLayer() == this;
|
---|
462 | boolean inactive = !active && Config.getPref().getBoolean("draw.data.inactive_color", true);
|
---|
463 | boolean virtual = !inactive && mv.isVirtualNodesEnabled();
|
---|
464 |
|
---|
465 | // draw the hatched area for non-downloaded region. only draw if we're the active
|
---|
466 | // and bounds are defined; don't draw for inactive layers or loaded GPX files etc
|
---|
467 | if (active && Config.getPref().getBoolean("draw.data.downloaded_area", true) && !data.getDataSources().isEmpty()) {
|
---|
468 | // initialize area with current viewport
|
---|
469 | Rectangle b = mv.getBounds();
|
---|
470 | // on some platforms viewport bounds seem to be offset from the left,
|
---|
471 | // over-grow it just to be sure
|
---|
472 | b.grow(100, 100);
|
---|
473 | Path2D p = new Path2D.Double();
|
---|
474 |
|
---|
475 | // combine successively downloaded areas
|
---|
476 | for (Bounds bounds : data.getDataSourceBounds()) {
|
---|
477 | if (bounds.isCollapsed()) {
|
---|
478 | continue;
|
---|
479 | }
|
---|
480 | p.append(mv.getState().getArea(bounds), false);
|
---|
481 | }
|
---|
482 | // subtract combined areas
|
---|
483 | Area a = new Area(b);
|
---|
484 | a.subtract(new Area(p));
|
---|
485 |
|
---|
486 | // paint remainder
|
---|
487 | MapViewPoint anchor = mv.getState().getPointFor(new EastNorth(0, 0));
|
---|
488 | Rectangle2D anchorRect = new Rectangle2D.Double(anchor.getInView().getX() % HATCHED_SIZE,
|
---|
489 | anchor.getInView().getY() % HATCHED_SIZE, HATCHED_SIZE, HATCHED_SIZE);
|
---|
490 | g.setPaint(new TexturePaint(hatched, anchorRect));
|
---|
491 | g.fill(a);
|
---|
492 | }
|
---|
493 |
|
---|
494 | Rendering painter = MapRendererFactory.getInstance().createActiveRenderer(g, mv, inactive);
|
---|
495 | painter.render(data, virtual, box);
|
---|
496 | MainApplication.getMap().conflictDialog.paintConflicts(g, mv);
|
---|
497 | }
|
---|
498 |
|
---|
499 | @Override public String getToolTipText() {
|
---|
500 | DataCountVisitor counter = new DataCountVisitor();
|
---|
501 | for (final OsmPrimitive osm : data.allPrimitives()) {
|
---|
502 | osm.accept(counter);
|
---|
503 | }
|
---|
504 | int nodes = counter.nodes - counter.deletedNodes;
|
---|
505 | int ways = counter.ways - counter.deletedWays;
|
---|
506 | int rels = counter.relations - counter.deletedRelations;
|
---|
507 |
|
---|
508 | StringBuilder tooltip = new StringBuilder("<html>")
|
---|
509 | .append(trn("{0} node", "{0} nodes", nodes, nodes))
|
---|
510 | .append("<br>")
|
---|
511 | .append(trn("{0} way", "{0} ways", ways, ways))
|
---|
512 | .append("<br>")
|
---|
513 | .append(trn("{0} relation", "{0} relations", rels, rels));
|
---|
514 |
|
---|
515 | File f = getAssociatedFile();
|
---|
516 | if (f != null) {
|
---|
517 | tooltip.append("<br>").append(f.getPath());
|
---|
518 | }
|
---|
519 | tooltip.append("</html>");
|
---|
520 | return tooltip.toString();
|
---|
521 | }
|
---|
522 |
|
---|
523 | @Override public void mergeFrom(final Layer from) {
|
---|
524 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Merging layers"));
|
---|
525 | monitor.setCancelable(false);
|
---|
526 | if (from instanceof OsmDataLayer && ((OsmDataLayer) from).isUploadDiscouraged()) {
|
---|
527 | setUploadDiscouraged(true);
|
---|
528 | }
|
---|
529 | mergeFrom(((OsmDataLayer) from).data, monitor);
|
---|
530 | monitor.close();
|
---|
531 | }
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * merges the primitives in dataset <code>from</code> into the dataset of
|
---|
535 | * this layer
|
---|
536 | *
|
---|
537 | * @param from the source data set
|
---|
538 | */
|
---|
539 | public void mergeFrom(final DataSet from) {
|
---|
540 | mergeFrom(from, null);
|
---|
541 | }
|
---|
542 |
|
---|
543 | /**
|
---|
544 | * merges the primitives in dataset <code>from</code> into the dataset of this layer
|
---|
545 | *
|
---|
546 | * @param from the source data set
|
---|
547 | * @param progressMonitor the progress monitor, can be {@code null}
|
---|
548 | */
|
---|
549 | public void mergeFrom(final DataSet from, ProgressMonitor progressMonitor) {
|
---|
550 | final DataSetMerger visitor = new DataSetMerger(data, from);
|
---|
551 | try {
|
---|
552 | visitor.merge(progressMonitor);
|
---|
553 | } catch (DataIntegrityProblemException e) {
|
---|
554 | Logging.error(e);
|
---|
555 | JOptionPane.showMessageDialog(
|
---|
556 | Main.parent,
|
---|
557 | e.getHtmlMessage() != null ? e.getHtmlMessage() : e.getMessage(),
|
---|
558 | tr("Error"),
|
---|
559 | JOptionPane.ERROR_MESSAGE
|
---|
560 | );
|
---|
561 | return;
|
---|
562 | }
|
---|
563 |
|
---|
564 | int numNewConflicts = 0;
|
---|
565 | for (Conflict<?> c : visitor.getConflicts()) {
|
---|
566 | if (!data.getConflicts().hasConflict(c)) {
|
---|
567 | numNewConflicts++;
|
---|
568 | data.getConflicts().add(c);
|
---|
569 | }
|
---|
570 | }
|
---|
571 | // repaint to make sure new data is displayed properly.
|
---|
572 | invalidate();
|
---|
573 | // warn about new conflicts
|
---|
574 | MapFrame map = MainApplication.getMap();
|
---|
575 | if (numNewConflicts > 0 && map != null && map.conflictDialog != null) {
|
---|
576 | map.conflictDialog.warnNumNewConflicts(numNewConflicts);
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | @Override
|
---|
581 | public boolean isMergable(final Layer other) {
|
---|
582 | // allow merging between normal layers and discouraged layers with a warning (see #7684)
|
---|
583 | return other instanceof OsmDataLayer;
|
---|
584 | }
|
---|
585 |
|
---|
586 | @Override
|
---|
587 | public void visitBoundingBox(final BoundingXYVisitor v) {
|
---|
588 | for (final Node n: data.getNodes()) {
|
---|
589 | if (n.isUsable()) {
|
---|
590 | v.visit(n);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | /**
|
---|
596 | * Clean out the data behind the layer. This means clearing the redo/undo lists,
|
---|
597 | * really deleting all deleted objects and reset the modified flags. This should
|
---|
598 | * be done after an upload, even after a partial upload.
|
---|
599 | *
|
---|
600 | * @param processed A list of all objects that were actually uploaded.
|
---|
601 | * May be <code>null</code>, which means nothing has been uploaded
|
---|
602 | */
|
---|
603 | public void cleanupAfterUpload(final Collection<? extends IPrimitive> processed) {
|
---|
604 | // return immediately if an upload attempt failed
|
---|
605 | if (processed == null || processed.isEmpty())
|
---|
606 | return;
|
---|
607 |
|
---|
608 | MainApplication.undoRedo.clean(data);
|
---|
609 |
|
---|
610 | // if uploaded, clean the modified flags as well
|
---|
611 | data.cleanupDeletedPrimitives();
|
---|
612 | data.beginUpdate();
|
---|
613 | try {
|
---|
614 | for (OsmPrimitive p: data.allPrimitives()) {
|
---|
615 | if (processed.contains(p)) {
|
---|
616 | p.setModified(false);
|
---|
617 | }
|
---|
618 | }
|
---|
619 | } finally {
|
---|
620 | data.endUpdate();
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 | @Override
|
---|
625 | public Object getInfoComponent() {
|
---|
626 | final DataCountVisitor counter = new DataCountVisitor();
|
---|
627 | for (final OsmPrimitive osm : data.allPrimitives()) {
|
---|
628 | osm.accept(counter);
|
---|
629 | }
|
---|
630 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
631 |
|
---|
632 | String nodeText = trn("{0} node", "{0} nodes", counter.nodes, counter.nodes);
|
---|
633 | if (counter.deletedNodes > 0) {
|
---|
634 | nodeText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedNodes, counter.deletedNodes)+')';
|
---|
635 | }
|
---|
636 |
|
---|
637 | String wayText = trn("{0} way", "{0} ways", counter.ways, counter.ways);
|
---|
638 | if (counter.deletedWays > 0) {
|
---|
639 | wayText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedWays, counter.deletedWays)+')';
|
---|
640 | }
|
---|
641 |
|
---|
642 | String relationText = trn("{0} relation", "{0} relations", counter.relations, counter.relations);
|
---|
643 | if (counter.deletedRelations > 0) {
|
---|
644 | relationText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedRelations, counter.deletedRelations)+')';
|
---|
645 | }
|
---|
646 |
|
---|
647 | p.add(new JLabel(tr("{0} consists of:", getName())), GBC.eol());
|
---|
648 | p.add(new JLabel(nodeText, ImageProvider.get("data", "node"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
|
---|
649 | p.add(new JLabel(wayText, ImageProvider.get("data", "way"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
|
---|
650 | p.add(new JLabel(relationText, ImageProvider.get("data", "relation"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
|
---|
651 | p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))),
|
---|
652 | GBC.eop().insets(15, 0, 0, 0));
|
---|
653 | if (isUploadDiscouraged()) {
|
---|
654 | p.add(new JLabel(tr("Upload is discouraged")), GBC.eop().insets(15, 0, 0, 0));
|
---|
655 | }
|
---|
656 | if (data.getUploadPolicy() == UploadPolicy.BLOCKED) {
|
---|
657 | p.add(new JLabel(tr("Upload is blocked")), GBC.eop().insets(15, 0, 0, 0));
|
---|
658 | }
|
---|
659 |
|
---|
660 | return p;
|
---|
661 | }
|
---|
662 |
|
---|
663 | @Override public Action[] getMenuEntries() {
|
---|
664 | List<Action> actions = new ArrayList<>();
|
---|
665 | actions.addAll(Arrays.asList(
|
---|
666 | LayerListDialog.getInstance().createActivateLayerAction(this),
|
---|
667 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
668 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
669 | SeparatorLayerAction.INSTANCE,
|
---|
670 | LayerListDialog.getInstance().createMergeLayerAction(this),
|
---|
671 | LayerListDialog.getInstance().createDuplicateLayerAction(this),
|
---|
672 | new LayerSaveAction(this),
|
---|
673 | new LayerSaveAsAction(this)));
|
---|
674 | if (ExpertToggleAction.isExpert()) {
|
---|
675 | actions.addAll(Arrays.asList(
|
---|
676 | new LayerGpxExportAction(this),
|
---|
677 | new ConvertToGpxLayerAction()));
|
---|
678 | }
|
---|
679 | actions.addAll(Arrays.asList(
|
---|
680 | SeparatorLayerAction.INSTANCE,
|
---|
681 | new RenameLayerAction(getAssociatedFile(), this)));
|
---|
682 | if (ExpertToggleAction.isExpert()) {
|
---|
683 | actions.add(new ToggleUploadDiscouragedLayerAction(this));
|
---|
684 | }
|
---|
685 | actions.addAll(Arrays.asList(
|
---|
686 | new ConsistencyTestAction(),
|
---|
687 | SeparatorLayerAction.INSTANCE,
|
---|
688 | new LayerListPopup.InfoAction(this)));
|
---|
689 | return actions.toArray(new Action[0]);
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Converts given OSM dataset to GPX data.
|
---|
694 | * @param data OSM dataset
|
---|
695 | * @param file output .gpx file
|
---|
696 | * @return GPX data
|
---|
697 | */
|
---|
698 | public static GpxData toGpxData(DataSet data, File file) {
|
---|
699 | GpxData gpxData = new GpxData();
|
---|
700 | gpxData.storageFile = file;
|
---|
701 | Set<Node> doneNodes = new HashSet<>();
|
---|
702 | waysToGpxData(data.getWays(), gpxData, doneNodes);
|
---|
703 | nodesToGpxData(data.getNodes(), gpxData, doneNodes);
|
---|
704 | return gpxData;
|
---|
705 | }
|
---|
706 |
|
---|
707 | private static void waysToGpxData(Collection<Way> ways, GpxData gpxData, Set<Node> doneNodes) {
|
---|
708 | /* When the dataset has been obtained from a gpx layer and now is being converted back,
|
---|
709 | * the ways have negative ids. The first created way corresponds to the first gpx segment,
|
---|
710 | * and has the highest id (i.e., closest to zero).
|
---|
711 | * Thus, sorting by OsmPrimitive#getUniqueId gives the original order.
|
---|
712 | * (Only works if the data layer has not been saved to and been loaded from an osm file before.)
|
---|
713 | */
|
---|
714 | ways.stream()
|
---|
715 | .sorted(OsmPrimitiveComparator.comparingUniqueId().reversed())
|
---|
716 | .forEachOrdered(w -> {
|
---|
717 | if (!w.isUsable()) {
|
---|
718 | return;
|
---|
719 | }
|
---|
720 | Collection<Collection<WayPoint>> trk = new ArrayList<>();
|
---|
721 | Map<String, Object> trkAttr = new HashMap<>();
|
---|
722 |
|
---|
723 | String name = w.get("name");
|
---|
724 | if (name != null) {
|
---|
725 | trkAttr.put("name", name);
|
---|
726 | }
|
---|
727 |
|
---|
728 | List<WayPoint> trkseg = null;
|
---|
729 | for (Node n : w.getNodes()) {
|
---|
730 | if (!n.isUsable()) {
|
---|
731 | trkseg = null;
|
---|
732 | continue;
|
---|
733 | }
|
---|
734 | if (trkseg == null) {
|
---|
735 | trkseg = new ArrayList<>();
|
---|
736 | trk.add(trkseg);
|
---|
737 | }
|
---|
738 | if (!n.isTagged()) {
|
---|
739 | doneNodes.add(n);
|
---|
740 | }
|
---|
741 | trkseg.add(nodeToWayPoint(n));
|
---|
742 | }
|
---|
743 |
|
---|
744 | gpxData.addTrack(new ImmutableGpxTrack(trk, trkAttr));
|
---|
745 | });
|
---|
746 | }
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * @param n the {@code Node} to convert
|
---|
750 | * @return {@code WayPoint} object
|
---|
751 | * @since 13210
|
---|
752 | */
|
---|
753 | public static WayPoint nodeToWayPoint(Node n) {
|
---|
754 | return nodeToWayPoint(n, 0);
|
---|
755 | }
|
---|
756 |
|
---|
757 | /**
|
---|
758 | * @param n the {@code Node} to convert
|
---|
759 | * @param time a time value in milliseconds from the epoch.
|
---|
760 | * @return {@code WayPoint} object
|
---|
761 | * @since 13210
|
---|
762 | */
|
---|
763 | public static WayPoint nodeToWayPoint(Node n, long time) {
|
---|
764 | WayPoint wpt = new WayPoint(n.getCoor());
|
---|
765 |
|
---|
766 | // Position info
|
---|
767 |
|
---|
768 | addDoubleIfPresent(wpt, n, GpxConstants.PT_ELE);
|
---|
769 |
|
---|
770 | if (time > 0) {
|
---|
771 | wpt.setTime(time);
|
---|
772 | } else if (!n.isTimestampEmpty()) {
|
---|
773 | wpt.put("time", DateUtils.fromTimestamp(n.getRawTimestamp()));
|
---|
774 | wpt.setTime();
|
---|
775 | }
|
---|
776 |
|
---|
777 | addDoubleIfPresent(wpt, n, GpxConstants.PT_MAGVAR);
|
---|
778 | addDoubleIfPresent(wpt, n, GpxConstants.PT_GEOIDHEIGHT);
|
---|
779 |
|
---|
780 | // Description info
|
---|
781 |
|
---|
782 | addStringIfPresent(wpt, n, GpxConstants.GPX_NAME);
|
---|
783 | addStringIfPresent(wpt, n, GpxConstants.GPX_DESC, "description");
|
---|
784 | addStringIfPresent(wpt, n, GpxConstants.GPX_CMT, "comment");
|
---|
785 | addStringIfPresent(wpt, n, GpxConstants.GPX_SRC, "source", "source:position");
|
---|
786 |
|
---|
787 | Collection<GpxLink> links = new ArrayList<>();
|
---|
788 | for (String key : new String[]{"link", "url", "website", "contact:website"}) {
|
---|
789 | String value = n.get(key);
|
---|
790 | if (value != null) {
|
---|
791 | links.add(new GpxLink(value));
|
---|
792 | }
|
---|
793 | }
|
---|
794 | wpt.put(GpxConstants.META_LINKS, links);
|
---|
795 |
|
---|
796 | addStringIfPresent(wpt, n, GpxConstants.PT_SYM, "wpt_symbol");
|
---|
797 | addStringIfPresent(wpt, n, GpxConstants.PT_TYPE);
|
---|
798 |
|
---|
799 | // Accuracy info
|
---|
800 | addStringIfPresent(wpt, n, GpxConstants.PT_FIX, "gps:fix");
|
---|
801 | addIntegerIfPresent(wpt, n, GpxConstants.PT_SAT, "gps:sat");
|
---|
802 | addDoubleIfPresent(wpt, n, GpxConstants.PT_HDOP, "gps:hdop");
|
---|
803 | addDoubleIfPresent(wpt, n, GpxConstants.PT_VDOP, "gps:vdop");
|
---|
804 | addDoubleIfPresent(wpt, n, GpxConstants.PT_PDOP, "gps:pdop");
|
---|
805 | addDoubleIfPresent(wpt, n, GpxConstants.PT_AGEOFDGPSDATA, "gps:ageofdgpsdata");
|
---|
806 | addIntegerIfPresent(wpt, n, GpxConstants.PT_DGPSID, "gps:dgpsid");
|
---|
807 |
|
---|
808 | return wpt;
|
---|
809 | }
|
---|
810 |
|
---|
811 | private static void nodesToGpxData(Collection<Node> nodes, GpxData gpxData, Set<Node> doneNodes) {
|
---|
812 | List<Node> sortedNodes = new ArrayList<>(nodes);
|
---|
813 | sortedNodes.removeAll(doneNodes);
|
---|
814 | Collections.sort(sortedNodes);
|
---|
815 | for (Node n : sortedNodes) {
|
---|
816 | if (n.isIncomplete() || n.isDeleted()) {
|
---|
817 | continue;
|
---|
818 | }
|
---|
819 | gpxData.waypoints.add(nodeToWayPoint(n));
|
---|
820 | }
|
---|
821 | }
|
---|
822 |
|
---|
823 | private static void addIntegerIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
|
---|
824 | List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
|
---|
825 | possibleKeys.add(0, gpxKey);
|
---|
826 | for (String key : possibleKeys) {
|
---|
827 | String value = p.get(key);
|
---|
828 | if (value != null) {
|
---|
829 | try {
|
---|
830 | int i = Integer.parseInt(value);
|
---|
831 | // Sanity checks
|
---|
832 | if ((!GpxConstants.PT_SAT.equals(gpxKey) || i >= 0) &&
|
---|
833 | (!GpxConstants.PT_DGPSID.equals(gpxKey) || (0 <= i && i <= 1023))) {
|
---|
834 | wpt.put(gpxKey, value);
|
---|
835 | break;
|
---|
836 | }
|
---|
837 | } catch (NumberFormatException e) {
|
---|
838 | Logging.trace(e);
|
---|
839 | }
|
---|
840 | }
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | private static void addDoubleIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
|
---|
845 | List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
|
---|
846 | possibleKeys.add(0, gpxKey);
|
---|
847 | for (String key : possibleKeys) {
|
---|
848 | String value = p.get(key);
|
---|
849 | if (value != null) {
|
---|
850 | try {
|
---|
851 | double d = Double.parseDouble(value);
|
---|
852 | // Sanity checks
|
---|
853 | if (!GpxConstants.PT_MAGVAR.equals(gpxKey) || (0.0 <= d && d < 360.0)) {
|
---|
854 | wpt.put(gpxKey, value);
|
---|
855 | break;
|
---|
856 | }
|
---|
857 | } catch (NumberFormatException e) {
|
---|
858 | Logging.trace(e);
|
---|
859 | }
|
---|
860 | }
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | private static void addStringIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
|
---|
865 | List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
|
---|
866 | possibleKeys.add(0, gpxKey);
|
---|
867 | for (String key : possibleKeys) {
|
---|
868 | String value = p.get(key);
|
---|
869 | // Sanity checks
|
---|
870 | if (value != null && (!GpxConstants.PT_FIX.equals(gpxKey) || GpxConstants.FIX_VALUES.contains(value))) {
|
---|
871 | wpt.put(gpxKey, value);
|
---|
872 | break;
|
---|
873 | }
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * Converts OSM data behind this layer to GPX data.
|
---|
879 | * @return GPX data
|
---|
880 | */
|
---|
881 | public GpxData toGpxData() {
|
---|
882 | return toGpxData(data, getAssociatedFile());
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Action that converts this OSM layer to a GPX layer.
|
---|
887 | */
|
---|
888 | public class ConvertToGpxLayerAction extends AbstractAction {
|
---|
889 | /**
|
---|
890 | * Constructs a new {@code ConvertToGpxLayerAction}.
|
---|
891 | */
|
---|
892 | public ConvertToGpxLayerAction() {
|
---|
893 | super(tr("Convert to GPX layer"));
|
---|
894 | new ImageProvider("converttogpx").getResource().attachImageIcon(this, true);
|
---|
895 | putValue("help", ht("/Action/ConvertToGpxLayer"));
|
---|
896 | }
|
---|
897 |
|
---|
898 | @Override
|
---|
899 | public void actionPerformed(ActionEvent e) {
|
---|
900 | final GpxData gpxData = toGpxData();
|
---|
901 | final GpxLayer gpxLayer = new GpxLayer(gpxData, tr("Converted from: {0}", getName()));
|
---|
902 | if (getAssociatedFile() != null) {
|
---|
903 | String filename = getAssociatedFile().getName().replaceAll(Pattern.quote(".gpx.osm") + '$', "") + ".gpx";
|
---|
904 | gpxLayer.setAssociatedFile(new File(getAssociatedFile().getParentFile(), filename));
|
---|
905 | }
|
---|
906 | MainApplication.getLayerManager().addLayer(gpxLayer);
|
---|
907 | if (Config.getPref().getBoolean("marker.makeautomarkers", true) && !gpxData.waypoints.isEmpty()) {
|
---|
908 | MainApplication.getLayerManager().addLayer(new MarkerLayer(gpxData, tr("Converted from: {0}", getName()), null, gpxLayer));
|
---|
909 | }
|
---|
910 | MainApplication.getLayerManager().removeLayer(OsmDataLayer.this);
|
---|
911 | }
|
---|
912 | }
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * Determines if this layer contains data at the given coordinate.
|
---|
916 | * @param coor the coordinate
|
---|
917 | * @return {@code true} if data sources bounding boxes contain {@code coor}
|
---|
918 | */
|
---|
919 | public boolean containsPoint(LatLon coor) {
|
---|
920 | // we'll assume that if this has no data sources
|
---|
921 | // that it also has no borders
|
---|
922 | if (this.data.getDataSources().isEmpty())
|
---|
923 | return true;
|
---|
924 |
|
---|
925 | boolean layerBoundsPoint = false;
|
---|
926 | for (DataSource src : this.data.getDataSources()) {
|
---|
927 | if (src.bounds.contains(coor)) {
|
---|
928 | layerBoundsPoint = true;
|
---|
929 | break;
|
---|
930 | }
|
---|
931 | }
|
---|
932 | return layerBoundsPoint;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Replies the set of conflicts currently managed in this layer.
|
---|
937 | *
|
---|
938 | * @return the set of conflicts currently managed in this layer
|
---|
939 | */
|
---|
940 | public ConflictCollection getConflicts() {
|
---|
941 | return data.getConflicts();
|
---|
942 | }
|
---|
943 |
|
---|
944 | @Override
|
---|
945 | public boolean isDownloadable() {
|
---|
946 | return data.getDownloadPolicy() != DownloadPolicy.BLOCKED && !isLocked();
|
---|
947 | }
|
---|
948 |
|
---|
949 | @Override
|
---|
950 | public boolean isUploadable() {
|
---|
951 | return data.getUploadPolicy() != UploadPolicy.BLOCKED && !isLocked();
|
---|
952 | }
|
---|
953 |
|
---|
954 | @Override
|
---|
955 | public boolean requiresUploadToServer() {
|
---|
956 | return isUploadable() && requiresUploadToServer;
|
---|
957 | }
|
---|
958 |
|
---|
959 | @Override
|
---|
960 | public boolean requiresSaveToFile() {
|
---|
961 | return getAssociatedFile() != null && requiresSaveToFile;
|
---|
962 | }
|
---|
963 |
|
---|
964 | @Override
|
---|
965 | public void onPostLoadFromFile() {
|
---|
966 | setRequiresSaveToFile(false);
|
---|
967 | setRequiresUploadToServer(isModified());
|
---|
968 | invalidate();
|
---|
969 | }
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Actions run after data has been downloaded to this layer.
|
---|
973 | */
|
---|
974 | public void onPostDownloadFromServer() {
|
---|
975 | setRequiresSaveToFile(true);
|
---|
976 | setRequiresUploadToServer(isModified());
|
---|
977 | invalidate();
|
---|
978 | }
|
---|
979 |
|
---|
980 | @Override
|
---|
981 | public void onPostSaveToFile() {
|
---|
982 | setRequiresSaveToFile(false);
|
---|
983 | setRequiresUploadToServer(isModified());
|
---|
984 | }
|
---|
985 |
|
---|
986 | @Override
|
---|
987 | public void onPostUploadToServer() {
|
---|
988 | setRequiresUploadToServer(isModified());
|
---|
989 | // keep requiresSaveToDisk unchanged
|
---|
990 | }
|
---|
991 |
|
---|
992 | private class ConsistencyTestAction extends AbstractAction {
|
---|
993 |
|
---|
994 | ConsistencyTestAction() {
|
---|
995 | super(tr("Dataset consistency test"));
|
---|
996 | }
|
---|
997 |
|
---|
998 | @Override
|
---|
999 | public void actionPerformed(ActionEvent e) {
|
---|
1000 | String result = DatasetConsistencyTest.runTests(data);
|
---|
1001 | if (result.isEmpty()) {
|
---|
1002 | JOptionPane.showMessageDialog(Main.parent, tr("No problems found"));
|
---|
1003 | } else {
|
---|
1004 | JPanel p = new JPanel(new GridBagLayout());
|
---|
1005 | p.add(new JLabel(tr("Following problems found:")), GBC.eol());
|
---|
1006 | JosmTextArea info = new JosmTextArea(result, 20, 60);
|
---|
1007 | info.setCaretPosition(0);
|
---|
1008 | info.setEditable(false);
|
---|
1009 | p.add(new JScrollPane(info), GBC.eop());
|
---|
1010 |
|
---|
1011 | JOptionPane.showMessageDialog(Main.parent, p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | @Override
|
---|
1017 | public synchronized void destroy() {
|
---|
1018 | super.destroy();
|
---|
1019 | data.removeSelectionListener(this);
|
---|
1020 | data.removeHighlightUpdateListener(this);
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | @Override
|
---|
1024 | public void processDatasetEvent(AbstractDatasetChangedEvent event) {
|
---|
1025 | invalidate();
|
---|
1026 | setRequiresSaveToFile(true);
|
---|
1027 | setRequiresUploadToServer(event.getDataset().requiresUploadToServer());
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | @Override
|
---|
1031 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
1032 | invalidate();
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | @Override
|
---|
1036 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
1037 | // No reprojection required. The dataset itself is registered as projection
|
---|
1038 | // change listener and already got notified.
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | @Override
|
---|
1042 | public final boolean isUploadDiscouraged() {
|
---|
1043 | return data.getUploadPolicy() == UploadPolicy.DISCOURAGED;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | /**
|
---|
1047 | * Sets the "discouraged upload" flag.
|
---|
1048 | * @param uploadDiscouraged {@code true} if upload of data managed by this layer is discouraged.
|
---|
1049 | * This feature allows to use "private" data layers.
|
---|
1050 | */
|
---|
1051 | public final void setUploadDiscouraged(boolean uploadDiscouraged) {
|
---|
1052 | if (data.getUploadPolicy() != UploadPolicy.BLOCKED &&
|
---|
1053 | (uploadDiscouraged ^ isUploadDiscouraged())) {
|
---|
1054 | data.setUploadPolicy(uploadDiscouraged ? UploadPolicy.DISCOURAGED : UploadPolicy.NORMAL);
|
---|
1055 | for (LayerStateChangeListener l : layerStateChangeListeners) {
|
---|
1056 | l.uploadDiscouragedChanged(this, uploadDiscouraged);
|
---|
1057 | }
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | @Override
|
---|
1062 | public final boolean isModified() {
|
---|
1063 | return data.isModified();
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | @Override
|
---|
1067 | public boolean isSavable() {
|
---|
1068 | return true; // With OsmExporter
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | @Override
|
---|
1072 | public boolean checkSaveConditions() {
|
---|
1073 | if (isDataSetEmpty() && 1 != GuiHelper.runInEDTAndWaitAndReturn(() -> {
|
---|
1074 | if (GraphicsEnvironment.isHeadless()) {
|
---|
1075 | return 2;
|
---|
1076 | }
|
---|
1077 | return new ExtendedDialog(
|
---|
1078 | Main.parent,
|
---|
1079 | tr("Empty document"),
|
---|
1080 | tr("Save anyway"), tr("Cancel"))
|
---|
1081 | .setContent(tr("The document contains no data."))
|
---|
1082 | .setButtonIcons("save", "cancel")
|
---|
1083 | .showDialog().getValue();
|
---|
1084 | })) {
|
---|
1085 | return false;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | ConflictCollection conflictsCol = getConflicts();
|
---|
1089 | return conflictsCol == null || conflictsCol.isEmpty() || 1 == GuiHelper.runInEDTAndWaitAndReturn(() ->
|
---|
1090 | new ExtendedDialog(
|
---|
1091 | Main.parent,
|
---|
1092 | /* I18N: Display title of the window showing conflicts */
|
---|
1093 | tr("Conflicts"),
|
---|
1094 | tr("Reject Conflicts and Save"), tr("Cancel"))
|
---|
1095 | .setContent(
|
---|
1096 | tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"))
|
---|
1097 | .setButtonIcons("save", "cancel")
|
---|
1098 | .showDialog().getValue()
|
---|
1099 | );
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /**
|
---|
1103 | * Check the data set if it would be empty on save. It is empty, if it contains
|
---|
1104 | * no objects (after all objects that are created and deleted without being
|
---|
1105 | * transferred to the server have been removed).
|
---|
1106 | *
|
---|
1107 | * @return <code>true</code>, if a save result in an empty data set.
|
---|
1108 | */
|
---|
1109 | private boolean isDataSetEmpty() {
|
---|
1110 | if (data != null) {
|
---|
1111 | for (OsmPrimitive osm : data.allNonDeletedPrimitives()) {
|
---|
1112 | if (!osm.isDeleted() || !osm.isNewOrUndeleted())
|
---|
1113 | return false;
|
---|
1114 | }
|
---|
1115 | }
|
---|
1116 | return true;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | @Override
|
---|
1120 | public File createAndOpenSaveFileChooser() {
|
---|
1121 | String extension = PROPERTY_SAVE_EXTENSION.get();
|
---|
1122 | File file = getAssociatedFile();
|
---|
1123 | if (file == null && isRenamed()) {
|
---|
1124 | StringBuilder filename = new StringBuilder(Config.getPref().get("lastDirectory")).append('/').append(getName());
|
---|
1125 | if (!OsmImporter.FILE_FILTER.acceptName(filename.toString())) {
|
---|
1126 | filename.append('.').append(extension);
|
---|
1127 | }
|
---|
1128 | file = new File(filename.toString());
|
---|
1129 | }
|
---|
1130 | return new FileChooserManager()
|
---|
1131 | .title(tr("Save OSM file"))
|
---|
1132 | .extension(extension)
|
---|
1133 | .file(file)
|
---|
1134 | .allTypes(true)
|
---|
1135 | .getFileForSave();
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | @Override
|
---|
1139 | public AbstractIOTask createUploadTask(final ProgressMonitor monitor) {
|
---|
1140 | UploadDialog dialog = UploadDialog.getUploadDialog();
|
---|
1141 | return new UploadLayerTask(
|
---|
1142 | dialog.getUploadStrategySpecification(),
|
---|
1143 | this,
|
---|
1144 | monitor,
|
---|
1145 | dialog.getChangeset());
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | @Override
|
---|
1149 | public AbstractUploadDialog getUploadDialog() {
|
---|
1150 | UploadDialog dialog = UploadDialog.getUploadDialog();
|
---|
1151 | dialog.setUploadedPrimitives(new APIDataSet(data));
|
---|
1152 | return dialog;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | @Override
|
---|
1156 | public ProjectionBounds getViewProjectionBounds() {
|
---|
1157 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
1158 | v.visit(data.getDataSourceBoundingBox());
|
---|
1159 | if (!v.hasExtend()) {
|
---|
1160 | v.computeBoundingBox(data.getNodes());
|
---|
1161 | }
|
---|
1162 | return v.getBounds();
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | @Override
|
---|
1166 | public void highlightUpdated(HighlightUpdateEvent e) {
|
---|
1167 | invalidate();
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | @Override
|
---|
1171 | public void setName(String name) {
|
---|
1172 | if (data != null) {
|
---|
1173 | data.setName(name);
|
---|
1174 | }
|
---|
1175 | super.setName(name);
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | @Override
|
---|
1179 | public void lock() {
|
---|
1180 | data.lock();
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | @Override
|
---|
1184 | public void unlock() {
|
---|
1185 | data.unlock();
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | @Override
|
---|
1189 | public boolean isLocked() {
|
---|
1190 | return data.isLocked();
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /**
|
---|
1194 | * Sets the "upload in progress" flag, which will result in displaying a new icon and forbid to remove the layer.
|
---|
1195 | * @since 13434
|
---|
1196 | */
|
---|
1197 | public void setUploadInProgress() {
|
---|
1198 | if (!isUploadInProgress.compareAndSet(false, true)) {
|
---|
1199 | Logging.warn("Trying to set uploadInProgress flag on layer already being uploaded ", getName());
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | /**
|
---|
1204 | * Unsets the "upload in progress" flag, which will result in displaying the standard icon and allow to remove the layer.
|
---|
1205 | * @since 13434
|
---|
1206 | */
|
---|
1207 | public void unsetUploadInProgress() {
|
---|
1208 | if (!isUploadInProgress.compareAndSet(true, false)) {
|
---|
1209 | Logging.warn("Trying to unset uploadInProgress flag on layer not being uploaded ", getName());
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | @Override
|
---|
1214 | public boolean isUploadInProgress() {
|
---|
1215 | return isUploadInProgress.get();
|
---|
1216 | }
|
---|
1217 | }
|
---|