Ticket #19939: 19939.patch

File 19939.patch, 11.4 KB (added by GerdP, 5 years ago)
  • src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

     
    133133    public void dataChanged(DataChangedEvent event) {
    134134        // just trigger a repaint - the display name of the relation members may have changed
    135135        Collection<RelationMember> sel = getSelectedMembers();
    136         GuiHelper.runInEDT(this::fireTableDataChanged);
     136        GuiHelper.runInEDTAndWait(this::fireTableDataChanged);
    137137        setSelectedMembers(sel);
    138138    }
    139139
  • src/org/openstreetmap/josm/gui/dialogs/relation/ParentRelationLoadingTask.java

     
    1 // License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.dialogs.relation;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.io.IOException;
    7 import java.util.ArrayList;
    8 import java.util.List;
    9 import java.util.Optional;
    10 
    11 import javax.swing.JOptionPane;
    12 import javax.swing.SwingUtilities;
    13 
    14 import org.openstreetmap.josm.data.osm.DataSet;
    15 import org.openstreetmap.josm.data.osm.DataSetMerger;
    16 import org.openstreetmap.josm.data.osm.Relation;
    17 import org.openstreetmap.josm.gui.MainApplication;
    18 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    19 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    20 import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
    21 import org.openstreetmap.josm.io.OsmApi;
    22 import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
    23 import org.openstreetmap.josm.io.OsmTransferException;
    24 import org.openstreetmap.josm.tools.CheckParameterUtil;
    25 import org.openstreetmap.josm.tools.Logging;
    26 import org.xml.sax.SAXException;
    27 
    28 /**
    29  * This is an asynchronous task for loading the parents of a given relation.
    30  *
    31  * Typical usage:
    32  * <pre>
    33  *  final ParentRelationLoadingTask task = new ParentRelationLoadingTask(
    34  *                   child,   // the child relation
    35  *                   MainApplication.getLayerManager().getEditLayer(), // the edit layer
    36  *                   true,  // load fully
    37  *                   new PleaseWaitProgressMonitor()  // a progress monitor
    38  *   );
    39  *   task.setContinuation(
    40  *       new Runnable() {
    41  *          public void run() {
    42  *              if (task.isCanceled() || task.hasError())
    43  *                  return;
    44  *              List&lt;Relation&gt; parents = task.getParents();
    45  *              // do something with the parent relations
    46  *       }
    47  *   );
    48  *
    49  *   // start the task
    50  *   MainApplication.worker.submit(task);
    51  * </pre>
    52  *
    53  */
    54 public class ParentRelationLoadingTask extends PleaseWaitRunnable {
    55     private boolean canceled;
    56     private Exception lastException;
    57     private DataSet referrers;
    58     private final boolean full;
    59     private final OsmDataLayer layer;
    60     private final Relation child;
    61     private final List<Relation> parents;
    62     private Runnable continuation;
    63 
    64     /**
    65      * Creates a new task for asynchronously downloading the parents of a child relation.
    66      *
    67      * @param child the child relation. Must not be null. Must have an id &gt; 0.
    68      * @param layer  the OSM data layer. Must not be null.
    69      * @param full if true, parent relations are fully downloaded (i.e. with their members)
    70      * @param monitor the progress monitor to be used
    71      *
    72      * @throws IllegalArgumentException if child is null
    73      * @throws IllegalArgumentException if layer is null
    74      * @throws IllegalArgumentException if child.getId() == 0
    75      */
    76     public ParentRelationLoadingTask(Relation child, OsmDataLayer layer, boolean full, PleaseWaitProgressMonitor monitor) {
    77         super(tr("Download referring relations"), monitor, false /* don't ignore exception */);
    78         CheckParameterUtil.ensureThat(child.getUniqueId() > 0, "id > 0");
    79         CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    80         if (!layer.isDownloadable()) {
    81             throw new IllegalArgumentException("Non-downloadable layer: " + layer);
    82         }
    83         referrers = null;
    84         this.layer = layer;
    85         parents = new ArrayList<>();
    86         this.child = child;
    87         this.full = full;
    88     }
    89 
    90     /**
    91      * Set a continuation which is called upon the job finished.
    92      *
    93      * @param continuation the continuation
    94      */
    95     public void setContinuation(Runnable continuation) {
    96         this.continuation = continuation;
    97     }
    98 
    99     /**
    100      * Replies true if this has been canceled by the user.
    101      *
    102      * @return true if this has been canceled by the user.
    103      */
    104     public boolean isCanceled() {
    105         return canceled;
    106     }
    107 
    108     /**
    109      * Replies true if an exception has been caught during the execution of this task.
    110      *
    111      * @return true if an exception has been caught during the execution of this task.
    112      */
    113     public boolean hasError() {
    114         return lastException != null;
    115     }
    116 
    117     protected OsmDataLayer getLayer() {
    118         return layer;
    119     }
    120 
    121     public List<Relation> getParents() {
    122         return parents;
    123     }
    124 
    125     @Override
    126     protected void cancel() {
    127         canceled = true;
    128         OsmApi.getOsmApi().cancel();
    129     }
    130 
    131     protected void showLastException() {
    132         JOptionPane.showMessageDialog(
    133                 MainApplication.getMainFrame(),
    134                 Optional.ofNullable(lastException.getMessage()).orElseGet(lastException::toString),
    135                 tr("Error"),
    136                 JOptionPane.ERROR_MESSAGE
    137         );
    138     }
    139 
    140     @Override
    141     protected void finish() {
    142         if (canceled) return;
    143         if (lastException != null) {
    144             showLastException();
    145             return;
    146         }
    147         parents.clear();
    148         for (Relation parent : referrers.getRelations()) {
    149             parents.add((Relation) getLayer().data.getPrimitiveById(parent));
    150         }
    151         if (continuation != null) {
    152             continuation.run();
    153         }
    154     }
    155 
    156     @Override
    157     protected void realRun() throws SAXException, IOException, OsmTransferException {
    158         try {
    159             progressMonitor.indeterminateSubTask(null);
    160             OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(child, full);
    161             referrers = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
    162             if (referrers != null) {
    163                 final DataSetMerger visitor = new DataSetMerger(getLayer().getDataSet(), referrers);
    164                 visitor.merge();
    165 
    166                 // copy the merged layer's data source info
    167                 getLayer().getDataSet().addDataSources(referrers.getDataSources());
    168                 // FIXME: this is necessary because there are dialogs listening
    169                 // for DataChangeEvents which manipulate Swing components on this thread.
    170                 SwingUtilities.invokeLater(getLayer()::onPostDownloadFromServer);
    171 
    172                 if (visitor.getConflicts().isEmpty())
    173                     return;
    174                 getLayer().getConflicts().add(visitor.getConflicts());
    175                 JOptionPane.showMessageDialog(
    176                         MainApplication.getMainFrame(),
    177                         tr("There were {0} conflicts during import.",
    178                                 visitor.getConflicts().size()),
    179                                 tr("Warning"),
    180                                 JOptionPane.WARNING_MESSAGE
    181                 );
    182             }
    183         } catch (OsmTransferException e) {
    184             if (canceled) {
    185                 Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
    186                 return;
    187             }
    188             lastException = e;
    189         }
    190     }
    191 }
  • src/org/openstreetmap/josm/gui/dialogs/relation/ReferringRelationsBrowser.java

    Property changes on: src/org/openstreetmap/josm/gui/dialogs/relation/ParentRelationLoadingTask.java
    ___________________________________________________________________
    Deleted: svn:eol-style
    ## -1 +0,0 ##
    -native
    \ No newline at end of property
     
    99import java.awt.event.ActionEvent;
    1010import java.awt.event.MouseAdapter;
    1111import java.awt.event.MouseEvent;
     12import java.util.Collections;
     13import java.util.List;
     14import java.util.stream.Collectors;
    1215
    1316import javax.swing.AbstractAction;
    1417import javax.swing.JButton;
     
    2225import javax.swing.event.ListSelectionEvent;
    2326import javax.swing.event.ListSelectionListener;
    2427
     28import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
     29import org.openstreetmap.josm.data.osm.OsmPrimitive;
     30import org.openstreetmap.josm.data.osm.PrimitiveId;
    2531import org.openstreetmap.josm.data.osm.Relation;
    2632import org.openstreetmap.josm.gui.MainApplication;
    2733import org.openstreetmap.josm.gui.PrimitiveRenderer;
     34import org.openstreetmap.josm.gui.io.DownloadPrimitivesTask;
    2835import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    29 import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
     36import org.openstreetmap.josm.gui.util.GuiHelper;
    3037import org.openstreetmap.josm.tools.ImageProvider;
    3138
    3239/**
     
    117124
    118125        @Override
    119126        public void actionPerformed(ActionEvent e) {
    120             boolean full = cbReadFull.isSelected();
    121             final ParentRelationLoadingTask task = new ParentRelationLoadingTask(
    122                     model.getRelation(),
    123                     getLayer(),
    124                     full,
    125                     new PleaseWaitProgressMonitor(tr("Loading parent relations"))
    126             );
    127             task.setContinuation(() -> {
    128                     if (task.isCanceled() || task.hasError())
    129                         return;
    130                     model.populate(task.getParents());
    131                 });
     127            DownloadReferrersTask task = new DownloadReferrersTask(getLayer(), Collections.singleton(model.getRelation()));
    132128            MainApplication.worker.submit(task);
     129            MainApplication.worker.submit(() -> {
     130                if (cbReadFull.isSelected() && !task.getProgressMonitor().isCanceled()) {
     131                    // download all members of parents
     132                    List<PrimitiveId> parentsChildren = model.getRelation().referrers(Relation.class)
     133                            .collect(Collectors.toSet()).stream().flatMap(r -> r.getMemberPrimitives().stream())
     134                            .distinct().map(OsmPrimitive::getPrimitiveId).distinct().collect(Collectors.toList());
     135                    new DownloadPrimitivesTask(getLayer(), parentsChildren, false).run();
     136                }
     137            });
     138            GuiHelper.executeByMainWorkerInEDT(() ->
     139                model.populate(model.getRelation().getReferrers().stream()
     140                        .filter(Relation.class::isInstance)
     141                        .map(Relation.class::cast)
     142                        .collect(Collectors.toList()))
     143                );
    133144        }
    134145
    135146        @Override