1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.InputEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
19 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
20 | import org.openstreetmap.josm.io.IncompleteDownloader;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Action that opens a connection to the osm server and download map data.
|
---|
25 | *
|
---|
26 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
27 | * The url and account settings from the preferences are used.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class DownloadIncompleteAction extends JosmAction {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Open the download dialog and download the data.
|
---|
35 | * Run in the worker thread.
|
---|
36 | */
|
---|
37 | private final class DownloadTask extends PleaseWaitRunnable {
|
---|
38 | private IncompleteDownloader reader;
|
---|
39 |
|
---|
40 | private DownloadTask(Collection<Way> toDownload) {
|
---|
41 | super(trn("Downloading {0} segment", "Downloading {0} segments", toDownload.size(), toDownload.size()));
|
---|
42 | reader = new IncompleteDownloader(toDownload);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override public void realRun() throws IOException, SAXException {
|
---|
46 | reader.parse();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override protected void finish() {
|
---|
50 | MergeVisitor merger = new MergeVisitor(Main.ds);
|
---|
51 | for (OsmPrimitive osm : reader.data.allPrimitives())
|
---|
52 | osm.visit(merger);
|
---|
53 | Main.parent.repaint();
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override protected void cancel() {
|
---|
57 | reader.cancel();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public DownloadIncompleteAction() {
|
---|
62 | super(tr("Download incomplete objects"), "downloadincomplete", tr("Download all (selected) incomplete ways from the OSM server."), KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void actionPerformed(ActionEvent e) {
|
---|
66 | Collection<Way> ways = new HashSet<Way>();
|
---|
67 | for (Way w : Main.ds.ways)
|
---|
68 | if (w.isIncomplete() && w.selected)
|
---|
69 | ways.add(w);
|
---|
70 | if (ways.isEmpty()) {
|
---|
71 | JOptionPane.showMessageDialog(Main.parent, tr("Please select an incomplete way."));
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(Main.parent, tr("Download {0} incomplete ways?", ways.size()), tr("Download?"), JOptionPane.YES_NO_OPTION))
|
---|
75 | return;
|
---|
76 | PleaseWaitRunnable task = new DownloadTask(ways);
|
---|
77 | Main.worker.execute(task);
|
---|
78 | }
|
---|
79 | }
|
---|