source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java

Last change on this file was 16438, checked in by simon04, 5 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.gpx;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.io.IOException;
8import java.util.List;
9import java.util.stream.Collectors;
10
11import javax.swing.AbstractAction;
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
15import org.openstreetmap.josm.actions.AbstractMergeAction;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.gpx.GpxData;
18import org.openstreetmap.josm.data.gpx.WayPoint;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.PleaseWaitRunnable;
21import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
22import org.openstreetmap.josm.gui.progress.ProgressTaskId;
23import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
24import org.openstreetmap.josm.io.OsmTransferException;
25import org.openstreetmap.josm.tools.ImageProvider;
26import org.openstreetmap.josm.tools.Logging;
27import org.xml.sax.SAXException;
28
29/**
30 * Class downloading WMS and TMS along the GPX track.
31 * @since 5715
32 */
33public class DownloadWmsAlongTrackAction extends AbstractAction {
34
35 private final transient GpxData data;
36
37 /**
38 * @param data that represents GPX track, along which data should be downloaded
39 */
40 public DownloadWmsAlongTrackAction(final GpxData data) {
41 super(tr("Precache imagery tiles along this track"));
42 new ImageProvider("downloadalongtrack").getResource().attachImageIcon(this, true);
43 this.data = data;
44 }
45
46 static class PrecacheWmsTask extends PleaseWaitRunnable {
47
48 private final AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer;
49 private final List<LatLon> points;
50 private AbstractTileSourceLayer<? extends AbstractTMSTileSource>.PrecacheTask precacheTask;
51
52 protected PrecacheWmsTask(AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer, List<LatLon> points) {
53 super(tr("Precaching WMS"));
54 this.layer = layer;
55 this.points = points;
56 }
57
58 @Override
59 protected void realRun() throws SAXException, IOException, OsmTransferException {
60 precacheTask = layer.getDownloadAreaToCacheTask(progressMonitor, points, 0, 0);
61 precacheTask.run();
62 synchronized (this) {
63 try {
64 while (!precacheTask.isFinished() && !progressMonitor.isCanceled()) {
65 wait(200);
66 }
67 } catch (InterruptedException ex) {
68 Logging.warn("InterruptedException in "+getClass().getSimpleName()+" while precaching WMS");
69 Thread.currentThread().interrupt();
70 }
71 }
72 }
73
74 @Override
75 protected void finish() {
76 // Do nothing
77 }
78
79 @Override
80 protected void cancel() {
81 precacheTask.cancel();
82 }
83
84 @Override
85 public ProgressTaskId canRunInBackground() {
86 return ProgressTaskIds.PRECACHE_WMS;
87 }
88 }
89
90 PrecacheWmsTask createTask() {
91 List<LatLon> points = data.tracks.stream()
92 .flatMap(trk -> trk.getSegments().stream())
93 .flatMap(segment -> segment.getWayPoints().stream())
94 .map(WayPoint::getCoor)
95 .collect(Collectors.toList());
96 for (WayPoint p : data.waypoints) {
97 points.add(p.getCoor());
98 }
99 AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer = askedLayer();
100 return layer != null ? new PrecacheWmsTask(layer, points) : null;
101 }
102
103 @Override
104 public void actionPerformed(ActionEvent e) {
105 PrecacheWmsTask task = createTask();
106 if (task != null) {
107 MainApplication.worker.execute(task);
108 }
109 }
110
111 @SuppressWarnings({ "rawtypes", "unchecked" })
112 protected AbstractTileSourceLayer<? extends AbstractTMSTileSource> askedLayer() {
113 List<AbstractTileSourceLayer> targetLayers = MainApplication.getLayerManager().getLayersOfType(AbstractTileSourceLayer.class);
114 if (targetLayers.isEmpty()) {
115 warnNoImageryLayers();
116 return null;
117 }
118 return AbstractMergeAction.askTargetLayer(targetLayers.toArray(new AbstractTileSourceLayer[0]),
119 tr("Please select the imagery layer."),
120 tr("Select imagery layer"),
121 tr("Download"), "dialogs/down");
122 }
123
124 protected void warnNoImageryLayers() {
125 JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
126 tr("There are no imagery layers."), tr("No imagery layers"), JOptionPane.WARNING_MESSAGE);
127 }
128}
Note: See TracBrowser for help on using the repository browser.