Changeset 18008 in josm
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 -
trunk/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java
r17845 r18008 2 2 package org.openstreetmap.josm.gui.layer; 3 3 4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 5 import static org.junit.jupiter.api.Assertions.assertEquals; 5 6 import static org.junit.jupiter.api.Assertions.assertFalse; 6 7 import static org.junit.jupiter.api.Assertions.assertNull; 8 import static org.junit.jupiter.api.Assertions.assertThrows; 7 9 import static org.junit.jupiter.api.Assertions.assertTrue; 8 import static org.junit.jupiter.api.Assertions.assertThrows;9 10 10 11 import java.awt.Color; 12 import java.awt.Component; 11 13 import java.io.IOException; 12 14 import java.util.ArrayList; … … 27 29 import org.openstreetmap.josm.data.gpx.WayPoint; 28 30 import org.openstreetmap.josm.data.osm.DataSet; 31 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 32 import org.openstreetmap.josm.data.projection.CustomProjection; 29 33 import org.openstreetmap.josm.data.projection.Projections; 30 34 import org.openstreetmap.josm.gui.MainApplication; … … 261 265 assertNull(new GpxLayer(new GpxData(), "", false).getChangesetSourceTag()); 262 266 } 267 268 /** 269 * Checks that potential operations that could be called after destroy() are harmless 270 */ 271 @Test 272 void testRobustnessAfterDestroy() { 273 GpxData data = new GpxData(); 274 GpxLayer layer = new GpxLayer(data, "1", false); 275 GpxLayer otherLayer = new GpxLayer(new GpxData(), "2", false); 276 assertEquals(data, layer.getData()); 277 assertTrue(layer.isMergable(otherLayer)); 278 assertTrue(layer.hasColor()); 279 assertTrue(layer.isSavable()); 280 assertTrue(layer.checkSaveConditions()); 281 assertFalse(layer.isModified()); 282 assertFalse(layer.requiresSaveToFile()); 283 assertNull(layer.getChangesetSourceTag()); 284 assertNull(layer.getAssociatedFile()); 285 286 layer.destroy(); 287 288 assertNull(layer.getData()); 289 assertNull(layer.getColor()); 290 assertFalse(layer.hasColor()); 291 assertFalse(layer.isMergable(otherLayer)); 292 assertFalse(layer.isSavable()); 293 assertFalse(layer.checkSaveConditions()); 294 assertFalse(layer.isModified()); 295 assertFalse(layer.requiresSaveToFile()); 296 assertNull(layer.getChangesetSourceTag()); 297 assertNull(layer.getAssociatedFile()); 298 Object infoComponent = layer.getInfoComponent(); 299 assertTrue(infoComponent instanceof JScrollPane); 300 Component view = ((JScrollPane) infoComponent).getViewport().getView(); 301 assertTrue(view instanceof HtmlPanel); 302 String text = ((HtmlPanel) view).getEditorPane().getText().trim(); 303 assertTrue(text.startsWith("<html>"), text); 304 assertTrue(text.endsWith("</html>"), text); 305 assertEquals("<html><br></html>", layer.getToolTipText()); 306 assertDoesNotThrow(() -> layer.jumpToNextMarker()); 307 assertDoesNotThrow(() -> layer.jumpToPreviousMarker()); 308 assertDoesNotThrow(() -> layer.visitBoundingBox(new BoundingXYVisitor())); 309 assertDoesNotThrow(() -> layer.filterTracksByDate(null, null, false)); 310 assertDoesNotThrow(() -> layer.projectionChanged(new CustomProjection(), new CustomProjection())); 311 } 263 312 }
Note:
See TracChangeset
for help on using the changeset viewer.