Changeset 18008 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2021-07-12T23:45:16+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
r17900 r18008 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.layer; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.trn; 6 7 import java.awt.Color; 8 import java.awt.Dimension; 9 import java.awt.Graphics2D; 10 import java.awt.event.ActionEvent; 11 import java.io.File; 12 import java.time.Instant; 13 import java.util.ArrayList; 14 import java.util.Arrays; 15 import java.util.Collections; 16 import java.util.List; 17 import java.util.NoSuchElementException; 18 import java.util.stream.Collectors; 19 20 import javax.swing.AbstractAction; 21 import javax.swing.Action; 22 import javax.swing.Icon; 23 import javax.swing.JScrollPane; 24 import javax.swing.SwingUtilities; 3 25 4 26 import org.openstreetmap.josm.actions.AutoScaleAction; … … 42 64 import org.openstreetmap.josm.tools.date.Interval; 43 65 44 import javax.swing.AbstractAction;45 import javax.swing.Action;46 import javax.swing.Icon;47 import javax.swing.JScrollPane;48 import javax.swing.SwingUtilities;49 import java.awt.Color;50 import java.awt.Dimension;51 import java.awt.Graphics2D;52 import java.awt.event.ActionEvent;53 import java.io.File;54 import java.time.Instant;55 import java.util.ArrayList;56 import java.util.Arrays;57 import java.util.Collections;58 import java.util.List;59 import java.util.NoSuchElementException;60 import java.util.stream.Collectors;61 62 import static org.openstreetmap.josm.tools.I18n.tr;63 import static org.openstreetmap.josm.tools.I18n.trn;64 65 66 /** 66 67 * A layer that displays data from a Gpx file / the OSM gpx downloads. … … 130 131 @Override 131 132 public Color getColor() { 133 if (data == null) 134 return null; 132 135 Color[] c = data.getTracks().stream().map(t -> t.getColor()).distinct().toArray(Color[]::new); 133 136 return c.length == 1 ? c[0] : null; //only return if exactly one distinct color present … … 146 149 @Override 147 150 public boolean hasColor() { 148 return true;151 return data != null; 149 152 } 150 153 … … 168 171 .append("<html><head><style>td { padding: 4px 16px; }</style></head><body>"); 169 172 173 if (data != null) { 174 fillDataInfoComponent(info); 175 } 176 177 info.append("<br></body></html>"); 178 179 final JScrollPane sp = new JScrollPane(new HtmlPanel(info.toString())); 180 sp.setPreferredSize(new Dimension(sp.getPreferredSize().width+20, 370)); 181 SwingUtilities.invokeLater(() -> sp.getVerticalScrollBar().setValue(0)); 182 return sp; 183 } 184 185 private void fillDataInfoComponent(StringBuilder info) { 170 186 if (data.attr.containsKey("name")) { 171 187 info.append(tr("Name: {0}", data.get(GpxConstants.META_NAME))).append("<br>"); … … 215 231 info.append(tr("Length: {0}", SystemOfMeasurement.getSystemOfMeasurement().getDistText(data.length()))).append("<br>") 216 232 .append(trn("{0} route, ", "{0} routes, ", data.getRoutes().size(), data.getRoutes().size())) 217 .append(trn("{0} waypoint", "{0} waypoints", data.getWaypoints().size(), data.getWaypoints().size())) 218 .append("<br></body></html>"); 219 220 final JScrollPane sp = new JScrollPane(new HtmlPanel(info.toString())); 221 sp.setPreferredSize(new Dimension(sp.getPreferredSize().width+20, 370)); 222 SwingUtilities.invokeLater(() -> sp.getVerticalScrollBar().setValue(0)); 223 return sp; 233 .append(trn("{0} waypoint", "{0} waypoints", data.getWaypoints().size(), data.getWaypoints().size())); 224 234 } 225 235 … … 284 294 StringBuilder info = new StringBuilder(48).append("<html>"); 285 295 296 if (data != null) { 297 fillDataToolTipText(info); 298 } 299 300 info.append("<br></html>"); 301 302 return info.toString(); 303 } 304 305 private void fillDataToolTipText(StringBuilder info) { 286 306 if (data.attr.containsKey(GpxConstants.META_NAME)) { 287 307 info.append(tr("Name: {0}", data.get(GpxConstants.META_NAME))).append("<br>"); … … 305 325 .collect(Collectors.joining("<br>"))); 306 326 } 307 308 info.append("<br></html>");309 310 return info.toString();311 327 } 312 328 313 329 @Override 314 330 public boolean isMergable(Layer other) { 315 return other instanceof GpxLayer;331 return data != null && other instanceof GpxLayer; 316 332 } 317 333 … … 323 339 */ 324 340 public void filterTracksByDate(Instant fromDate, Instant toDate, boolean showWithoutDate) { 341 if (data == null) 342 return; 325 343 int i = 0; 326 344 long from = fromDate.toEpochMilli(); … … 358 376 @Override 359 377 public void visitBoundingBox(BoundingXYVisitor v) { 360 v.visit(data.recalculateBounds()); 378 if (data != null) { 379 v.visit(data.recalculateBounds()); 380 } 361 381 } 362 382 363 383 @Override 364 384 public File getAssociatedFile() { 365 return data .storageFile;385 return data != null ? data.storageFile : null; 366 386 } 367 387 … … 391 411 @Override 392 412 public void projectionChanged(Projection oldValue, Projection newValue) { 393 if (newValue == null ) return;413 if (newValue == null || data == null) return; 394 414 data.resetEastNorthCache(); 395 415 } … … 397 417 @Override 398 418 public boolean isSavable() { 399 return true; // With GpxExporter419 return data != null; // With GpxExporter 400 420 } 401 421 … … 560 580 @Override 561 581 public void jumpToNextMarker() { 562 List<IGpxTrackSegment> segments = data.getTrackSegmentsStream().collect(Collectors.toList()); 563 jumpToNext(segments); 582 if (data != null) { 583 jumpToNext(data.getTrackSegmentsStream().collect(Collectors.toList())); 584 } 564 585 } 565 586 … … 569 590 @Override 570 591 public void jumpToPreviousMarker() { 571 List<IGpxTrackSegment> segments = data.getTrackSegmentsStream().collect(Collectors.toList()); 572 Collections.reverse(segments); 573 jumpToNext(segments); 592 if (data != null) { 593 List<IGpxTrackSegment> segments = data.getTrackSegmentsStream().collect(Collectors.toList()); 594 Collections.reverse(segments); 595 jumpToNext(segments); 596 } 574 597 } 575 598
Note:
See TracChangeset
for help on using the changeset viewer.