Ignore:
Timestamp:
2023-06-13T21:32:22+02:00 (2 years ago)
Author:
taylor.smock
Message:

Fix #9446: Show progress on upload validation (patch updated by gaben)

This does the following:

  • Use ValidationTask in more locations (ValidatorCLI and ValidateUploadHook specifically)
  • Add an async method for checking upload conditions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r18706 r18752  
    6464/**
    6565 * Dialog that pops up when the user closes a layer with modified data.
    66  *
    67  * It asks for confirmation that all modification should be discarded and offers
     66 * <p>
     67 * It asks for confirmation that all modifications should be discarded and offer
    6868 * to save the layers to file or upload to server, depending on the type of layer.
    6969 */
     
    450450                    closeDialog();
    451451                }
    452             } catch (UserCancelException ignore) {
    453                 Logging.trace(ignore);
     452            } catch (UserCancelException userCancelException) {
     453                Logging.trace(userCancelException);
    454454            }
    455455        }
     
    557557                AbstractModifiableLayer layer = layerInfo.getLayer();
    558558                if (canceled) {
     559                    GuiHelper.runInEDTAndWait(() -> model.setUploadState(layer, UploadOrSaveState.CANCELED));
     560                    continue;
     561                }
     562                GuiHelper.runInEDTAndWait(() -> monitor.subTask(tr("Preparing layer ''{0}'' for upload ...", layerInfo.getName())));
     563
     564                // checkPreUploadConditions must not be run in the EDT to avoid deadlocks
     565                if (!UploadAction.checkPreUploadConditions(layer)) {
     566                    GuiHelper.runInEDTAndWait(() -> model.setUploadState(layer, UploadOrSaveState.FAILED));
     567                    continue;
     568                }
     569
     570                GuiHelper.runInEDTAndWait(() -> uploadLayersUploadModelStateOnFinish(layer));
     571                currentTask = null;
     572            }
     573        }
     574
     575        /**
     576         * Update the {@link #model} state on upload finish
     577         * @param layer The layer that has been saved
     578         */
     579        private void uploadLayersUploadModelStateOnFinish(AbstractModifiableLayer layer) {
     580            AbstractUploadDialog dialog = layer.getUploadDialog();
     581            if (dialog != null) {
     582                dialog.setVisible(true);
     583                if (dialog.isCanceled()) {
    559584                    model.setUploadState(layer, UploadOrSaveState.CANCELED);
    560                     continue;
    561                 }
    562                 monitor.subTask(tr("Preparing layer ''{0}'' for upload ...", layerInfo.getName()));
    563 
    564                 if (!UploadAction.checkPreUploadConditions(layer)) {
    565                     model.setUploadState(layer, UploadOrSaveState.FAILED);
    566                     continue;
    567                 }
    568 
    569                 AbstractUploadDialog dialog = layer.getUploadDialog();
    570                 if (dialog != null) {
    571                     dialog.setVisible(true);
    572                     if (dialog.isCanceled()) {
    573                         model.setUploadState(layer, UploadOrSaveState.CANCELED);
    574                         continue;
    575                     }
    576                     dialog.rememberUserInput();
    577                 }
    578 
    579                 currentTask = layer.createUploadTask(monitor);
    580                 if (currentTask == null) {
    581                     model.setUploadState(layer, UploadOrSaveState.FAILED);
    582                     continue;
    583                 }
    584                 Future<?> currentFuture = worker.submit(currentTask);
    585                 try {
    586                     // wait for the asynchronous task to complete
    587                     currentFuture.get();
    588                 } catch (CancellationException e) {
    589                     Logging.trace(e);
    590                     model.setUploadState(layer, UploadOrSaveState.CANCELED);
    591                 } catch (InterruptedException | ExecutionException e) {
    592                     Logging.error(e);
    593                     model.setUploadState(layer, UploadOrSaveState.FAILED);
    594                     ExceptionDialogUtil.explainException(e);
    595                 }
    596                 if (currentTask.isCanceled()) {
    597                     model.setUploadState(layer, UploadOrSaveState.CANCELED);
    598                 } else if (currentTask.isFailed()) {
    599                     Logging.error(currentTask.getLastException());
    600                     ExceptionDialogUtil.explainException(currentTask.getLastException());
    601                     model.setUploadState(layer, UploadOrSaveState.FAILED);
    602                 } else {
    603                     model.setUploadState(layer, UploadOrSaveState.OK);
    604                 }
    605                 currentTask = null;
     585                    return;
     586                }
     587                dialog.rememberUserInput();
     588            }
     589
     590            currentTask = layer.createUploadTask(monitor);
     591            if (currentTask == null) {
     592                model.setUploadState(layer, UploadOrSaveState.FAILED);
     593                return;
     594            }
     595            Future<?> currentFuture = worker.submit(currentTask);
     596            try {
     597                // wait for the asynchronous task to complete
     598                currentFuture.get();
     599            } catch (CancellationException e) {
     600                Logging.trace(e);
     601                model.setUploadState(layer, UploadOrSaveState.CANCELED);
     602            } catch (InterruptedException e) {
     603                Thread.currentThread().interrupt();
     604                Logging.error(e);
     605                model.setUploadState(layer, UploadOrSaveState.FAILED);
     606                ExceptionDialogUtil.explainException(e);
     607            } catch (ExecutionException e) {
     608                Logging.error(e);
     609                model.setUploadState(layer, UploadOrSaveState.FAILED);
     610                ExceptionDialogUtil.explainException(e);
     611            }
     612            if (currentTask.isCanceled()) {
     613                model.setUploadState(layer, UploadOrSaveState.CANCELED);
     614            } else if (currentTask.isFailed()) {
     615                Logging.error(currentTask.getLastException());
     616                ExceptionDialogUtil.explainException(currentTask.getLastException());
     617                model.setUploadState(layer, UploadOrSaveState.FAILED);
     618            } else {
     619                model.setUploadState(layer, UploadOrSaveState.OK);
    606620            }
    607621        }
     
    673687        @Override
    674688        public void run() {
     689            GuiHelper.runInEDTAndWait(() -> model.setMode(SaveLayersModel.Mode.UPLOADING_AND_SAVING));
     690            // We very specifically do not want to block the EDT or the worker thread when validating
     691            List<SaveLayerInfo> toUpload = model.getLayersToUpload();
     692            if (!toUpload.isEmpty()) {
     693                uploadLayers(toUpload);
     694            }
    675695            GuiHelper.runInEDTAndWait(() -> {
    676                 model.setMode(SaveLayersModel.Mode.UPLOADING_AND_SAVING);
    677                 List<SaveLayerInfo> toUpload = model.getLayersToUpload();
    678                 if (!toUpload.isEmpty()) {
    679                     uploadLayers(toUpload);
    680                 }
    681696                List<SaveLayerInfo> toSave = model.getLayersToSave();
    682697                if (!toSave.isEmpty()) {
Note: See TracChangeset for help on using the changeset viewer.