- Timestamp:
- 2013-01-29T21:40:59+01:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java
r5361 r5691 10 10 import java.awt.event.KeyEvent; 11 11 import java.util.ArrayList; 12 import java.util.Collection; 12 13 import java.util.Collections; 13 14 import java.util.LinkedList; … … 120 121 openUrl(layer.isSelected(), uploadAddresses.getText()); 121 122 } 122 123 123 124 /** 124 * Open the given URL. 125 * Replies the list of download tasks accepting the given url. 126 * @param url The URL to open 127 * @return The list of download tasks accepting the given url. 128 * @since 5691 125 129 */ 126 public void openUrl(boolean new_layer, final String url) { 127 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data")); 128 DownloadTask task = null; 129 Future<?> future = null; 130 for (int i = 0; future == null && i < downloadTasks.size(); i++) { 130 public Collection<DownloadTask> findDownloadTasks(final String url) { 131 List<DownloadTask> result = new ArrayList<DownloadTask>(); 132 for (int i = 0; i < downloadTasks.size(); i++) { 131 133 Class<? extends DownloadTask> taskClass = downloadTasks.get(i); 132 134 if (taskClass != null) { 133 135 try { 134 task = taskClass.getConstructor().newInstance();136 DownloadTask task = taskClass.getConstructor().newInstance(); 135 137 if (task.acceptsUrl(url)) { 136 future = task.loadUrl(new_layer, url, monitor);138 result.add(task); 137 139 } 138 140 } catch (Exception e) { … … 140 142 } 141 143 } 144 } 145 return result; 146 } 147 148 /** 149 * Open the given URL. 150 * @param new_layer true if the URL needs to be opened in a new layer, false otherwise 151 * @param url The URL to open 152 */ 153 public void openUrl(boolean new_layer, final String url) { 154 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data")); 155 Collection<DownloadTask> tasks = findDownloadTasks(url); 156 DownloadTask task = null; 157 Future<?> future = null; 158 if (!tasks.isEmpty()) { 159 // TODO: handle multiple suitable tasks ? 160 future = tasks.iterator().next().loadUrl(new_layer, url, monitor); 142 161 } 143 162 if (future != null) { -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java
r5501 r5691 5 5 6 6 import java.io.IOException; 7 import java.net.URL; 7 8 import java.util.concurrent.Future; 8 9 import java.util.regex.Matcher; … … 178 179 } 179 180 } 181 182 @Override 183 public String getConfirmationMessage(URL url) { 184 // TODO 185 return null; 186 } 180 187 } -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
r5663 r5691 6 6 import java.awt.geom.Area; 7 7 import java.io.IOException; 8 import java.net.URL; 8 9 import java.util.ArrayList; 9 10 import java.util.Collection; … … 17 18 18 19 import javax.swing.JOptionPane; 20 19 21 import org.openstreetmap.josm.Main; 20 22 import org.openstreetmap.josm.data.Bounds; … … 45 47 */ 46 48 public class DownloadOsmTask extends AbstractDownloadTask { 49 50 private static final String PATTERN_OSM_API_URL = "http://.*/api/0.6/(map|nodes?|ways?|relations?|\\*).*"; 51 private static final String PATTERN_OVERPASS_API_URL = "http://.*/interpreter\\?data=.*"; 52 private static final String PATTERN_OVERPASS_API_XAPI_URL = "http://.*/xapi\\?.*\\[@meta\\].*"; 53 private static final String PATTERN_EXTERNAL_OSM_FILE = "https?://.*/.*\\.osm"; 54 47 55 protected Bounds currentBounds; 48 56 protected DataSet downloadedData; … … 145 153 public boolean acceptsUrl(String url) { 146 154 return url != null && ( 147 url.matches( "http://.*/api/0.6/(map|nodes?|ways?|relations?|\\*).*")// OSM API 0.6 and XAPI148 || url.matches( "http://.*/interpreter\\?data=.*")// Overpass API149 || url.matches( "http://.*/xapi\\?.*\\[@meta\\].*")// Overpass API XAPI compatibility layer150 || url.matches( "https?://.*/.*\\.osm")// Remote .osm files155 url.matches(PATTERN_OSM_API_URL) // OSM API 0.6 and XAPI 156 || url.matches(PATTERN_OVERPASS_API_URL) // Overpass API 157 || url.matches(PATTERN_OVERPASS_API_XAPI_URL) // Overpass API XAPI compatibility layer 158 || url.matches(PATTERN_EXTERNAL_OSM_FILE) // Remote .osm files 151 159 ); 152 160 } … … 352 360 } 353 361 } 362 363 @Override 364 public String getConfirmationMessage(URL url) { 365 if (url != null) { 366 String urlString = url.toExternalForm(); 367 if (urlString.matches(PATTERN_OSM_API_URL)) { 368 // TODO: proper i18n after stabilization 369 String message = "<ul><li>"+tr("OSM Server URL:") + " " + url.getHost() + "</li><li>" + 370 tr("Command")+": "+url.getPath()+"</li>"; 371 if (url.getQuery() != null) { 372 message += "<li>" + tr("Request details: {0}", url.getQuery().replaceAll(",\\s*", ", ")) + "</li>"; 373 } 374 message += "</ul>"; 375 return message; 376 } 377 // TODO: other APIs 378 } 379 return null; 380 } 354 381 } -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java
r5402 r5691 2 2 package org.openstreetmap.josm.actions.downloadtasks; 3 3 4 import java.net.URL; 4 5 import java.util.List; 5 6 import java.util.concurrent.Future; … … 92 93 */ 93 94 public void cancel(); 95 96 /** 97 * Replies the HTML-formatted confirmation message to be shown to user when the given URL needs to be confirmed before loading. 98 * @param url The URL to be confirmed 99 * @return The HTML-formatted confirmation message to be shown to user 100 * @since 101 */ 102 public String getConfirmationMessage(URL url); 94 103 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java
r5680 r5691 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 import java.util.Collection; 6 9 import java.util.HashMap; 7 10 11 import org.openstreetmap.josm.Main; 8 12 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 9 13 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; … … 19 23 */ 20 24 public static final String command = "import"; 25 26 private URL url; 27 private Collection<DownloadTask> suitableDownloadTasks; 21 28 22 29 @Override 23 30 protected void handleRequest() throws RequestHandlerErrorException { 24 31 try { 25 DownloadTask osmTask = new DownloadOsmTask(); 26 osmTask.loadUrl(false, args.get("url"), null); 32 if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) { 33 // TODO: add new_layer parameter 34 // TODO: handle multiple suitable download tasks ? 35 suitableDownloadTasks.iterator().next().loadUrl(false, url.toExternalForm(), null); 36 } 27 37 } catch (Exception ex) { 28 38 System.out.println("RemoteControl: Error parsing import remote control request:"); … … 39 49 @Override 40 50 public String getPermissionMessage() { 51 // URL can be any suitable URL giving back OSM data, including OSM API calls, even if calls to the main API 52 // should rather be passed to LoadAndZoomHandler or LoadObjectHandler. 53 // Other API instances will however use the import handler to force JOSM to make requests to this API instance. 54 // (Example with OSM-FR website that makes calls to the OSM-FR API) 55 // For user-friendliness, let's try to decode these OSM API calls to give a better confirmation message. 56 String taskMessage = null; 57 if (suitableDownloadTasks != null && !suitableDownloadTasks.isEmpty()) { 58 // TODO: handle multiple suitable download tasks ? 59 taskMessage = suitableDownloadTasks.iterator().next().getConfirmationMessage(url); 60 } 41 61 return tr("Remote Control has been asked to import data from the following URL:") 42 + "<br>" + args.get("url");62 + "<br>" + (taskMessage == null ? url.toString() : taskMessage); 43 63 } 44 64 … … 80 100 @Override 81 101 protected void validateRequest() throws RequestHandlerBadRequestException { 82 // Nothing to do 102 final String urlString = args.get("url"); 103 try { 104 // Ensure the URL is valid 105 url = new URL(urlString); 106 } catch (MalformedURLException e) { 107 throw new RequestHandlerBadRequestException("MalformedURLException: "+e.getMessage()); 108 } 109 // Find download tasks for the given URL 110 suitableDownloadTasks = Main.main.menu.openLocation.findDownloadTasks(urlString); 111 if (suitableDownloadTasks.isEmpty()) { 112 // It should maybe be better to reject the request in that case ? 113 // For compatibility reasons with older instances of JOSM, arbitrary choice of DownloadOsmTask 114 suitableDownloadTasks.add(new DownloadOsmTask()); 115 } 83 116 } 84 117 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
r5680 r5691 11 11 import java.util.List; 12 12 13 import javax.swing.JLabel; 13 14 import javax.swing.JOptionPane; 14 15 … … 129 130 */ 130 131 if (Main.pref.getBoolean(globalConfirmationKey, globalConfirmationDefault)) { 131 if (JOptionPane.showConfirmDialog(Main.parent, 132 "<html>" + getPermissionMessage() + 133 "<br>" + tr("Do you want to allow this?"), 132 // Ensure dialog box does not exceed main window size 133 Integer maxWidth = (int) Math.max(200, Main.parent.getWidth()*0.6); 134 String message = "<html><div>" + getPermissionMessage() + 135 "<br/>" + tr("Do you want to allow this?") + "</div></html>"; 136 JLabel label = new JLabel(message); 137 if (label.getPreferredSize().width > maxWidth) { 138 label.setText(message.replaceFirst("<div>", "<div style=\"width:" + maxWidth + "px;\">")); 139 } 140 if (JOptionPane.showConfirmDialog(Main.parent, label, 134 141 tr("Confirm Remote Control action"), 135 142 JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
Note:
See TracChangeset
for help on using the changeset viewer.