Fix issue with UI blocking

Signed-off-by: Taylor Smock <tsmock@fb.com>
pull/1/head
Taylor Smock 2022-05-13 08:53:20 -06:00
rodzic 5a265e6520
commit 0d24b66fd7
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
3 zmienionych plików z 16 dodań i 12 usunięć

Wyświetl plik

@ -168,7 +168,7 @@ public final class MapWithAIDataUtils {
GuiHelper.runInEDT(noUrls::show); GuiHelper.runInEDT(noUrls::show);
if (MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty() if (MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty()
&& MapWithAILayerInfo.getInstance().getDefaultLayers().isEmpty()) { && MapWithAILayerInfo.getInstance().getDefaultLayers().isEmpty()) {
MapWithAILayerInfo.getInstance().loadDefaults(true, MainApplication.worker, false, MapWithAILayerInfo.getInstance().loadDefaults(true, MapWithAIDataUtils.getForkJoinPool(), false,
() -> Logging.info("MapWithAI Sources: Initialized sources")); () -> Logging.info("MapWithAI Sources: Initialized sources"));
} }
} }

Wyświetl plik

@ -19,8 +19,9 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -35,6 +36,7 @@ import org.openstreetmap.josm.gui.PleaseWaitRunnable;
import org.openstreetmap.josm.io.CachedFile; import org.openstreetmap.josm.io.CachedFile;
import org.openstreetmap.josm.io.NetworkManager; import org.openstreetmap.josm.io.NetworkManager;
import org.openstreetmap.josm.io.imagery.ImageryReader; import org.openstreetmap.josm.io.imagery.ImageryReader;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo.MapWithAIPreferenceEntry; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo.MapWithAIPreferenceEntry;
import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.ESRISourceReader; import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.ESRISourceReader;
import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.MapWithAISourceReader; import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.MapWithAISourceReader;
@ -192,7 +194,7 @@ public class MapWithAILayerInfo {
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
Logging.trace("MapWithAI loaded: {0}", ESRISourceReader.SOURCE_CACHE.getClass()); Logging.trace("MapWithAI loaded: {0}", ESRISourceReader.SOURCE_CACHE.getClass());
} }
loadDefaults(false, MainApplication.worker, fastFail, listener); loadDefaults(false, MapWithAIDataUtils.getForkJoinPool(), fastFail, listener);
} }
/** /**
@ -210,7 +212,7 @@ public class MapWithAILayerInfo {
* @param listener A listener to call when the everything is done * @param listener A listener to call when the everything is done
* @since 12634 * @since 12634
*/ */
public void loadDefaults(boolean clearCache, ExecutorService worker, boolean fastFail, FinishListener listener) { public void loadDefaults(boolean clearCache, ForkJoinPool worker, boolean fastFail, FinishListener listener) {
final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail); final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
if (this.finishListenerListenerList == null) { if (this.finishListenerListenerList == null) {
this.finishListenerListenerList = ListenerList.create(); this.finishListenerListenerList = ListenerList.create();
@ -227,7 +229,7 @@ public class MapWithAILayerInfo {
@Override @Override
protected void realRun() { protected void realRun() {
loader.run(); loader.compute();
} }
@Override @Override
@ -257,7 +259,7 @@ public class MapWithAILayerInfo {
/** /**
* Loader/updater of the available imagery entries * Loader/updater of the available imagery entries
*/ */
class DefaultEntryLoader implements Runnable { class DefaultEntryLoader extends RecursiveTask<List<MapWithAIInfo>> {
private final boolean clearCache; private final boolean clearCache;
private final boolean fastFail; private final boolean fastFail;
@ -277,7 +279,7 @@ public class MapWithAILayerInfo {
} }
@Override @Override
public void run() { public List<MapWithAIInfo> compute() {
if (this.clearCache) { if (this.clearCache) {
ESRISourceReader.SOURCE_CACHE.clear(); ESRISourceReader.SOURCE_CACHE.clear();
} }
@ -294,7 +296,7 @@ public class MapWithAILayerInfo {
} }
for (String source : getImageryLayersSites()) { for (String source : getImageryLayersSites()) {
if (canceled) { if (canceled) {
return; return this.newLayers;
} }
loadSource(source); loadSource(source);
} }
@ -315,6 +317,7 @@ public class MapWithAILayerInfo {
} }
} }
this.finish(); this.finish();
return this.newLayers;
} }
protected void loadSource(String source) { protected void loadSource(String source) {
@ -609,11 +612,11 @@ public class MapWithAILayerInfo {
} }
/** /**
* Remove preview layers, if {@link SHOW_PREVIEW} is not {@code true} * Remove preview layers, if {@link #SHOW_PREVIEW} is not {@code true}
* *
* @param layers The layers to filter * @param layers The layers to filter
* @return The layers without any preview layers, if {@link SHOW_PREVIEW} is not * @return The layers without any preview layers, if {@link #SHOW_PREVIEW} is
* {@code true}. * not {@code true}.
*/ */
private static List<MapWithAIInfo> filterPreview(List<MapWithAIInfo> layers) { private static List<MapWithAIInfo> filterPreview(List<MapWithAIInfo> layers) {
if (Boolean.TRUE.equals(SHOW_PREVIEW.get()) && ExpertToggleAction.isExpert()) { if (Boolean.TRUE.equals(SHOW_PREVIEW.get()) && ExpertToggleAction.isExpert()) {

Wyświetl plik

@ -61,6 +61,7 @@ import org.openstreetmap.josm.gui.preferences.imagery.ImageryProvidersPanel;
import org.openstreetmap.josm.gui.util.GuiHelper; import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.gui.widgets.FilterField; import org.openstreetmap.josm.gui.widgets.FilterField;
import org.openstreetmap.josm.gui.widgets.HtmlPanel; import org.openstreetmap.josm.gui.widgets.HtmlPanel;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAICategory; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAICategory;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
@ -854,7 +855,7 @@ public class MapWithAIProvidersPanel extends JPanel {
@Override @Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
this.setEnabled(false); this.setEnabled(false);
MapWithAILayerInfo.getInstance().loadDefaults(true, MainApplication.worker, false, () -> MapWithAILayerInfo.getInstance().loadDefaults(true, MapWithAIDataUtils.getForkJoinPool(), false, () ->
// This needs to be run in a block to avoid race conditions. // This needs to be run in a block to avoid race conditions.
GuiHelper.runInEDT(() -> { GuiHelper.runInEDT(() -> {
defaultTable.getSelectionModel().clearSelection(); defaultTable.getSelectionModel().clearSelection();