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

Last change on this file since 2695 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • Property svn:eol-style set to native
File size: 4.0 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.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}
Note: See TracBrowser for help on using the repository browser.