- Timestamp:
- 2011-10-16T23:59:38+02:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java
r4310 r4521 9 9 import java.awt.event.ActionEvent; 10 10 import java.awt.event.KeyEvent; 11 import java.util.ArrayList; 11 12 import java.util.Collections; 12 13 import java.util.LinkedList; … … 16 17 import javax.swing.JCheckBox; 17 18 import javax.swing.JLabel; 19 import javax.swing.JOptionPane; 18 20 import javax.swing.JPanel; 21 import javax.swing.SwingUtilities; 19 22 20 23 import org.openstreetmap.josm.Main; 24 import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask; 21 25 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 26 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 22 27 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; 23 28 import org.openstreetmap.josm.gui.ExtendedDialog; … … 33 38 public class OpenLocationAction extends JosmAction { 34 39 40 protected final List<Class<? extends DownloadTask>> downloadTasks; 41 35 42 /** 36 43 * Create an open action. The name is "Open a file". … … 41 48 Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), KeyEvent.VK_L, Shortcut.GROUP_MENU), true); 42 49 putValue("help", ht("/Action/OpenLocation")); 50 this.downloadTasks = new ArrayList<Class<? extends DownloadTask>>(); 51 addDownloadTaskClass(DownloadOsmTask.class); 52 addDownloadTaskClass(DownloadGpsTask.class); 43 53 } 44 54 … … 104 114 105 115 /** 106 * Open the given file.116 * Open the given URL. 107 117 */ 108 public void openUrl(boolean new_layer, String url) { 109 DownloadOsmTask task = new DownloadOsmTask(); 118 public void openUrl(boolean new_layer, final String url) { 110 119 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data")); 111 Future<?> future = task.loadUrl(new_layer, url, monitor); 112 Main.worker.submit(new PostDownloadHandler(task, future)); 120 DownloadTask task = null; 121 Future<?> future = null; 122 for (int i = 0; future == null && i < downloadTasks.size(); i++) { 123 Class<? extends DownloadTask> taskClass = downloadTasks.get(i); 124 if (taskClass != null) { 125 try { 126 task = taskClass.getConstructor().newInstance(); 127 if (task.acceptsUrl(url)) { 128 future = task.loadUrl(new_layer, url, monitor); 129 } 130 } catch (Exception e) { 131 e.printStackTrace(); 132 } 133 } 134 } 135 if (future != null) { 136 Main.worker.submit(new PostDownloadHandler(task, future)); 137 } else { 138 SwingUtilities.invokeLater(new Runnable() { 139 public void run() { 140 JOptionPane.showMessageDialog(Main.parent, tr( 141 "<html>Cannot open URL ''{0}'' because no suitable download task is available.</html>", 142 url), tr("Download Location"), JOptionPane.ERROR_MESSAGE); 143 } 144 }); 145 } 146 } 147 148 public boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) { 149 return this.downloadTasks.add(taskClass); 113 150 } 114 151 } -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java
r4334 r4521 9 9 import org.openstreetmap.josm.Main; 10 10 import org.openstreetmap.josm.data.Bounds; 11 import org.openstreetmap.josm.data.Bounds.ParseMethod; 11 12 import org.openstreetmap.josm.data.gpx.GpxData; 12 13 import org.openstreetmap.josm.gui.PleaseWaitRunnable; … … 15 16 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 16 17 import org.openstreetmap.josm.io.BoundingBoxDownloader; 18 import org.openstreetmap.josm.io.OsmServerLocationReader; 19 import org.openstreetmap.josm.io.OsmServerReader; 17 20 import org.openstreetmap.josm.io.OsmTransferException; 18 21 import org.xml.sax.SAXException; … … 21 24 22 25 private DownloadTask downloadTask; 26 27 private static final String PATTERN_TRACE_ID = "http://.*openstreetmap.org/trace/\\p{Digit}+/data"; 28 29 private static final String PATTERN_TRACKPOINTS_BBOX = "http://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*"; 23 30 24 31 public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) { … … 30 37 } 31 38 32 public Future<?> loadUrl(boolean a,java.lang.String b, ProgressMonitor progressMonitor) { 39 public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) { 40 if (url != null && url.matches(PATTERN_TRACE_ID)) { 41 downloadTask = new DownloadTask(newLayer, 42 new OsmServerLocationReader(url), progressMonitor); 43 // We need submit instead of execute so we can wait for it to finish and get the error 44 // message if necessary. If no one calls getErrorMessage() it just behaves like execute. 45 return Main.worker.submit(downloadTask); 46 47 } else if (url != null && url.matches(PATTERN_TRACKPOINTS_BBOX)) { 48 String[] table = url.split("\\?|=|&"); 49 for (int i = 0; i<table.length; i++) { 50 if (table[i].equals("bbox") && i<table.length-1 ) { 51 return download(newLayer, new Bounds(table[i+1], ",", ParseMethod.LEFT_BOTTOM_RIGHT_TOP), progressMonitor); 52 } 53 } 54 } 33 55 return null; 34 // FIXME this is not currently used 56 } 57 58 /* (non-Javadoc) 59 * @see org.openstreetmap.josm.actions.downloadtasks.DownloadTask#acceptsUrl(java.lang.String) 60 */ 61 @Override 62 public boolean acceptsUrl(String url) { 63 return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX)); 35 64 } 36 65 … … 42 71 43 72 class DownloadTask extends PleaseWaitRunnable { 44 private BoundingBoxDownloader reader;73 private OsmServerReader reader; 45 74 private GpxData rawData; 46 75 private final boolean newLayer; 47 76 48 public DownloadTask(boolean newLayer, BoundingBoxDownloader reader, ProgressMonitor progressMonitor) {77 public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) { 49 78 super(tr("Downloading GPS data")); 50 79 this.reader = reader; -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
r4310 r4521 64 64 return Main.worker.submit(downloadTask); 65 65 } 66 67 /* (non-Javadoc) 68 * @see org.openstreetmap.josm.actions.downloadtasks.DownloadTask#acceptsUrl(java.lang.String) 69 */ 70 @Override 71 public boolean acceptsUrl(String url) { 72 return url != null && url.matches("http://.*/api/0.6/(map|nodes?|ways?|relations?).*"); 73 } 66 74 67 75 public void cancel() { -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java
r3083 r4521 65 65 */ 66 66 Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor); 67 68 /** 69 * Returns true if the task is able to open the given URL, false otherwise. 70 * @param url the url to download from 71 * @return True if the task is able to open the given URL, false otherwise. 72 */ 73 boolean acceptsUrl(String url); 67 74 68 75 /** -
trunk/src/org/openstreetmap/josm/data/Bounds.java
r4423 r4521 30 30 return new LatLon(maxLat, maxLon); 31 31 } 32 32 33 public enum ParseMethod { 34 MINLAT_MINLON_MAXLAT_MAXLON, 35 LEFT_BOTTOM_RIGHT_TOP 36 } 37 33 38 /** 34 39 * Construct bounds out of two points … … 60 65 61 66 public Bounds(String asString, String separator) throws IllegalArgumentException { 67 this(asString, separator, ParseMethod.MINLAT_MINLON_MAXLAT_MAXLON); 68 } 69 70 public Bounds(String asString, String separator, ParseMethod parseMethod) throws IllegalArgumentException { 62 71 CheckParameterUtil.ensureParameterNotNull(asString, "asString"); 63 72 String[] components = asString.split(separator); 64 73 if (components.length != 4) 65 throw new IllegalArgumentException(MessageFormat.format("Exactly four doubles ex cpected in string, got {0}: {1}", components.length, asString));74 throw new IllegalArgumentException(MessageFormat.format("Exactly four doubles expected in string, got {0}: {1}", components.length, asString)); 66 75 double[] values = new double[4]; 67 76 for (int i=0; i<4; i++) { … … 72 81 } 73 82 } 74 if (!LatLon.isValidLat(values[0])) 75 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[0])); 76 if (!LatLon.isValidLon(values[1])) 77 throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", values[1])); 78 if (!LatLon.isValidLat(values[2])) 79 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[2])); 80 if (!LatLon.isValidLon(values[3])) 81 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[3])); 82 83 this.minLat = LatLon.roundToOsmPrecision(values[0]); 84 this.minLon = LatLon.roundToOsmPrecision(values[1]); 85 this.maxLat = LatLon.roundToOsmPrecision(values[2]); 86 this.maxLon = LatLon.roundToOsmPrecision(values[3]); 83 84 int minLatIndex; 85 int minLonIndex; 86 int maxLatIndex; 87 int maxLonIndex; 88 89 switch (parseMethod) { 90 case LEFT_BOTTOM_RIGHT_TOP: 91 minLatIndex = 1; 92 minLonIndex = 0; 93 maxLatIndex = 3; 94 maxLonIndex = 2; 95 break; 96 case MINLAT_MINLON_MAXLAT_MAXLON: 97 default: 98 minLatIndex = 0; 99 minLonIndex = 1; 100 maxLatIndex = 2; 101 maxLonIndex = 3; 102 } 103 104 if (!LatLon.isValidLat(values[minLatIndex])) 105 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[minLatIndex])); 106 if (!LatLon.isValidLon(values[minLonIndex])) 107 throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", values[minLonIndex])); 108 if (!LatLon.isValidLat(values[maxLatIndex])) 109 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[maxLatIndex])); 110 if (!LatLon.isValidLon(values[maxLonIndex])) 111 throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", values[maxLonIndex])); 112 113 this.minLat = LatLon.roundToOsmPrecision(values[minLatIndex]); 114 this.minLon = LatLon.roundToOsmPrecision(values[minLonIndex]); 115 this.maxLat = LatLon.roundToOsmPrecision(values[maxLatIndex]); 116 this.maxLon = LatLon.roundToOsmPrecision(values[maxLonIndex]); 87 117 } 88 118 -
trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java
r2795 r4521 36 36 * ways. 37 37 */ 38 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws IOException, SAXException,OsmTransferException { 38 @Override 39 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException { 39 40 progressMonitor.beginTask("", 1); 40 41 try { … … 70 71 if (cancel) 71 72 return null; 72 throw new SAXException("Illegal characters within the HTTP-header response.", e);73 throw new OsmTransferException("Illegal characters within the HTTP-header response.", e); 73 74 } catch (IOException e) { 74 75 if (cancel) 75 76 return null; 76 throw e;77 throw new OsmTransferException(e); 77 78 } catch (SAXException e) { 78 throw e;79 throw new OsmTransferException(e); 79 80 } catch (OsmTransferException e) { 80 81 throw e; -
trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java
r3083 r4521 6 6 import java.io.InputStream; 7 7 8 import org.openstreetmap.josm.data.gpx.GpxData; 8 9 import org.openstreetmap.josm.data.osm.DataSet; 9 10 import org.openstreetmap.josm.gui.progress.ProgressMonitor; … … 39 40 progressMonitor.finishTask(); 40 41 try { 42 activeConnection = null; 41 43 if (in != null) { 42 44 in.close(); 43 45 } 44 activeConnection = null;45 46 } catch(Exception e) {/* ignore it */} 46 47 } 47 48 } 48 49 50 @Override 51 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException { 52 InputStream in = null; 53 progressMonitor.beginTask(tr("Contacting Server...", 10)); 54 try { 55 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true)); 56 if (in == null) 57 return null; 58 progressMonitor.subTask(tr("Downloading OSM data...")); 59 GpxReader reader = new GpxReader(in); 60 reader.parse(false); 61 GpxData result = reader.data; 62 result.fromServer = true; 63 return result; 64 } catch(OsmTransferException e) { 65 throw e; 66 } catch (Exception e) { 67 if (cancel) 68 return null; 69 throw new OsmTransferException(e); 70 } finally { 71 progressMonitor.finishTask(); 72 try { 73 activeConnection = null; 74 if (in != null) { 75 in.close(); 76 } 77 } catch(Exception e) {/* ignore it */} 78 } 79 } 49 80 } -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r4310 r4521 16 16 17 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.data.gpx.GpxData; 18 19 import org.openstreetmap.josm.data.osm.DataSet; 19 20 import org.openstreetmap.josm.gui.progress.ProgressMonitor; … … 141 142 public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException; 142 143 144 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException { 145 return null; 146 } 147 143 148 /** 144 149 * Returns true if this reader is adding authentication credentials to the read
Note:
See TracChangeset
for help on using the changeset viewer.