Changeset 1670 in josm for trunk/src/org
- Timestamp:
- 2009-06-15T20:22:46+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 14 added
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/UpdateDataAction.java
r1465 r1670 15 15 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTaskList; 16 16 import org.openstreetmap.josm.data.osm.DataSource; 17 import org.openstreetmap.josm.gui.ExtendedDialog;18 17 import org.openstreetmap.josm.tools.Shortcut; 19 18 … … 26 25 tr("Update Data"), 27 26 KeyEvent.VK_U, 28 Shortcut.GROUP_ NONE),29 true);27 Shortcut.GROUP_HOTKEY), 28 true); 30 29 } 31 30 … … 33 32 int bboxCount = 0; 34 33 List<Area> areas = new ArrayList<Area>(); 35 for(DataSource ds : Main.main.editLayer().data.dataSources) 34 for(DataSource ds : Main.main.editLayer().data.dataSources) { 36 35 areas.add(new Area(ds.bounds.asRect())); 37 38 // This would loop over all DataLayers but download all data to the currently 39 // selected one 40 /*for(Layer l : Main.map.mapView.getAllLayers()) { 41 if(!(l instanceof OsmDataLayer)) continue; 42 43 for(DataSource ds : ((OsmDataLayer)l).data.dataSources) 44 areas.add(new Area(ds.bounds.asRect())); 45 }*/ 36 } 46 37 47 38 // The next two blocks removes every intersection from every DataSource Area … … 62 53 63 54 for(Area a : areas) { 64 if(a.isEmpty()) 55 if(a.isEmpty()) { 65 56 continue; 57 } 66 58 bboxCount++; 67 59 } … … 69 61 if(bboxCount == 0) { 70 62 JOptionPane.showMessageDialog(Main.parent, 71 72 63 tr("No data to update found. Have you already opened or downloaded a data layer?")); 64 return; 73 65 } 74 75 int result = new ExtendedDialog(Main.parent,76 tr("Update Data"),77 tr("This action will require {0} individual download requests. "78 + "Do you wish to continue?", bboxCount),79 new String[] { tr("Update Data"), tr("Cancel") },80 new String[] { "updatedata.png", "cancel.png" }).getValue();81 82 if(result != 1)83 return;84 66 85 67 new DownloadOsmTaskList().download(false, areas); 86 68 } 87 88 69 } -
trunk/src/org/openstreetmap/josm/actions/UploadAction.java
r1668 r1670 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.EventQueue;7 6 import java.awt.GridBagLayout; 8 7 import java.awt.event.ActionEvent; … … 13 12 import java.util.LinkedList; 14 13 import java.util.List; 15 import java.util.concurrent.ExecutionException; 16 import java.util.concurrent.FutureTask; 14 import java.util.logging.Logger; 17 15 import java.util.regex.Matcher; 18 16 import java.util.regex.Pattern; … … 23 21 import javax.swing.JPanel; 24 22 import javax.swing.JScrollPane; 25 import javax.swing.SwingUtilities;26 23 27 24 import org.openstreetmap.josm.Main; 28 25 import org.openstreetmap.josm.data.osm.OsmPrimitive; 29 import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;30 26 import org.openstreetmap.josm.gui.ExtendedDialog; 31 27 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 32 28 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 33 29 import org.openstreetmap.josm.gui.historycombobox.SuggestingJHistoryComboBox; 34 import org.openstreetmap.josm.io.DiffResultReader;35 30 import org.openstreetmap.josm.io.OsmApi; 36 31 import org.openstreetmap.josm.io.OsmApiException; … … 47 42 * An dialog is displayed asking the user to specify a rectangle to grab. 48 43 * The url and account settings from the preferences are used. 44 * 45 * If the upload fails this action offers various options to resolve conflicts. 49 46 * 50 47 * @author imi 51 48 */ 52 49 public class UploadAction extends JosmAction { 50 static private Logger logger = Logger.getLogger(UploadAction.class.getName()); 53 51 54 52 public static final String HISTORY_KEY = "upload.comment.history"; … … 215 213 return; 216 214 } 217 System.out.println("got exception: " + sxe.toString());218 215 uploadFailed = true; 219 216 lastException = sxe; … … 230 227 server.disconnectActiveConnection(); 231 228 uploadCancelled = true; 232 } ;233 234 } 229 } 230 } 231 235 232 Main.worker.execute(new UploadDiffTask()); 236 233 } 237 234 238 public void handleFailedUpload(Exception e) { 235 /** 236 * Synchronizes the local state of an {@see OsmPrimitive} with its state on the 237 * server. The method uses an individual GET for the primitive. 238 * 239 * @param id the primitive ID 240 */ 241 protected void synchronizePrimitive(final String id) { 242 243 /** 244 * The asynchronous task to update a a specific id 245 * 246 */ 247 class UpdatePrimitiveTask extends PleaseWaitRunnable { 248 249 private boolean uploadCancelled = false; 250 private boolean uploadFailed = false; 251 private Exception lastException = null; 252 253 public UpdatePrimitiveTask() { 254 super(tr("Updating primitive"),false /* don't ignore exceptions */); 255 } 256 257 @Override protected void realRun() throws SAXException, IOException { 258 try { 259 UpdateSelectionAction act = new UpdateSelectionAction(); 260 act.updatePrimitive(Long.parseLong(id)); 261 } catch (Exception sxe) { 262 if (uploadCancelled) { 263 System.out.println("Ignoring exception caught because upload is cancelled. Exception is: " + sxe.toString()); 264 return; 265 } 266 uploadFailed = true; 267 lastException = sxe; 268 } 269 } 270 271 @Override protected void finish() { 272 if (uploadFailed) { 273 handleFailedUpload(lastException); 274 } 275 } 276 277 @Override protected void cancel() { 278 OsmApi.getOsmApi().cancel(); 279 uploadCancelled = true; 280 } 281 } 282 283 Main.worker.execute(new UpdatePrimitiveTask()); 284 } 285 286 /** 287 * Synchronizes the local state of the dataset with the state on the server. 288 * 289 * Reuses the functionality of {@see UpdateDataAction}. 290 * 291 * @see UpdateDataAction#actionPerformed(ActionEvent) 292 */ 293 protected void synchronizeDataSet() { 294 UpdateDataAction act = new UpdateDataAction(); 295 act.actionPerformed(new ActionEvent(this,0,"")); 296 } 297 298 /** 299 * Handles the case that a conflict in a specific {@see OsmPrimitive} was detected while 300 * uploading 301 * 302 * @param primitiveType the type of the primitive, either <code>node</code>, <code>way</code> or 303 * <code>relation</code> 304 * @param id the id of the primitive 305 * @param serverVersion the version of the primitive on the server 306 * @param myVersion the version of the primitive in the local dataset 307 */ 308 protected void handleUploadConflictForKnownConflict(String primitiveType, String id, String serverVersion, String myVersion) { 309 Object[] options = new Object[] { 310 tr("Synchronize {0} {1} only", tr(primitiveType), id), 311 tr("Synchronize entire dataset"), 312 tr("Cancel") 313 }; 314 Object defaultOption = options[0]; 315 String msg = tr("<html>Uploading <strong>failed</strong> because the server has a newer version of one<br>" 316 + "of your nodes, ways, or relations.<br>" 317 + "The conflict is caused by the <strong>{0}</strong> with id <strong>{1}</strong>,<br>" 318 + "the server has version {2}, your version is {3}.<br>" 319 + "<br>" 320 + "Click <strong>{4}</strong> to synchronize the conflicting primitive only.<br>" 321 + "Click <strong>{5}</strong> to synchronize the entire local dataset with the server.<br>" 322 + "Click <strong>{6}</strong> to abort and continue editing.<br></html>", 323 tr(primitiveType), id, serverVersion, myVersion, 324 options[0], options[1], options[2] 325 ); 326 int optionsType = JOptionPane.YES_NO_CANCEL_OPTION; 327 int ret = JOptionPane.showOptionDialog( 328 null, 329 msg, 330 tr("Conflict detected"), 331 optionsType, 332 JOptionPane.ERROR_MESSAGE, 333 null, 334 options, 335 defaultOption 336 ); 337 switch(ret) { 338 case JOptionPane.CLOSED_OPTION: return; 339 case JOptionPane.CANCEL_OPTION: return; 340 case 0: synchronizePrimitive(id); break; 341 case 1: synchronizeDataSet(); break; 342 default: 343 // should not happen 344 throw new IllegalStateException(tr("unexpected return value. Got {0}", ret)); 345 } 346 } 347 348 /** 349 * Handles the case that a conflict was detected while uploading where we don't 350 * know what {@see OsmPrimitive} actually caused the conflict (for whatever reason) 351 * 352 */ 353 protected void handleUploadConflictForUnknownConflict() { 354 Object[] options = new Object[] { 355 tr("Synchronize entire dataset"), 356 tr("Cancel") 357 }; 358 Object defaultOption = options[0]; 359 String msg = tr("<html>Uploading <strong>failed</strong> because the server has a newer version of one<br>" 360 + "of your nodes, ways, or relations.<br>" 361 + "<br>" 362 + "Click <strong>{0}</strong> to synchronize the entire local dataset with the server.<br>" 363 + "Click <strong>{1}</strong> to abort and continue editing.<br></html>", 364 options[0], options[1] 365 ); 366 int optionsType = JOptionPane.YES_NO_OPTION; 367 int ret = JOptionPane.showOptionDialog( 368 null, 369 msg, 370 tr("Conflict detected"), 371 optionsType, 372 JOptionPane.ERROR_MESSAGE, 373 null, 374 options, 375 defaultOption 376 ); 377 switch(ret) { 378 case JOptionPane.CLOSED_OPTION: return; 379 case 1: return; 380 case 0: synchronizeDataSet(); break; 381 default: 382 // should not happen 383 throw new IllegalStateException(tr("unexpected return value. Got {0}", ret)); 384 } 385 } 386 387 /** 388 * handles an upload conflict, i.e. an error indicated by a HTTP return code 409. 389 * 390 * @param e the exception 391 */ 392 protected void handleUploadConflict(OsmApiException e) { 393 String pattern = "Version mismatch: Provided (\\d+), server had: (\\d+) of (\\S+) (\\d+)"; 394 Pattern p = Pattern.compile(pattern); 395 Matcher m = p.matcher(e.getErrorHeader()); 396 if (m.matches()) { 397 handleUploadConflictForKnownConflict(m.group(3), m.group(4), m.group(2),m.group(1)); 398 } else { 399 logger.warning(tr("Warning: error header \"{0}\" did not match expected pattern \"{1}\"", e.getErrorHeader(),pattern)); 400 handleUploadConflictForUnknownConflict(); 401 } 402 } 403 404 /** 405 * Handles an upload error due to a violated precondition, i.e. a HTTP return code 412 406 * 407 * @param e the exception 408 */ 409 protected void handlePreconditionFailed(OsmApiException e) { 410 JOptionPane.showMessageDialog( 411 Main.parent, 412 tr("<html>Uploading to the server <strong>failed</strong> because your current<br>" 413 +"dataset violates a precondition.<br>" 414 +"The error message is:<br>" 415 + "{0}" 416 + "</html>", 417 e.getMessage() 418 ), 419 tr("Precondition violation"), 420 JOptionPane.ERROR_MESSAGE 421 ); 422 e.printStackTrace(); 423 } 424 425 426 /** 427 * Handles an error due to a delete request on an already deleted 428 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we know what 429 * {@see OsmPrimitive} is responsible for the error. 430 * 431 * Reuses functionality of the {@see UpdateSelectionAction} to resolve 432 * conflicts due to mismatches in the deleted state. 433 * 434 * @param primitiveType the type of the primitive 435 * @param id the id of the primitive 436 * 437 * @see UpdateSelectionAction#handlePrimitiveGoneException(long) 438 */ 439 protected void handleGoneForKnownPrimitive(String primitiveType, String id) { 440 UpdateSelectionAction act = new UpdateSelectionAction(); 441 act.handlePrimitiveGoneException(Long.parseLong(id)); 442 } 443 444 /** 445 * handles the case of an error due to a delete request on an already deleted 446 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which 447 * {@see OsmPrimitive} is causing the error. 448 * 449 * @param e the exception 450 */ 451 protected void handleGoneForUnknownPrimitive(OsmApiException e) { 452 String msg = tr("<html>Uploading <strong>failed</strong> because a primitive you tried to<br>" 453 + "delete on the server is already deleted.<br>" 454 + "<br>" 455 + "The error message is:<br>" 456 + "{0}" 457 + "</html>", 458 e.getMessage() 459 ); 460 JOptionPane.showMessageDialog( 461 Main.parent, 462 msg, 463 tr("Primitive already deleted"), 464 JOptionPane.ERROR_MESSAGE 465 ); 466 467 } 468 469 /** 470 * Handles an error which is caused by a delete request for an already deleted 471 * {@see OsmPrimitive} on the server, i.e. a HTTP response code of 410. 472 * Note that an <strong>update</strong> on an already deleted object results 473 * in a 409, not a 410. 474 * 475 * @param e the exception 476 */ 477 protected void handleGone(OsmApiException e) { 478 String pattern = "The (\\S+) with the id (\\d+) has already been deleted"; 479 Pattern p = Pattern.compile(pattern); 480 Matcher m = p.matcher(e.getErrorHeader()); 481 if (m.matches()) { 482 handleGoneForKnownPrimitive(m.group(1), m.group(2)); 483 } else { 484 logger.warning(tr("Error header \"{0}\" doesn't match expected pattern \"{1}\"",e.getErrorHeader(), pattern)); 485 handleGoneForUnknownPrimitive(e); 486 } 487 } 488 489 490 /** 491 * error handler for any exception thrown during upload 492 * 493 * @param e the exception 494 */ 495 protected void handleFailedUpload(Exception e) { 496 // API initialization failed. Notify the user and return. 497 // 239 498 if (e instanceof OsmApiInitializationException) { 240 handleOsmApiInitializationException( e);499 handleOsmApiInitializationException((OsmApiInitializationException)e); 241 500 return; 242 501 } 502 243 503 if (e instanceof OsmApiException) { 244 504 OsmApiException ex = (OsmApiException)e; 505 // There was an upload conflict. Let the user decide whether 506 // and how to resolve it 507 // 245 508 if(ex.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) { 246 Pattern p = Pattern.compile("Version mismatch: Provided (\\d+), server had: (\\d+) of (\\S+) (\\d+)"); 247 Matcher m = p.matcher(ex.getErrorHeader()); 248 String msg; 249 if (m.matches()) { 250 msg = tr("<html>Uploading <strong>failed</strong> because the server has a newer version of one<br>" 251 + "of your nodes, ways or relations.<br>" 252 + "The conflict is caused by the <strong>{0}</strong> with id <strong>{1}</strong>,<br>" 253 + "the server has version {2}, your version is {3}.<br>" 254 + "Please synchronize your local dataset using <br>" 255 + "<strong>File->Update Data</strong>, resolve<br>" 256 + "any conflicts and try to upload again.</html>", 257 m.group(3),m.group(4), m.group(2), m.group(1) 258 ); 259 } else { 260 msg = tr("<html>Uploading failed because the server has a newer version of one<br>" 261 + "of your nodes, ways or relations.<br>" 262 + "Please synchronize your local dataset using <br>" 263 + "<strong>File->Update Data</strong>, resolve<br>" 264 + "any conflicts and try to upload again.</html>" 265 ); 266 } 267 JOptionPane.showMessageDialog( 268 null, 269 msg, 270 tr("Upload to OSM API failed"), 271 JOptionPane.WARNING_MESSAGE 272 ); 509 handleUploadConflict(ex); 273 510 return; 274 511 } 275 } 512 // There was a precondition failed. Notify the user. 513 // 514 else if (ex.getResponseCode() == HttpURLConnection.HTTP_PRECON_FAILED) { 515 handlePreconditionFailed(ex); 516 return; 517 } 518 // Tried to delete an already deleted primitive? Let the user 519 // decide whether and how to resolve this conflict. 520 // 521 else if (ex.getResponseCode() == HttpURLConnection.HTTP_GONE) { 522 handleGone(ex); 523 return; 524 } 525 } 526 527 // For any other exception just notify the user 528 // 529 String msg = e.getMessage().substring(0,Math.min(80, e.getMessage().length())); 530 if (msg.length() < e.getMessage().length()) { 531 msg += " ..."; 532 } 533 e.printStackTrace(); 276 534 JOptionPane.showMessageDialog( 277 535 null, 278 e.getMessage(),536 msg, 279 537 tr("Upload to OSM API failed"), 280 538 JOptionPane.ERROR_MESSAGE 281 539 ); 282 } 283 284 protected void handleOsmApiInitializationException(Exception e) { 540 541 } 542 543 /** 544 * handles an exception caught during OSM API initialization 545 * 546 * @param e the exception 547 */ 548 protected void handleOsmApiInitializationException(OsmApiInitializationException e) { 285 549 JOptionPane.showMessageDialog( 286 null,550 Main.parent, 287 551 tr( "Failed to initialize communication with the OSM server {0}.\n" 288 552 + "Check the server URL in your preferences and your internet connection.", -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
r1647 r1670 22 22 import org.openstreetmap.josm.io.OsmServerLocationReader; 23 23 import org.openstreetmap.josm.io.OsmServerReader; 24 import org.openstreetmap.josm.io.OsmTransferException; 24 25 import org.xml.sax.SAXException; 25 26 … … 32 33 private static Bounds currentBounds; 33 34 private Future<Task> task = null; 35 private DataSet downloadedData; 34 36 35 private staticclass Task extends PleaseWaitRunnable {37 private class Task extends PleaseWaitRunnable { 36 38 private OsmServerReader reader; 37 39 private DataSet dataSet; … … 49 51 } 50 52 51 @Override public void realRun() throws IOException, SAXException {53 @Override public void realRun() throws IOException, SAXException, OsmTransferException { 52 54 Main.pleaseWaitDlg.setCustomText(msg); 53 55 dataSet = reader.parseOsm(); … … 59 61 if (dataSet.allPrimitives().isEmpty()) { 60 62 // If silent is set to true, we don't want to see information messages 61 if(!silent) 63 if(!silent) { 62 64 errorMessage = tr("No data imported."); 65 } 63 66 // need to synthesize a download bounds lest the visual indication of downloaded 64 67 // area doesn't work 65 68 dataSet.dataSources.add(new DataSource(currentBounds, "OpenStreetMap server")); 66 69 } 67 70 rememberDownloadedData(dataSet); 68 71 OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer {0}", num), null); 69 if (newLayer) 72 if (newLayer) { 70 73 Main.main.addLayer(layer); 71 else74 } else { 72 75 Main.main.editLayer().mergeFrom(layer); 76 } 73 77 74 78 Main.pleaseWaitDlg.setCustomText(""); … … 76 80 77 81 @Override protected void cancel() { 78 if (reader != null) 82 if (reader != null) { 79 83 reader.cancel(); 84 } 80 85 Main.pleaseWaitDlg.cancel.setEnabled(false); 81 86 } 82 87 } 83 88 private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true); 89 90 private void rememberDownloadedData(DataSet ds) { 91 this.downloadedData = ds; 92 } 93 94 public DataSet getDownloadedData() { 95 return downloadedData; 96 } 84 97 85 98 public void download(DownloadAction action, double minlat, double minlon, … … 96 109 97 110 boolean newLayer = action != null 98 111 && (action.dialog == null || action.dialog.newLayer.isSelected()); 99 112 100 113 Task t = new Task(newLayer, … … 124 137 false, 125 138 getDataLayersCount(), 126 139 ""); 127 140 task = Main.worker.submit(t, t); 128 141 } … … 145 158 int num = 0; 146 159 for(Layer l : Main.map.mapView.getAllLayers()) 147 if(l instanceof OsmDataLayer) 160 if(l instanceof OsmDataLayer) { 148 161 num++; 162 } 149 163 return num; 150 164 } 151 165 152 /*153 * (non-Javadoc)154 * @see org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask#getErrorMessage()155 */166 /* 167 * (non-Javadoc) 168 * @see org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask#getErrorMessage() 169 */ 156 170 public String getErrorMessage() { 157 171 if(task == null) … … 161 175 Task t = task.get(); 162 176 return t.errorMessage == null 163 164 : t.errorMessage;177 ? "" 178 : t.errorMessage; 165 179 } catch (Exception e) { 166 180 return ""; -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskList.java
r1465 r1670 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.EventQueue; 7 import java.awt.event.ActionEvent; 6 8 import java.awt.geom.Area; 7 9 import java.awt.geom.Rectangle2D; 10 import java.util.ArrayList; 8 11 import java.util.Collection; 12 import java.util.HashSet; 9 13 import java.util.LinkedList; 10 14 import java.util.List; 15 import java.util.Set; 11 16 12 17 import javax.swing.JOptionPane; 13 18 14 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.actions.UpdateSelectionAction; 15 21 import org.openstreetmap.josm.data.osm.DataSet; 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 16 24 import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask; 17 25 import org.openstreetmap.josm.gui.layer.Layer; … … 62 70 public void download(boolean newLayer, Collection<Area> areas) { 63 71 List<Rectangle2D> rects = new LinkedList<Rectangle2D>(); 64 for(Area a : areas) 72 for(Area a : areas) { 65 73 rects.add(a.getBounds2D()); 74 } 66 75 67 76 download(newLayer, rects); … … 76 85 for(DownloadTask dt : osmTasks) { 77 86 String err = dt.getErrorMessage(); 78 if(err.equals("")) 87 if(err.equals("")) { 79 88 continue; 89 } 80 90 errors += "* " + err + "\r\n"; 81 91 } 82 92 83 osmTasks.clear(); 84 if(errors.equals("")) 93 if(! errors.equals("")) { 94 JOptionPane.showMessageDialog(Main.parent, 95 tr("The following errors occured during mass download:") + "\r\n" + errors, 96 tr("Errors during Download"), 97 JOptionPane.ERROR_MESSAGE); 85 98 return; 99 } 86 100 87 JOptionPane.showMessageDialog(Main.parent, 88 tr("The following errors occured during mass download:") + "\r\n" + errors, 89 tr("Errors during Download"), 90 JOptionPane.ERROR_MESSAGE); 101 Set<Long> myPrimitiveIds = Main.main.editLayer().data.getPrimitiveIds(); 102 Set<Long> downloadedIds = getDownloadedIds(); 103 myPrimitiveIds.removeAll(downloadedIds); 104 if (! myPrimitiveIds.isEmpty()) { 105 handlePotentiallyDeletedPrimitives(myPrimitiveIds); 106 } 107 } 108 109 protected void checkPotentiallyDeletedPrimitives(Set<Long> potentiallyDeleted) { 110 DataSet ds = Main.main.editLayer().data; 111 ArrayList<OsmPrimitive> toSelect = new ArrayList<OsmPrimitive>(); 112 for (Long id : potentiallyDeleted) { 113 OsmPrimitive primitive = ds.getPrimitiveById(id); 114 if (primitive != null) { 115 toSelect.add(primitive); 116 } 117 } 118 ds.setSelected(toSelect); 119 EventQueue.invokeLater( 120 new Runnable() { 121 public void run() { 122 new UpdateSelectionAction().actionPerformed(new ActionEvent(this, 0, "")); 123 } 124 } 125 ); 126 } 127 128 protected void handlePotentiallyDeletedPrimitives(Set<Long> potentiallyDeleted) { 129 String [] options = { 130 "Check individually", 131 "Ignore" 132 }; 133 134 String message = tr("<html>" 135 + "There are {0} primitives in your local dataset which<br>" 136 + "might be deleted on the server. If you later try to delete or<br>" 137 + "update them on the server the server is likely to report a<br>" 138 + "conflict.<br>" 139 + "<br>" 140 + "Click <strong>{1}</strong> to check these primitives individually.<br>" 141 + "Click <strong>{2}</strong> to ignore.<br>" 142 + "</html>", 143 potentiallyDeleted.size(), options[0], options[1] 144 ); 145 146 int ret = JOptionPane.showOptionDialog( 147 Main.parent, 148 message, 149 tr("Deleted or moved primitives"), 150 JOptionPane.YES_NO_OPTION, 151 JOptionPane.WARNING_MESSAGE, 152 null, 153 options, 154 options[0] 155 ); 156 switch(ret) { 157 case JOptionPane.CLOSED_OPTION: return; 158 case JOptionPane.NO_OPTION: return; 159 case JOptionPane.YES_OPTION: checkPotentiallyDeletedPrimitives(potentiallyDeleted); break; 160 } 161 } 162 163 protected boolean wasDownloaded(long id, DataSet ds) { 164 OsmPrimitive primitive = ds.getPrimitiveById(id); 165 return primitive != null; 166 } 167 168 public boolean wasDownloaded(long id) { 169 for (DownloadTask task : osmTasks) { 170 if(task instanceof DownloadOsmTask) { 171 DataSet ds = ((DownloadOsmTask)task).getDownloadedData(); 172 if(wasDownloaded(id,ds)) return true; 173 } 174 } 175 return false; 176 } 177 178 179 public Set<Long> getDownloadedIds() { 180 HashSet<Long> ret = new HashSet<Long>(); 181 for (DownloadTask task : osmTasks) { 182 if(task instanceof DownloadOsmTask) { 183 DataSet ds = ((DownloadOsmTask)task).getDownloadedData(); 184 ret.addAll(ds.getPrimitiveIds()); 185 } 186 } 187 return ret; 91 188 } 92 189 } -
trunk/src/org/openstreetmap/josm/actions/search/SelectionWebsiteLoader.java
r1195 r1670 22 22 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 23 23 import org.openstreetmap.josm.io.OsmIdReader; 24 import org.openstreetmap.josm.io.OsmTransferException; 24 25 import org.openstreetmap.josm.io.ProgressInputStream; 25 26 import org.xml.sax.SAXException; … … 47 48 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) { 48 49 if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) { 49 if (mode == SearchAction.SearchMode.remove) 50 if (mode == SearchAction.SearchMode.remove) { 50 51 sel.remove(osm); 51 else52 } else { 52 53 sel.add(osm); 54 } 53 55 } 54 56 } … … 59 61 e.printStackTrace(); 60 62 JOptionPane.showMessageDialog(Main.parent,tr("Parsing error in URL: \"{0}\"",url)); 63 } catch(OsmTransferException e) { 64 e.printStackTrace(); 65 if (e.getCause() != null) { 66 if (e.getCause() instanceof IOException ) { 67 JOptionPane.showMessageDialog(Main.parent, tr("Could not read from URL: \"{0}\"",url), 68 tr("Error"), JOptionPane.ERROR_MESSAGE); 69 } else if (e.getCause() instanceof SAXException) { 70 JOptionPane.showMessageDialog(Main.parent,tr("Parsing error in URL: \"{0}\"",url), 71 tr("Error"), JOptionPane.ERROR_MESSAGE); 72 } 73 } else { 74 JOptionPane.showMessageDialog(Main.parent,tr("Error while communicating with server.",url), 75 tr("Error"), JOptionPane.ERROR_MESSAGE); 76 } 77 61 78 } 62 79 } … … 66 83 } 67 84 @Override protected void finish() { 68 if (sel != null) 85 if (sel != null) { 69 86 Main.ds.setSelected(sel); 87 } 70 88 } 71 89 } -
trunk/src/org/openstreetmap/josm/command/CoordinateConflictResolveCommand.java
r1654 r1670 33 33 /** the merge decision */ 34 34 private final MergeDecisionType decision; 35 36 37 /**38 * replies a (localized) display name for the type of an OSM primitive39 *40 * @param primitive the primitive41 * @return a localized display name42 */43 protected String getPrimitiveTypeAsString(OsmPrimitive primitive) {44 if (primitive instanceof Node) return tr("node");45 if (primitive instanceof Way) return tr("way");46 if (primitive instanceof Relation) return tr("relation");47 return "";48 }49 35 50 36 /** -
trunk/src/org/openstreetmap/josm/command/TagConflictResolveCommand.java
r1654 r1670 14 14 import org.openstreetmap.josm.data.osm.Node; 15 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 16 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 16 17 import org.openstreetmap.josm.data.osm.Relation; 17 18 import org.openstreetmap.josm.data.osm.Way; … … 52 53 53 54 /** 54 * replies a (localized) display name for the type of an OSM primitive55 *56 * @param primitive the primitive57 * @return a localized display name58 */59 protected String getPrimitiveTypeAsString(OsmPrimitive primitive) {60 if (primitive instanceof Node) return tr("node");61 if (primitive instanceof Way) return tr("way");62 if (primitive instanceof Relation) return tr("relation");63 return "";64 }65 66 /**67 55 * constructor 68 56 * … … 82 70 return new DefaultMutableTreeNode( 83 71 new JLabel( 84 tr("Resolve {0} tag conflicts in {1} {2}",getNumDecidedConflicts(), getPrimitiveTypeAsString(my), my.id),72 tr("Resolve {0} tag conflicts in {1} {2}",getNumDecidedConflicts(), OsmPrimitiveType.from(my).getLocalizedDisplayNameSingular(), my.id), 85 73 ImageProvider.get("data", "object"), 86 74 JLabel.HORIZONTAL -
trunk/src/org/openstreetmap/josm/command/VersionConflictResolveCommand.java
r1654 r1670 13 13 import org.openstreetmap.josm.data.osm.Node; 14 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 15 16 import org.openstreetmap.josm.data.osm.Relation; 16 17 import org.openstreetmap.josm.data.osm.Way; … … 37 38 } 38 39 39 //FIXME copied from TagConflictResolveCommand -> refactor40 /**41 * replies a (localized) display name for the type of an OSM primitive42 *43 * @param primitive the primitive44 * @return a localized display name45 */46 protected String getPrimitiveTypeAsString(OsmPrimitive primitive) {47 if (primitive instanceof Node) return tr("node");48 if (primitive instanceof Way) return tr("way");49 if (primitive instanceof Relation) return tr("relation");50 return "";51 }52 53 40 @Override 54 41 public MutableTreeNode description() { 55 42 return new DefaultMutableTreeNode( 56 43 new JLabel( 57 tr("Resolve version conflicts for {0} {1}", getPrimitiveTypeAsString(my), my.id),44 tr("Resolve version conflicts for {0} {1}",OsmPrimitiveType.from(my).getLocalizedDisplayNameSingular(), my.id), 58 45 ImageProvider.get("data", "object"), 59 46 JLabel.HORIZONTAL -
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r1647 r1670 10 10 import java.util.LinkedList; 11 11 import java.util.List; 12 import java.util.Set; 12 13 13 14 import org.openstreetmap.josm.data.SelectionChangedListener; 15 import static org.openstreetmap.josm.tools.I18n.tr; 14 16 15 17 /** … … 78 80 Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>(); 79 81 for (OsmPrimitive osm : allPrimitives()) 80 if (!osm.deleted) 82 if (!osm.deleted) { 81 83 o.add(osm); 84 } 82 85 return o; 83 86 } … … 86 89 Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>(); 87 90 for (OsmPrimitive osm : allPrimitives()) 88 if (!osm.deleted && !osm.incomplete) 91 if (!osm.deleted && !osm.incomplete) { 89 92 o.add(osm); 93 } 90 94 return o; 91 95 } … … 94 98 Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>(); 95 99 for (OsmPrimitive osm : allPrimitives()) 96 if (!osm.deleted && !osm.incomplete && !(osm instanceof Relation)) 100 if (!osm.deleted && !osm.incomplete && !(osm instanceof Relation)) { 97 101 o.add(osm); 102 } 98 103 return o; 99 104 } … … 150 155 clearSelection(ways); 151 156 clearSelection(relations); 152 for (OsmPrimitive osm : selection) 157 for (OsmPrimitive osm : selection) { 153 158 osm.selected = true; 159 } 154 160 fireSelectionChanged(selection); 155 161 } … … 164 170 clearSelection(relations); 165 171 for (OsmPrimitive o : osm) 166 if (o != null) 172 if (o != null) { 167 173 o.selected = true; 174 } 168 175 fireSelectionChanged(Arrays.asList(osm)); 169 176 } … … 176 183 if (list == null) 177 184 return; 178 for (OsmPrimitive osm : list) 185 for (OsmPrimitive osm : list) { 179 186 osm.selected = false; 187 } 180 188 } 181 189 … … 189 197 return sel; 190 198 for (OsmPrimitive osm : list) 191 if (osm.selected && !osm.deleted) 199 if (osm.selected && !osm.deleted) { 192 200 sel.add(osm); 201 } 193 202 return sel; 194 203 } … … 200 209 */ 201 210 public static void fireSelectionChanged(Collection<? extends OsmPrimitive> sel) { 202 for (SelectionChangedListener l : selListeners) 211 for (SelectionChangedListener l : selListeners) { 203 212 l.selectionChanged(sel); 213 } 204 214 } 205 215 206 216 @Override public DataSet clone() { 207 217 DataSet ds = new DataSet(); 208 for (Node n : nodes) 218 for (Node n : nodes) { 209 219 ds.nodes.add(new Node(n)); 210 for (Way w : ways) 220 } 221 for (Way w : ways) { 211 222 ds.ways.add(new Way(w)); 212 for (Relation e : relations) 223 } 224 for (Relation e : relations) { 213 225 ds.relations.add(new Relation(e)); 214 for (DataSource source : dataSources) 226 } 227 for (DataSource source : dataSources) { 215 228 ds.dataSources.add(new DataSource(source.bounds, source.origin)); 229 } 216 230 ds.version = version; 217 231 return ds; … … 260 274 return selArr; 261 275 } 276 277 /** 278 * returns a primitive with a given id from the data set. null, if no such primitive 279 * exists 280 * 281 * @param id the id, > 0 required 282 * @return the primitive 283 * @exception IllegalArgumentException thrown, if id <= 0 284 */ 285 public OsmPrimitive getPrimitiveById(long id) { 286 if (id <= 0) 287 throw new IllegalArgumentException(tr("parameter {0} > 0 required. Got {1}.", "id", id)); 288 for (OsmPrimitive primitive : nodes) { 289 if (primitive.id == id) return primitive; 290 } 291 for (OsmPrimitive primitive : ways) { 292 if (primitive.id == id) return primitive; 293 } 294 for (OsmPrimitive primitive : relations) { 295 if (primitive.id == id) return primitive; 296 } 297 return null; 298 } 299 300 public Set<Long> getPrimitiveIds() { 301 HashSet<Long> ret = new HashSet<Long>(); 302 for (OsmPrimitive primitive : nodes) { 303 ret.add(primitive.id); 304 } 305 for (OsmPrimitive primitive : ways) { 306 ret.add(primitive.id); 307 } 308 for (OsmPrimitive primitive : relations) { 309 ret.add(primitive.id); 310 } 311 return ret; 312 } 262 313 } -
trunk/src/org/openstreetmap/josm/data/osm/Relation.java
r1598 r1670 78 78 } 79 79 80 @Override 80 81 public String getName() { 81 82 String name; … … 84 85 } else { 85 86 name = get("type"); 86 if (name == null) 87 if (name == null) { 87 88 name = tr("relation"); 89 } 88 90 89 91 name += " ("; 90 if(names == null) 91 names = Main.pref.getCollection("relation.nameOrder", Arrays.asList(defnames)); 92 if(names == null) { 93 names = Main.pref.getCollection("relation.nameOrder", Arrays.asList(defnames)); 94 } 92 95 String nameTag = null; 93 96 for (String n : names) { 94 97 nameTag = get(n); 95 if (nameTag != null) break; 98 if (nameTag != null) { 99 break; 100 } 96 101 } 97 if (nameTag != null) 102 if (nameTag != null) { 98 103 name += "\"" + nameTag + "\", "; 104 } 99 105 100 106 int mbno = members.size(); 101 107 name += trn("{0} member", "{0} members", mbno, mbno) + ")"; 102 if(errors != null) 108 if(errors != null) { 103 109 name = "*"+name; 110 } 104 111 } 105 112 return name; … … 113 120 return false; 114 121 } 115 122 116 123 public RelationMember firstMember() { 117 124 if (incomplete) return null; … … 122 129 return (members.size() == 0) ? null : members.get(members.size() -1); 123 130 } 131 132 /** 133 * removes all members with member.member == primitive 134 * 135 * @param primitive the primitive to check for 136 */ 137 public void removeMembersFor(OsmPrimitive primitive) { 138 if (primitive == null) 139 return; 140 141 ArrayList<RelationMember> todelete = new ArrayList<RelationMember>(); 142 for (RelationMember member: members) { 143 if (member.member == primitive) { 144 todelete.add(member); 145 } 146 } 147 members.removeAll(todelete); 148 } 124 149 } -
trunk/src/org/openstreetmap/josm/gui/MainMenu.java
r1639 r1670 62 62 import org.openstreetmap.josm.actions.UnselectAllAction; 63 63 import org.openstreetmap.josm.actions.UpdateDataAction; 64 import org.openstreetmap.josm.actions.UpdateSelectionAction; 64 65 import org.openstreetmap.josm.actions.UploadAction; 65 66 import org.openstreetmap.josm.actions.ZoomInAction; … … 94 95 public final DownloadAction download = new DownloadAction(); 95 96 public final JosmAction update = new UpdateDataAction(); 97 public final JosmAction updateSelection = new UpdateSelectionAction(); 96 98 public final JosmAction upload = new UploadAction(); 97 99 public final JosmAction exit = new ExitAction(); … … 194 196 add(fileMenu, upload); 195 197 add(fileMenu, update); 198 add(fileMenu, updateSelection); 196 199 fileMenu.addSeparator(); 197 200 add(fileMenu, exit); -
trunk/src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java
r1647 r1670 17 17 18 18 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.io.OsmTransferException; 19 20 import org.xml.sax.SAXException; 20 21 … … 109 110 x.printStackTrace(); 110 111 errorMessage = x.getMessage(); 112 } catch(OsmTransferException x) { 113 x.printStackTrace(); 114 if (x.getCause() != null) { 115 errorMessage = x.getCause().getMessage(); 116 } else { 117 errorMessage = x.getMessage(); 118 } 111 119 } finally { 112 120 closeDialog(); … … 134 142 * is called. finish() is called in any case. 135 143 */ 136 protected abstract void realRun() throws SAXException, IOException ;144 protected abstract void realRun() throws SAXException, IOException, OsmTransferException; 137 145 138 146 /** … … 158 166 Main.pleaseWaitDlg.dispose(); 159 167 } 160 if (errorMessage != null && !silent) 168 if (errorMessage != null && !silent) { 161 169 JOptionPane.showMessageDialog(Main.parent, errorMessage); 170 } 162 171 } 163 172 }; 164 173 165 174 // make sure, this is called in the dispatcher thread ASAP 166 if (EventQueue.isDispatchThread()) 175 if (EventQueue.isDispatchThread()) { 167 176 runnable.run(); 168 else177 } else { 169 178 EventQueue.invokeAndWait(runnable); 179 } 170 180 171 181 } catch (InterruptedException e) { -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
r1624 r1670 36 36 import org.openstreetmap.josm.data.osm.Node; 37 37 import org.openstreetmap.josm.data.osm.OsmPrimitive; 38 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 38 39 import org.openstreetmap.josm.data.osm.Relation; 39 40 import org.openstreetmap.josm.data.osm.RelationMember; … … 44 45 import org.openstreetmap.josm.gui.dialogs.ConflictDialog; 45 46 import org.openstreetmap.josm.io.OsmServerObjectReader; 47 import org.openstreetmap.josm.io.OsmTransferException; 46 48 import org.openstreetmap.josm.tools.GBC; 47 49 import org.openstreetmap.josm.tools.Shortcut; … … 145 147 String key = propertyData.getValueAt(i, 0).toString(); 146 148 String value = propertyData.getValueAt(i, 1).toString(); 147 if (key.length() > 0 && value.length() > 0) clone.put(key, value); 149 if (key.length() > 0 && value.length() > 0) { 150 clone.put(key, value); 151 } 148 152 } 149 153 refreshTables(); … … 175 179 { 176 180 sel = new ArrayList<OsmPrimitive>(cnt); 177 for (int i : memberTable.getSelectedRows()) 181 for (int i : memberTable.getSelectedRows()) { 178 182 sel.add((OsmPrimitive)memberTable.getValueAt(i, 1)); 183 } 179 184 } 180 185 else … … 182 187 cnt = memberTable.getRowCount(); 183 188 sel = new ArrayList<OsmPrimitive>(cnt); 184 for (int i = 0; i < cnt; ++i) 189 for (int i = 0; i < cnt; ++i) { 185 190 sel.add((OsmPrimitive)memberTable.getValueAt(i, 1)); 191 } 186 192 } 187 193 Main.ds.setSelected(sel); … … 239 245 240 246 buttonPanel.add(createButton(marktr("Add Selected"),"addselected", 241 tr("Add all currently selected objects as members"), KeyEvent.VK_D, new ActionListener() {247 tr("Add all currently selected objects as members"), KeyEvent.VK_D, new ActionListener() { 242 248 public void actionPerformed(ActionEvent e) { 243 249 addSelected(); … … 246 252 247 253 buttonPanel.add(createButton(marktr("Remove Selected"),"removeselected", 248 tr("Remove all currently selected objects from relation"), KeyEvent.VK_S, new ActionListener() {254 tr("Remove all currently selected objects from relation"), KeyEvent.VK_S, new ActionListener() { 249 255 public void actionPerformed(ActionEvent e) { 250 256 deleteSelected(); … … 259 265 260 266 buttonPanel.add(createButton(marktr("Remove"),"remove", 261 tr("Remove the member in the current table row from this relation"), KeyEvent.VK_M, new ActionListener() {267 tr("Remove the member in the current table row from this relation"), KeyEvent.VK_M, new ActionListener() { 262 268 public void actionPerformed(ActionEvent e) { 263 269 int[] rows = memberTable.getSelectedRows(); … … 273 279 274 280 buttonPanel.add(createButton(marktr("Download Members"),"downloadincomplete", 275 tr("Download all incomplete ways and nodes in relation"), KeyEvent.VK_K, new ActionListener() {281 tr("Download all incomplete ways and nodes in relation"), KeyEvent.VK_K, new ActionListener() { 276 282 public void actionPerformed(ActionEvent e) { 277 283 downloadRelationMembers(); … … 303 309 protected void buttonAction(ActionEvent evt) { 304 310 String a = evt.getActionCommand(); 305 if(applyChangesText.equals(a)) 311 if(applyChangesText.equals(a)) { 306 312 applyChanges(); 313 } 307 314 308 315 setVisible(false); … … 348 355 break; 349 356 } else if (m.member instanceof Relation) { 350 if (m.member == this.relation) 357 if (m.member == this.relation) { 351 358 break; 359 } 352 360 m = ((Relation)m.member).lastMember(); 353 361 depth++; … … 366 374 break; 367 375 } else if (m.member instanceof Relation) { 368 if (m.member == this.relation) 376 if (m.member == this.relation) { 369 377 break; 378 } 370 379 m = ((Relation)(m.member)).firstMember(); 371 380 depth++; … … 374 383 } 375 384 } 376 if (way2 != null) 385 if (way2 != null) { 377 386 break; 387 } 378 388 } 379 389 } … … 404 414 } 405 415 406 // end of section to determine linkedness. 416 // end of section to determine linkedness. 407 417 408 418 memberData.addRow(new Object[]{em.role, em.member, linked ? tr("yes") : tr("no")}); … … 416 426 private SideButton createButton(String name, String iconName, String tooltip, int mnemonic, ActionListener actionListener) { 417 427 return 418 428 new SideButton(name, iconName, "relationEditor", 419 429 tooltip, 420 430 Shortcut.registerShortcut("relationeditor:"+iconName, … … 422 432 mnemonic, 423 433 Shortcut.GROUP_MNEMONIC), 424 actionListener425 434 actionListener 435 ); 426 436 } 427 437 … … 483 493 for (RelationMember rm : clone.members) { 484 494 if (rm != null) { 485 while (m[i] != null) i++; 495 while (m[i] != null) { 496 i++; 497 } 486 498 m[i++] = rm; 487 499 } … … 509 521 } 510 522 if (download) { 511 OsmServerObjectReader reader = new OsmServerObjectReader(clone.id, Osm ServerObjectReader.TYPE_REL, true);523 OsmServerObjectReader reader = new OsmServerObjectReader(clone.id, OsmPrimitiveType.RELATION, true); 512 524 try { 513 525 DataSet dataSet = reader.parseOsm(); … … 515 527 final MergeVisitor visitor = new MergeVisitor(Main.main 516 528 .editLayer().data, dataSet); 517 for (final OsmPrimitive osm : dataSet.allPrimitives()) 529 for (final OsmPrimitive osm : dataSet.allPrimitives()) { 518 530 osm.visit(visitor); 531 } 519 532 visitor.fixReferences(); 520 533 521 534 // copy the merged layer's data source info 522 for (DataSource src : dataSet.dataSources) 535 for (DataSource src : dataSet.dataSources) { 523 536 Main.main.editLayer().data.dataSources.add(src); 537 } 524 538 Main.main.editLayer().fireDataChange(); 525 539 … … 530 544 JOptionPane.showMessageDialog(Main.parent, 531 545 tr("There were conflicts during import.")); 532 if (!dlg.isVisible()) 546 if (!dlg.isVisible()) { 533 547 dlg.action 534 535 }536 537 } catch (SAXException e) {548 .actionPerformed(new ActionEvent(this, 0, "")); 549 } 550 } 551 } catch(OsmTransferException e) { 538 552 e.printStackTrace(); 539 JOptionPane.showMessageDialog(this,tr("Error parsing server response.")+": "+e.getMessage(), 540 tr("Error"), JOptionPane.ERROR_MESSAGE); 541 } catch (IOException e) { 542 e.printStackTrace(); 543 JOptionPane.showMessageDialog(this,tr("Cannot connect to server.")+": "+e.getMessage(), 544 tr("Error"), JOptionPane.ERROR_MESSAGE); 553 if (e.getCause() != null) { 554 if (e.getCause() instanceof SAXException) { 555 JOptionPane.showMessageDialog(this,tr("Error parsing server response.")+": "+e.getCause().getMessage(), 556 tr("Error"), JOptionPane.ERROR_MESSAGE); 557 } else if(e.getCause() instanceof IOException) { 558 JOptionPane.showMessageDialog(this,tr("Cannot connect to server.")+": "+e.getCause().getMessage(), 559 tr("Error"), JOptionPane.ERROR_MESSAGE); 560 } 561 } else { 562 JOptionPane.showMessageDialog(this,tr("Error when communicating with server.")+": "+e.getMessage(), 563 tr("Error"), JOptionPane.ERROR_MESSAGE); 564 } 545 565 } 546 566 } -
trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
r1646 r1670 51 51 import org.openstreetmap.josm.data.osm.Node; 52 52 import org.openstreetmap.josm.data.osm.OsmPrimitive; 53 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 53 54 import org.openstreetmap.josm.data.osm.Relation; 54 55 import org.openstreetmap.josm.data.osm.Way; … … 77 78 public final int[] normal = new int[3]; 78 79 public final int[] deleted = new int[3]; 79 public final String[] names = {"node", "way", "relation"}; 80 public final String[] names = { 81 OsmPrimitiveType.NODE.getAPIName(), 82 OsmPrimitiveType.WAY.getAPIName(), 83 OsmPrimitiveType.RELATION.getAPIName() 84 }; 80 85 81 86 private void inc(final OsmPrimitive osm, final int i) { 82 87 normal[i]++; 83 if (osm.deleted) 88 if (osm.deleted) { 84 89 deleted[i]++; 90 } 85 91 } 86 92 … … 201 207 202 208 SimplePaintVisitor painter; 203 if (Main.pref.getBoolean("draw.wireframe")) 209 if (Main.pref.getBoolean("draw.wireframe")) { 204 210 painter = new SimplePaintVisitor(); 205 else211 } else { 206 212 painter = new MapPaintVisitor(); 213 } 207 214 painter.setGraphics(g); 208 215 painter.setNavigatableComponent(mv); … … 216 223 tool += undeletedSize(data.nodes)+" "+trn("node", "nodes", undeletedSize(data.nodes))+", "; 217 224 tool += undeletedSize(data.ways)+" "+trn("way", "ways", undeletedSize(data.ways)); 218 if (data.version != null) tool += ", " + tr("version {0}", data.version); 225 if (data.version != null) { 226 tool += ", " + tr("version {0}", data.version); 227 } 219 228 File f = getAssociatedFile(); 220 if (f != null) 229 if (f != null) { 221 230 tool = "<html>"+tool+"<br>"+f.getPath()+"</html>"; 231 } 222 232 return tool; 223 233 } 224 234 225 235 @Override public void mergeFrom(final Layer from) { 226 final MergeVisitor visitor = new MergeVisitor(data,((OsmDataLayer)from).data); 227 for (final OsmPrimitive osm : ((OsmDataLayer)from).data.allPrimitives()) { 228 // i++; 229 // if(i%100 == 0) { 230 // double perc = (((double)i) / ((double)max) * 100.0); 231 // System.out.format(" " + (int)perc + "%%"); 232 // } 236 mergeFrom(((OsmDataLayer)from).data); 237 } 238 239 /** 240 * merges the primitives in dataset <code>from</code> into the dataset of 241 * this layer 242 * 243 * @param from the source data set 244 */ 245 public void mergeFrom(final DataSet from) { 246 final MergeVisitor visitor = new MergeVisitor(data,from); 247 for (final OsmPrimitive osm : from.allPrimitives()) { 233 248 osm.visit(visitor); 234 249 } 235 250 visitor.fixReferences(); 236 // System.out.println("");237 251 238 252 Area a = data.getDataSourceArea(); 239 240 // copy the merged layer's data source info; 253 254 // copy the merged layer's data source info; 241 255 // only add source rectangles if they are not contained in the 242 256 // layer already. 243 for (DataSource src : ((OsmDataLayer)from).data.dataSources) {244 if (a == null || !a.contains(src.bounds.asRect())) 257 for (DataSource src : from.dataSources) { 258 if (a == null || !a.contains(src.bounds.asRect())) { 245 259 data.dataSources.add(src); 246 } 247 260 } 261 } 262 248 263 // copy the merged layer's API version, downgrade if required 249 264 if (data.version == null) { 250 data.version = ((OsmDataLayer)from).data.version;265 data.version = from.version; 251 266 } else { 252 if ("0.5".equals(data.version) ^ "0.5".equals( ((OsmDataLayer)from).data.version)) {253 System.err.println( "Warning: mixing 0.6 and 0.5 data results in version 0.5");267 if ("0.5".equals(data.version) ^ "0.5".equals(from.version)) { 268 System.err.println(tr("Warning: mixing 0.6 and 0.5 data results in version 0.5")); 254 269 data.version = "0.5"; 255 270 } … … 263 278 final ConflictDialog dlg = Main.map.conflictDialog; 264 279 dlg.add(visitor.conflicts); 265 JOptionPane.showMessageDialog(Main.parent,tr("There were conflicts during import."));266 if (!dlg.isVisible()) 280 JOptionPane.showMessageDialog(Main.parent,tr("There were {0} conflicts during import.", visitor.conflicts.size())); 281 if (!dlg.isVisible()) { 267 282 dlg.action.actionPerformed(new ActionEvent(this, 0, "")); 283 } 268 284 } 269 285 … … 274 290 @Override public void visitBoundingBox(final BoundingXYVisitor v) { 275 291 for (final Node n : data.nodes) 276 if (!n.deleted && !n.incomplete) 292 if (!n.deleted && !n.incomplete) { 277 293 v.visit(n); 294 } 278 295 } 279 296 … … 299 316 if (processed != null) { 300 317 final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed); 301 for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();) 318 for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();) { 302 319 cleanIterator(it, processedSet); 303 for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();) 320 } 321 for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();) { 304 322 cleanIterator(it, processedSet); 305 for (final Iterator<Relation> it = data.relations.iterator(); it.hasNext();) 323 } 324 for (final Iterator<Relation> it = data.relations.iterator(); it.hasNext();) { 306 325 cleanIterator(it, processedSet); 326 } 307 327 } 308 328 … … 330 350 return; 331 351 osm.modified = false; 332 if (osm.deleted) 352 if (osm.deleted) { 333 353 it.remove(); 354 } 334 355 } 335 356 … … 342 363 return; 343 364 this.modified = modified; 344 for (final ModifiedChangedListener l : listenerModified) 365 for (final ModifiedChangedListener l : listenerModified) { 345 366 l.modifiedChanged(modified, this); 367 } 346 368 } 347 369 … … 352 374 int size = 0; 353 375 for (final OsmPrimitive osm : list) 354 if (!osm.deleted) 376 if (!osm.deleted) { 355 377 size++; 378 } 356 379 return size; 357 380 } … … 359 382 @Override public Object getInfoComponent() { 360 383 final DataCountVisitor counter = new DataCountVisitor(); 361 for (final OsmPrimitive osm : data.allPrimitives()) 384 for (final OsmPrimitive osm : data.allPrimitives()) { 362 385 osm.visit(counter); 386 } 363 387 final JPanel p = new JPanel(new GridBagLayout()); 364 388 p.add(new JLabel(tr("{0} consists of:", name)), GBC.eol()); 365 389 for (int i = 0; i < counter.normal.length; ++i) { 366 390 String s = counter.normal[i]+" "+trn(counter.names[i],counter.names[i]+"s",counter.normal[i]); 367 if (counter.deleted[i] > 0) 391 if (counter.deleted[i] > 0) { 368 392 s += tr(" ({0} deleted.)",counter.deleted[i]); 393 } 369 394 p.add(new JLabel(s, ImageProvider.get("data", counter.names[i]), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0)); 370 395 } … … 375 400 376 401 @Override public Component[] getMenuEntries() { 377 if (Main.applet) {402 if (Main.applet) 378 403 return new Component[]{ 379 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 380 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 381 new JSeparator(), 382 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 383 new JSeparator(), 384 new JMenuItem(new LayerListPopup.InfoAction(this))}; 385 } 404 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 405 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 406 new JSeparator(), 407 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), 408 new JSeparator(), 409 new JMenuItem(new LayerListPopup.InfoAction(this))}; 386 410 return new Component[]{ 387 411 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), … … 409 433 HashSet<Node> doneNodes = new HashSet<Node>(); 410 434 for (Way w : data.ways) { 411 if (w.incomplete || w.deleted) continue; 435 if (w.incomplete || w.deleted) { 436 continue; 437 } 412 438 GpxTrack trk = new GpxTrack(); 413 439 gpxData.tracks.add(trk); 414 440 415 if (w.get("name") != null) 441 if (w.get("name") != null) { 416 442 trk.attr.put("name", w.get("name")); 443 } 417 444 418 445 ArrayList<WayPoint> trkseg = null; … … 429 456 doneNodes.add(n); 430 457 } 431 WayPoint wpt = new WayPoint(n.getCoor()); 458 WayPoint wpt = new WayPoint(n.getCoor()); 432 459 if (!n.isTimestampEmpty()) 433 460 { … … 442 469 // records them? 443 470 for (Node n : data.nodes) { 444 if (n.incomplete || n.deleted || doneNodes.contains(n)) continue; 471 if (n.incomplete || n.deleted || doneNodes.contains(n)) { 472 continue; 473 } 445 474 WayPoint wpt = new WayPoint(n.getCoor()); 446 475 if (!n.isTimestampEmpty()) { -
trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java
r1465 r1670 50 50 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000))); 51 51 InputStream in = getInputStream(url+i, Main.pleaseWaitDlg); 52 if (in == null) 52 if (in == null) { 53 53 break; 54 } 54 55 GpxData currentGpx = new GpxReader(in, null).data; 55 56 if (result == null) { … … 89 90 * @return A data set containing all data retrieved from that url 90 91 */ 91 public DataSet parseOsm() throws SAXException, IOException { 92 @Override 93 public DataSet parseOsm() throws OsmTransferException { 92 94 try { 93 95 Main.pleaseWaitDlg.progress.setValue(0); … … 106 108 if (cancel) 107 109 return null; 108 throw e;110 throw new OsmTransferException(e); 109 111 } catch (SAXException e) { 112 throw new OsmTransferException(e); 113 } catch(OsmTransferException e) { 110 114 throw e; 111 115 } catch (Exception e) { 112 116 if (cancel) 113 117 return null; 114 if (e instanceof RuntimeException) 115 throw (RuntimeException)e; 116 throw new RuntimeException(e); 118 throw new OsmTransferException(e); 117 119 } 118 120 } -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r1667 r1670 29 29 import org.openstreetmap.josm.Main; 30 30 import org.openstreetmap.josm.data.osm.Changeset; 31 import org.openstreetmap.josm.data.osm.Node;32 31 import org.openstreetmap.josm.data.osm.OsmPrimitive; 33 import org.openstreetmap.josm.data.osm.Relation; 34 import org.openstreetmap.josm.data.osm.Way; 32 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 35 33 import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor; 36 34 import org.xml.sax.Attributes; … … 67 65 if (api == null) { 68 66 api = new OsmApi(serverUrl); 67 instances.put(serverUrl,api); 69 68 } 70 69 return api; … … 80 79 String serverUrl = Main.pref.get("osm-server.url"); 81 80 if (serverUrl == null) 82 throw new IllegalStateException(tr("preference {0}missing. Can't initialize OsmApi", "osm-server.url"));81 throw new IllegalStateException(tr("preference ''{0}'' missing. Can't initialize OsmApi", "osm-server.url")); 83 82 return getOsmApi(serverUrl); 84 83 } … … 96 95 */ 97 96 private String version = null; 98 99 /**100 * Maximum downloadable area from server (degrees squared), from capabilities response101 * FIXME: make download dialog use this, instead of hard-coded default.102 */103 private String maxArea = null;104 97 105 98 /** the api capabilities */ … … 143 136 144 137 /** 145 * creates an instance of the OSM API. Initializes the server URL with the146 * value of the preference <code>osm-server.url</code>147 *148 * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set149 */150 protected OsmApi() {151 this.serverUrl = Main.pref.get("osm-server.url");152 if (serverUrl == null)153 throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url"));154 }155 156 /**157 * Helper that returns the lower-case type name of an OsmPrimitive158 * @param o the primitive159 * @return "node", "way", "relation", or "changeset"160 */161 public static String which(OsmPrimitive o) {162 if (o instanceof Node) return "node";163 if (o instanceof Way) return "way";164 if (o instanceof Relation) return "relation";165 if (o instanceof Changeset) return "changeset";166 return "";167 }168 169 /**170 138 * Returns the OSM protocol version we use to talk to the server. 171 139 * @return protocol version, or null if not yet negotiated. … … 185 153 /** 186 154 * Initializes this component by negotiating a protocol version with the server. 187 * 188 * @exception UnknownHostException thrown, if the API host is unknown 189 * @exception SocketTimeoutException thrown, if the connection to the API host times out 190 * @exception ConnectException throw, if the connection to the API host fails 191 * @exception Exception any other exception 155 * 156 * @exception OsmApiInitializationException thrown, if an exception occurs 192 157 */ 193 158 public void initialize() throws OsmApiInitializationException { … … 238 203 239 204 /** 240 * Helper that makes an int from the first whitespace separated token in a string.241 * @param s the string242 * @return the integer represenation of the first token in the string243 * @throws OsmTransferException if the string is empty or does not represent a number244 */245 public static int parseInt(String s) throws OsmTransferException {246 StringTokenizer t = new StringTokenizer(s);247 try {248 return Integer.parseInt(t.nextToken());249 } catch (Exception x) {250 throw new OsmTransferException(tr("Cannot read numeric value from response"));251 }252 }253 254 /**255 * Helper that makes a long from the first whitespace separated token in a string.256 * @param s the string257 * @return the long represenation of the first token in the string258 * @throws OsmTransferException if the string is empty or does not represent a number259 */260 public static long parseLong(String s) throws OsmTransferException {261 StringTokenizer t = new StringTokenizer(s);262 try {263 return Long.parseLong(t.nextToken());264 } catch (Exception x) {265 throw new OsmTransferException(tr("Cannot read numeric value from response"));266 }267 }268 269 /**270 205 * Returns the base URL for API requests, including the negotiated version number. 271 206 * @return base URL string … … 293 228 public void createPrimitive(OsmPrimitive osm) throws OsmTransferException { 294 229 initialize(); 295 osm.id = parseLong(sendRequest("PUT", which(osm)+"/create", toXml(osm, true))); 296 osm.version = 1; 230 String ret = ""; 231 try { 232 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true)); 233 osm.id = Long.parseLong(ret.trim()); 234 osm.version = 1; 235 } catch(NumberFormatException e){ 236 throw new OsmTransferException(tr("unexpected format of id replied by the server, got ''{0}''", ret)); 237 } 297 238 } 298 239 … … 309 250 if (version.equals("0.5")) { 310 251 // legacy mode does not return the new object version. 311 sendRequest("PUT", which(osm)+"/" + osm.id, toXml(osm, true));252 sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true)); 312 253 } else { 254 String ret = null; 313 255 // normal mode (0.6 and up) returns new object version. 314 osm.version = parseInt(sendRequest("PUT", which(osm)+"/" + osm.id, toXml(osm, true))); 256 try { 257 ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true)); 258 osm.version = Integer.parseInt(ret.trim()); 259 } catch(NumberFormatException e) { 260 throw new OsmTransferException(tr("unexpected format of new version of modified primitive ''{0}'', got ''{1}''", osm.id, ret)); 261 } 315 262 } 316 263 } … … 324 271 initialize(); 325 272 // legacy mode does not require payload. normal mode (0.6 and up) requires payload for version matching. 326 sendRequest("DELETE", which(osm)+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false));273 sendRequest("DELETE", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false)); 327 274 } 328 275 … … 334 281 public void createChangeset(String comment) throws OsmTransferException { 335 282 changeset = new Changeset(); 336 Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset..."));283 notifyStatusMessage(tr("Opening changeset...")); 337 284 Properties sysProp = System.getProperties(); 338 285 Object ua = sysProp.get("http.agent"); … … 349 296 public void stopChangeset() throws OsmTransferException { 350 297 initialize(); 351 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset..."));298 notifyStatusMessage(tr("Closing changeset...")); 352 299 sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null); 353 300 changeset = null; … … 372 319 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this); 373 320 321 notifyStatusMessage(tr("Preparing...")); 374 322 for (OsmPrimitive osm : list) { 375 int progress = Main.pleaseWaitDlg.progress.getValue();376 Main.pleaseWaitDlg.currentAction.setText(tr("Preparing..."));377 323 osm.visit(duv); 378 Main.pleaseWaitDlg.progress.setValue(progress+1); 379 } 380 381 Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); 324 notifyRelativeProgress(1); 325 } 326 notifyStatusMessage(tr("Uploading...")); 382 327 383 328 String diff = duv.getDocument(); … … 517 462 } 518 463 } 464 465 /** 466 * notifies any listeners about the current state of this API. Currently just 467 * displays the message in the global progress dialog, see {@see Main#pleaseWaitDlg} 468 * 469 * @param message a status message. 470 */ 471 protected void notifyStatusMessage(String message) { 472 Main.pleaseWaitDlg.currentAction.setText(message); 473 } 474 475 /** 476 * notifies any listeners about the current about a relative progress. Currently just 477 * increments the progress monitor in the in the global progress dialog, see {@see Main#pleaseWaitDlg} 478 * 479 * @param int the delta 480 */ 481 protected void notifyRelativeProgress(int delta) { 482 int current= Main.pleaseWaitDlg.progress.getValue(); 483 Main.pleaseWaitDlg.progress.setValue(current + delta); 484 } 519 485 } -
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r1523 r1670 22 22 23 23 import org.openstreetmap.josm.Main; 24 import org.openstreetmap.josm.gui.ExtendedDialog; 24 import org.openstreetmap.josm.gui.ExtendedDialog; 25 25 import org.openstreetmap.josm.tools.Base64; 26 26 import org.openstreetmap.josm.tools.GBC; … … 33 33 */ 34 34 public class OsmConnection { 35 36 public static class OsmParseException extends Exception {37 public OsmParseException() {super();}38 public OsmParseException(String message, Throwable cause) {super(message, cause);}39 public OsmParseException(String message) {super(message);}40 public OsmParseException(Throwable cause) {super(cause);}41 }42 35 43 36 protected boolean cancel = false; … … 77 70 if (passwordtried || username.equals("") || password.equals("")) { 78 71 JPanel p = new JPanel(new GridBagLayout()); 79 if (!username.equals("") && !password.equals("")) 72 if (!username.equals("") && !password.equals("")) { 80 73 p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop()); 74 } 81 75 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0)); 82 76 JTextField usernameField = new JTextField(username, 20); … … 92 86 p.add(savePassword, GBC.eop()); 93 87 94 int choice = new ExtendedDialog(Main.parent, 95 tr("Enter Password"), 88 int choice = new ExtendedDialog(Main.parent, 89 tr("Enter Password"), 96 90 p, 97 new String[] {tr("Login"), tr("Cancel")}, 98 new String[] {"ok.png", "cancel.png"}).getValue(); 99 91 new String[] {tr("Login"), tr("Cancel")}, 92 new String[] {"ok.png", "cancel.png"}).getValue(); 93 100 94 if (choice != 1) { 101 95 authCancelled = true; -
trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java
r1523 r1670 22 22 * Method to download OSM files from somewhere 23 23 */ 24 public DataSet parseOsm() throws SAXException, IOException { 24 @Override 25 public DataSet parseOsm() throws OsmTransferException { 25 26 try { 26 27 Main.pleaseWaitDlg.progress.setValue(0); … … 32 33 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 33 34 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 34 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2));35 // DataSource src = new DataSource(bounds, origin);36 // data.dataSources.add(src);37 35 in.close(); 38 36 activeConnection = null; … … 41 39 if (cancel) 42 40 return null; 43 throw e;41 throw new OsmTransferException(e); 44 42 } catch (SAXException e) { 43 throw new OsmTransferException(e); 44 } catch(OsmTransferException e) { 45 45 throw e; 46 46 } catch (Exception e) { 47 47 if (cancel) 48 48 return null; 49 if (e instanceof RuntimeException) 50 throw (RuntimeException)e; 51 throw new RuntimeException(e); 49 throw new OsmTransferException(e); 52 50 } 53 51 } -
trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java
r1523 r1670 7 7 import java.io.InputStream; 8 8 9 import javax.swing.JOptionPane; 10 9 11 import org.openstreetmap.josm.Main; 10 12 import org.openstreetmap.josm.data.osm.DataSet; 13 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 11 14 import org.xml.sax.SAXException; 12 13 import javax.swing.JOptionPane;14 15 15 16 public class OsmServerObjectReader extends OsmServerReader { 16 17 17 public final static String TYPE_WAY = "way";18 public final static String TYPE_REL = "relation";19 public final static String TYPE_NODE = "node";20 21 18 long id; 22 Stringtype;19 OsmPrimitiveType type; 23 20 boolean full; 24 21 25 public OsmServerObjectReader(long id, Stringtype, boolean full) {22 public OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full) { 26 23 this.id = id; 27 24 this.type = type; … … 34 31 * @throws IOException 35 32 */ 36 public DataSet parseOsm() throws SAXException, IOException { 33 @Override 34 public DataSet parseOsm() throws OsmTransferException { 37 35 try { 38 36 Main.pleaseWaitDlg.progress.setValue(0); 39 37 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server...")); 40 38 StringBuffer sb = new StringBuffer(); 41 sb.append(type );39 sb.append(type.getAPIName()); 42 40 sb.append("/"); 43 41 sb.append(id); 44 if (full )42 if (full && ! type.equals(OsmPrimitiveType.NODE)) { 45 43 sb.append("/full"); 44 } 46 45 47 46 final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg); … … 52 51 final DataSet data = osm.getDs(); 53 52 54 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2));55 // DataSource src = new DataSource(bounds, origin);56 // data.dataSources.add(src);57 53 if (osm.getParseNotes().length() != 0) { 58 54 JOptionPane.showMessageDialog(Main.parent, osm.getParseNotes()); … … 64 60 if (cancel) 65 61 return null; 66 throw e;62 throw new OsmTransferException(e); 67 63 } catch (SAXException e) { 64 throw new OsmTransferException(e); 65 } catch(OsmTransferException e) { 68 66 throw e; 69 67 } catch (Exception e) { 70 68 if (cancel) 71 69 return null; 72 if (e instanceof RuntimeException) 73 throw (RuntimeException)e; 74 throw new RuntimeException(e); 70 throw new OsmTransferException(e); 75 71 } 76 72 } -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r1664 r1670 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io. IOException;6 import java.io.BufferedReader; 7 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 8 9 import java.net.HttpURLConnection; 10 import java.net.MalformedURLException; 9 11 import java.net.URL; 12 import java.util.zip.GZIPInputStream; 10 13 import java.util.zip.Inflater; 11 14 import java.util.zip.InflaterInputStream; 12 import java.util.zip.GZIPInputStream;13 15 14 16 import javax.swing.JOptionPane; … … 17 19 import org.openstreetmap.josm.data.osm.DataSet; 18 20 import org.openstreetmap.josm.gui.PleaseWaitDialog; 19 import org.xml.sax.SAXException;20 21 21 22 /** … … 39 40 * @return An reader reading the input stream (servers answer) or <code>null</code>. 40 41 */ 41 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 42 43 // initialize API. Abort download in case of configuration or network 44 // errors 45 // 46 try { 47 api.initialize(); 48 } catch(Exception e) { 49 JOptionPane.showMessageDialog( 50 null, 51 tr( "Failed to initialize communication with the OSM server {0}.\n" 52 + "Check the server URL in your preferences and your internet connection.", 53 Main.pref.get("osm-server.url") 54 ), 55 tr("Error"), 56 JOptionPane.ERROR_MESSAGE 57 ); 58 e.printStackTrace(); 59 return null; 60 } 61 42 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException { 43 api.initialize(); 62 44 urlStr = api.getBaseUrl() + urlStr; 63 45 return getInputStreamRaw(urlStr, pleaseWaitDlg); 64 46 } 65 47 66 protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 67 68 // System.out.println("download: "+urlStr); 69 URL url = new URL(urlStr); 70 activeConnection = (HttpURLConnection)url.openConnection(); 48 protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException { 49 URL url = null; 50 try { 51 url = new URL(urlStr); 52 } catch(MalformedURLException e) { 53 throw new OsmTransferException(e); 54 } 55 try { 56 activeConnection = (HttpURLConnection)url.openConnection(); 57 } catch(Exception e) { 58 throw new OsmTransferException(tr("Failed to open connection to API {0}", url.toExternalForm()), e); 59 } 71 60 if (cancel) { 72 61 activeConnection.disconnect(); … … 81 70 82 71 try { 72 System.out.println("GET " + url); 83 73 activeConnection.connect(); 74 } catch (Exception e) { 75 throw new OsmTransferException(tr("Couldn't connect to the osm server. Please check your internet connection."), e); 84 76 } 85 catch (Exception e) { 86 throw new IOException(tr("Couldn't connect to the osm server. Please check your internet connection.")); 77 try { 78 if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 79 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null); 80 81 if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { 82 String errorHeader = activeConnection.getHeaderField("Error"); 83 InputStream i = null; 84 i = activeConnection.getErrorStream(); 85 StringBuilder errorBody = new StringBuilder(); 86 if (i != null) { 87 BufferedReader in = new BufferedReader(new InputStreamReader(i)); 88 String s; 89 while((s = in.readLine()) != null) { 90 errorBody.append(s); 91 errorBody.append("\n"); 92 } 93 } 94 95 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString()); 96 } 97 98 String encoding = activeConnection.getContentEncoding(); 99 InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg); 100 if (encoding != null && encoding.equalsIgnoreCase("gzip")) { 101 inputStream = new GZIPInputStream(inputStream); 102 } 103 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { 104 inputStream = new InflaterInputStream(inputStream, new Inflater(true)); 105 } 106 return inputStream; 107 } catch(Exception e) { 108 if (e instanceof OsmTransferException) 109 throw (OsmTransferException)e; 110 else 111 throw new OsmTransferException(e); 112 87 113 } 88 89 if (isAuthCancelled() && activeConnection.getResponseCode() == 401)90 return null;91 if (activeConnection.getResponseCode() == 500)92 throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time."));93 94 String encoding = activeConnection.getContentEncoding();95 InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);96 if (encoding != null && encoding.equalsIgnoreCase("gzip")) {97 inputStream = new GZIPInputStream(inputStream);98 }99 else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {100 inputStream = new InflaterInputStream(inputStream, new Inflater(true));101 }102 return inputStream;103 114 } 104 115 105 public abstract DataSet parseOsm() throws SAXException, IOException;116 public abstract DataSet parseOsm() throws OsmTransferException; 106 117 107 118 } -
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r1664 r1670 101 101 boolean useChangeset = Main.pref.getBoolean("osm-server.atomic-upload", apiVersion.compareTo("0.6")>=0); 102 102 if (useChangeset && ! canUseChangeset) { 103 System.out.println(tr("WARNING: preference ' {0}' or api version {1}of dataset requires to use changesets, but API is not handle them. Ignoring changesets.", "osm-server.atomic-upload", apiVersion));103 System.out.println(tr("WARNING: preference ''{0}'' or api version ''{1}'' of dataset requires to use changesets, but API is not handle them. Ignoring changesets.", "osm-server.atomic-upload", apiVersion)); 104 104 useChangeset = false; 105 105 } -
trunk/src/org/openstreetmap/josm/io/OsmWriter.java
r1640 r1670 11 11 import org.openstreetmap.josm.data.osm.Node; 12 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 13 14 import org.openstreetmap.josm.data.osm.Relation; 14 15 import org.openstreetmap.josm.data.osm.RelationMember; … … 25 26 26 27 public final String DEFAULT_API_VERSION = "0.6"; 27 28 28 29 /** 29 30 * The counter for newly created objects. Starts at -1 and goes down. 30 31 */ 31 32 private long newIdCounter = -1; 32 33 33 34 /** 34 35 * All newly created ids and their primitive that uses it. This is a back reference … … 41 42 private String version; 42 43 private Changeset changeset; 43 44 44 45 public OsmWriter(PrintWriter out, boolean osmConform, String version) { 45 46 super(out); … … 47 48 this.version = (version == null ? DEFAULT_API_VERSION : version); 48 49 } 49 50 50 51 public void setWithBody(boolean wb) { 51 52 this.withBody = wb; … … 57 58 this.version = v; 58 59 } 59 60 60 61 public void header() { 61 62 out.println("<?xml version='1.0' encoding='UTF-8'?>"); … … 70 71 public void writeContent(DataSet ds) { 71 72 for (Node n : ds.nodes) 72 if (shouldWrite(n)) 73 if (shouldWrite(n)) { 73 74 visit(n); 75 } 74 76 for (Way w : ds.ways) 75 if (shouldWrite(w)) 77 if (shouldWrite(w)) { 76 78 visit(w); 79 } 77 80 for (Relation e : ds.relations) 78 if (shouldWrite(e)) 81 if (shouldWrite(e)) { 79 82 visit(e); 83 } 80 84 } 81 85 … … 100 104 out.print(" lat='"+n.getCoor().lat()+"' lon='"+n.getCoor().lon()+"'"); 101 105 if (!withBody) { 102 out.println("/>"); 106 out.println("/>"); 103 107 } else { 104 108 addTags(n, "node", true); … … 110 114 addCommon(w, "way"); 111 115 if (!withBody) { 112 out.println("/>"); 116 out.println("/>"); 113 117 } else { 114 118 out.println(">"); 115 for (Node n : w.nodes) 119 for (Node n : w.nodes) { 116 120 out.println(" <nd ref='"+getUsedId(n)+"' />"); 121 } 117 122 addTags(w, "way", false); 118 123 } … … 123 128 addCommon(e, "relation"); 124 129 if (!withBody) { 125 out.println("/>"); 130 out.println("/>"); 126 131 } else { 127 132 out.println(">"); 128 133 for (RelationMember em : e.members) { 129 134 out.print(" <member type='"); 130 out.print(Osm Api.which(em.member));135 out.print(OsmPrimitiveType.from(em.member).getAPIName()); 131 136 out.println("' ref='"+getUsedId(em.member)+"' role='" + 132 137 XmlWriter.encode(em.role == null ? "" : em.role) + "' />"); … … 160 165 private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) { 161 166 if (osm.keys != null) { 162 if (tagOpen) 167 if (tagOpen) { 163 168 out.println(">"); 164 for (Entry<String, String> e : osm.keys.entrySet()) 169 } 170 for (Entry<String, String> e : osm.keys.entrySet()) { 165 171 out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) + 166 172 "' v='"+XmlWriter.encode(e.getValue())+ "' />"); 173 } 167 174 out.println(" </" + tagname + ">"); 168 } else if (tagOpen) 175 } else if (tagOpen) { 169 176 out.println(" />"); 170 else177 } else { 171 178 out.println(" </" + tagname + ">"); 179 } 172 180 } 173 181 … … 180 188 out.print(" <"+tagname); 181 189 if (id != 0) { 182 190 out.print(" id='"+getUsedId(osm)+"'"); 183 191 } 184 192 if (!osmConform) { 185 193 String action = null; 186 if (osm.deleted) 194 if (osm.deleted) { 187 195 action = "delete"; 188 else if (osm.modified)196 } else if (osm.modified) { 189 197 action = "modify"; 190 if (action != null) 198 } 199 if (action != null) { 191 200 out.print(" action='"+action+"'"); 201 } 192 202 } 193 203 if (!osm.isTimestampEmpty()) { … … 199 209 } 200 210 out.print(" visible='"+osm.visible+"'"); 201 if (osm.version != -1) 211 if (osm.version != -1) { 202 212 out.print(" version='"+osm.version+"'"); 203 if (this.changeset != null && this.changeset.id != 0) 213 } 214 if (this.changeset != null && this.changeset.id != 0) { 204 215 out.print(" changeset='"+this.changeset.id+"'" ); 205 } 206 216 } 217 } 218 207 219 public void close() { 208 220 out.close(); 209 221 } 210 222 211 223 public void flush() { 212 224 out.flush(); -
trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java
r1169 r1670 22 22 private PleaseWaitDialog pleaseWaitDlg; 23 23 24 public class OsmServerException extends IOException { 25 private OsmServerException(String e) { 26 super(e); 27 } 28 } 29 30 public ProgressInputStream(URLConnection con, PleaseWaitDialog pleaseWaitDlg) throws IOException, OsmServerException { 24 public ProgressInputStream(URLConnection con, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException { 31 25 this.connection = con; 32 26 … … 35 29 } catch (IOException e) { 36 30 if (con.getHeaderField("Error") != null) 37 throw new Osm ServerException(tr(con.getHeaderField("Error")));38 throw e;31 throw new OsmTransferException(tr(con.getHeaderField("Error"))); 32 throw new OsmTransferException(e); 39 33 } 40 34 … … 43 37 if (pleaseWaitDlg == null) 44 38 return; 45 if (contentLength > 0) 39 if (contentLength > 0) { 46 40 pleaseWaitDlg.progress.setMaximum(contentLength); 47 else41 } else { 48 42 pleaseWaitDlg.progress.setMaximum(0); 43 } 49 44 pleaseWaitDlg.progress.setValue(0); 50 45 } … … 56 51 @Override public int read(byte[] b, int off, int len) throws IOException { 57 52 int read = in.read(b, off, len); 58 if (read != -1) 53 if (read != -1) { 59 54 advanceTicker(read); 55 } 60 56 return read; 61 57 } … … 63 59 @Override public int read() throws IOException { 64 60 int read = in.read(); 65 if (read != -1) 61 if (read != -1) { 66 62 advanceTicker(1); 63 } 67 64 return read; 68 65 } … … 76 73 return; 77 74 78 if (pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1) 75 if (pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1) { 79 76 pleaseWaitDlg.progress.setMaximum(connection.getContentLength()); 77 } 80 78 81 79 readSoFar += amount; … … 89 87 String cur = pleaseWaitDlg.currentAction.getText(); 90 88 int i = cur.indexOf(' '); 91 if (i != -1) 89 if (i != -1) { 92 90 cur = cur.substring(0, i) + progStr; 93 else91 } else { 94 92 cur += progStr; 93 } 95 94 pleaseWaitDlg.currentAction.setText(cur); 96 95 }
Note:
See TracChangeset
for help on using the changeset viewer.