1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | */
|
---|
5 | package org.openstreetmap.josm.actions.search;
|
---|
6 |
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.net.MalformedURLException;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.net.URLConnection;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
23 | import org.openstreetmap.josm.io.OsmIdReader;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.openstreetmap.josm.io.ProgressInputStream;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | public class SelectionWebsiteLoader extends PleaseWaitRunnable {
|
---|
29 | public final URL url;
|
---|
30 | public Collection<OsmPrimitive> sel;
|
---|
31 | private final SearchAction.SearchMode mode;
|
---|
32 | private OsmIdReader idReader = new OsmIdReader();
|
---|
33 | public SelectionWebsiteLoader(String urlStr, SearchAction.SearchMode mode) {
|
---|
34 | super(tr("Load Selection"));
|
---|
35 | this.mode = mode;
|
---|
36 | URL u = null;
|
---|
37 | try {u = new URL(urlStr);} catch (MalformedURLException e) {}
|
---|
38 | this.url = u;
|
---|
39 | }
|
---|
40 | @Override protected void realRun() {
|
---|
41 | progressMonitor.setTicksCount(2);
|
---|
42 | sel = mode != SearchAction.SearchMode.remove ? new LinkedList<OsmPrimitive>() : Main.main.getCurrentDataSet().allNonDeletedPrimitives();
|
---|
43 | try {
|
---|
44 | URLConnection con = url.openConnection();
|
---|
45 | progressMonitor.subTask(tr("Contact {0}...", url.getHost()));
|
---|
46 | InputStream in = new ProgressInputStream(con, progressMonitor.createSubTaskMonitor(1, true));
|
---|
47 | progressMonitor.subTask(tr("Downloading..."));
|
---|
48 | Map<Long, String> ids = idReader.parseIds(in);
|
---|
49 | for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedPrimitives()) {
|
---|
50 | if (ids.containsKey(osm.getId()) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.getId()))) {
|
---|
51 | if (mode == SearchAction.SearchMode.remove) {
|
---|
52 | sel.remove(osm);
|
---|
53 | } else {
|
---|
54 | sel.add(osm);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | progressMonitor.worked(1);
|
---|
59 | } catch (IOException e) {
|
---|
60 | e.printStackTrace();
|
---|
61 | JOptionPane.showMessageDialog(
|
---|
62 | Main.parent,
|
---|
63 | tr("Could not read from URL: \"{0}\"",url),
|
---|
64 | tr("Error"),
|
---|
65 | JOptionPane.ERROR_MESSAGE
|
---|
66 | );
|
---|
67 | } catch (SAXException e) {
|
---|
68 | e.printStackTrace();
|
---|
69 | JOptionPane.showMessageDialog(
|
---|
70 | Main.parent,
|
---|
71 | tr("Parsing error in URL: \"{0}\"",url),
|
---|
72 | tr("Error"),
|
---|
73 | JOptionPane.ERROR_MESSAGE
|
---|
74 | );
|
---|
75 | } catch(OsmTransferException e) {
|
---|
76 | e.printStackTrace();
|
---|
77 | if (e.getCause() != null) {
|
---|
78 | if (e.getCause() instanceof IOException ) {
|
---|
79 | JOptionPane.showMessageDialog(
|
---|
80 | Main.parent, tr("Could not read from URL: \"{0}\"",url),
|
---|
81 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
82 | } else if (e.getCause() instanceof SAXException) {
|
---|
83 | JOptionPane.showMessageDialog(Main.parent,tr("Parsing error in URL: \"{0}\"",url),
|
---|
84 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | JOptionPane.showMessageDialog(Main.parent,tr("Error while communicating with server.",url),
|
---|
88 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 | }
|
---|
93 | @Override protected void cancel() {
|
---|
94 | sel = null;
|
---|
95 | idReader.cancel();
|
---|
96 | }
|
---|
97 | @Override protected void finish() {
|
---|
98 | if (sel != null) {
|
---|
99 | Main.main.getCurrentDataSet().setSelected(sel);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|