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