| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.io.IOException;
|
|---|
| 9 | import java.util.ArrayList;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.AbstractAction;
|
|---|
| 14 | import javax.swing.JLabel;
|
|---|
| 15 | import javax.swing.JOptionPane;
|
|---|
| 16 | import javax.swing.JPanel;
|
|---|
| 17 |
|
|---|
| 18 | import org.openstreetmap.josm.Main;
|
|---|
| 19 | import org.openstreetmap.josm.actions.AbstractMergeAction.LayerListCellRenderer;
|
|---|
| 20 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 21 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 22 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
|---|
| 23 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
|---|
| 24 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 26 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 27 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
|---|
| 28 | import org.openstreetmap.josm.gui.layer.WMSLayer.PrecacheTask;
|
|---|
| 29 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
|---|
| 30 | import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
|
|---|
| 31 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
|---|
| 32 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 33 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 34 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 35 | import org.xml.sax.SAXException;
|
|---|
| 36 |
|
|---|
| 37 | public class DownloadWmsAlongTrackAction extends AbstractAction {
|
|---|
| 38 |
|
|---|
| 39 | private final GpxData data;
|
|---|
| 40 |
|
|---|
| 41 | public DownloadWmsAlongTrackAction(final GpxData data) {
|
|---|
| 42 | super(tr("Precache imagery tiles along this track"), ImageProvider.get("downloadalongtrack"));
|
|---|
| 43 | this.data = data;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | @Override
|
|---|
| 47 | public void actionPerformed(ActionEvent e) {
|
|---|
| 48 | final List<LatLon> points = new ArrayList<LatLon>();
|
|---|
| 49 | for (GpxTrack trk : data.tracks) {
|
|---|
| 50 | for (GpxTrackSegment segment : trk.getSegments()) {
|
|---|
| 51 | for (WayPoint p : segment.getWayPoints()) {
|
|---|
| 52 | points.add(p.getCoor());
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | for (WayPoint p : data.waypoints) {
|
|---|
| 57 | points.add(p.getCoor());
|
|---|
| 58 | }
|
|---|
| 59 | final WMSLayer layer = askWMSLayer();
|
|---|
| 60 | if (layer != null) {
|
|---|
| 61 | PleaseWaitRunnable task = new PleaseWaitRunnable(tr("Precaching WMS")) {
|
|---|
| 62 | private PrecacheTask precacheTask;
|
|---|
| 63 |
|
|---|
| 64 | @Override
|
|---|
| 65 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 66 | precacheTask = new PrecacheTask(progressMonitor);
|
|---|
| 67 | layer.downloadAreaToCache(precacheTask, points, 0, 0);
|
|---|
| 68 | while (!precacheTask.isFinished() && !progressMonitor.isCanceled()) {
|
|---|
| 69 | synchronized (this) {
|
|---|
| 70 | try {
|
|---|
| 71 | wait(200);
|
|---|
| 72 | } catch (InterruptedException e) {
|
|---|
| 73 | Main.warn("InterruptedException in "+getClass().getSimpleName()+" while precaching WMS");
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | @Override
|
|---|
| 80 | protected void finish() {
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | @Override
|
|---|
| 84 | protected void cancel() {
|
|---|
| 85 | precacheTask.cancel();
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | public ProgressTaskId canRunInBackground() {
|
|---|
| 90 | return ProgressTaskIds.PRECACHE_WMS;
|
|---|
| 91 | }
|
|---|
| 92 | };
|
|---|
| 93 | Main.worker.execute(task);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | protected WMSLayer askWMSLayer() {
|
|---|
| 98 | Collection<WMSLayer> targetLayers = Main.map.mapView.getLayersOfType(WMSLayer.class);
|
|---|
| 99 | if (targetLayers.isEmpty()) {
|
|---|
| 100 | warnNoImageryLayers();
|
|---|
| 101 | return null;
|
|---|
| 102 | }
|
|---|
| 103 | JosmComboBox layerList = new JosmComboBox(targetLayers.toArray());
|
|---|
| 104 | layerList.setRenderer(new LayerListCellRenderer());
|
|---|
| 105 | layerList.setSelectedIndex(0);
|
|---|
| 106 | JPanel pnl = new JPanel(new GridBagLayout());
|
|---|
| 107 | pnl.add(new JLabel(tr("Please select the imagery layer.")), GBC.eol());
|
|---|
| 108 | pnl.add(layerList, GBC.eol());
|
|---|
| 109 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Select imagery layer"), new String[]{tr("Download"), tr("Cancel")});
|
|---|
| 110 | ed.setButtonIcons(new String[]{"dialogs/down", "cancel"});
|
|---|
| 111 | ed.setContent(pnl);
|
|---|
| 112 | ed.showDialog();
|
|---|
| 113 | if (ed.getValue() != 1) {
|
|---|
| 114 | return null;
|
|---|
| 115 | }
|
|---|
| 116 | return (WMSLayer) layerList.getSelectedItem();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | protected void warnNoImageryLayers() {
|
|---|
| 120 | JOptionPane.showMessageDialog(Main.parent, tr("There are no imagery layers."), tr("No imagery layers"), JOptionPane.WARNING_MESSAGE);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | }
|
|---|