source: josm/trunk/src/org/openstreetmap/josm/actions/search/SelectionWebsiteLoader.java@ 1811

Last change on this file since 1811 was 1811, checked in by jttt, 15 years ago

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2/**
3 *
4 */
5package org.openstreetmap.josm.actions.search;
6
7import static org.openstreetmap.josm.tools.I18n.tr;
8
9import java.io.IOException;
10import java.io.InputStream;
11import java.net.MalformedURLException;
12import java.net.URL;
13import java.net.URLConnection;
14import java.util.Collection;
15import java.util.LinkedList;
16import java.util.Map;
17
18import javax.swing.JOptionPane;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.gui.PleaseWaitRunnable;
23import org.openstreetmap.josm.io.OsmIdReader;
24import org.openstreetmap.josm.io.OsmTransferException;
25import org.openstreetmap.josm.io.ProgressInputStream;
26import org.xml.sax.SAXException;
27
28public 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.ds.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.ds.allNonDeletedPrimitives()) {
50 if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) {
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(Main.parent, tr("Could not read from URL: \"{0}\"",url));
62 } catch (SAXException e) {
63 e.printStackTrace();
64 JOptionPane.showMessageDialog(Main.parent,tr("Parsing error in URL: \"{0}\"",url));
65 } catch(OsmTransferException e) {
66 e.printStackTrace();
67 if (e.getCause() != null) {
68 if (e.getCause() instanceof IOException ) {
69 JOptionPane.showMessageDialog(Main.parent, tr("Could not read from URL: \"{0}\"",url),
70 tr("Error"), JOptionPane.ERROR_MESSAGE);
71 } else if (e.getCause() instanceof SAXException) {
72 JOptionPane.showMessageDialog(Main.parent,tr("Parsing error in URL: \"{0}\"",url),
73 tr("Error"), JOptionPane.ERROR_MESSAGE);
74 }
75 } else {
76 JOptionPane.showMessageDialog(Main.parent,tr("Error while communicating with server.",url),
77 tr("Error"), JOptionPane.ERROR_MESSAGE);
78 }
79
80 }
81 }
82 @Override protected void cancel() {
83 sel = null;
84 idReader.cancel();
85 }
86 @Override protected void finish() {
87 if (sel != null) {
88 Main.ds.setSelected(sel);
89 }
90 }
91}
Note: See TracBrowser for help on using the repository browser.