From 1baa5fe32db5de762afc3a82efa6a58fd6b488a3 Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Thu, 25 Nov 2021 19:12:17 +0100 Subject: [PATCH] JAL-3927 Make url checks asynchronous in preferences dialog --- src/jalview/gui/SlivkaPreferences.java | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/jalview/gui/SlivkaPreferences.java b/src/jalview/gui/SlivkaPreferences.java index 6c365b9..89ff3e4 100644 --- a/src/jalview/gui/SlivkaPreferences.java +++ b/src/jalview/gui/SlivkaPreferences.java @@ -18,7 +18,10 @@ import java.awt.event.MouseListener; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.NoSuchElementException; +import java.util.concurrent.CompletableFuture; import javax.swing.BorderFactory; import javax.swing.Box; @@ -46,7 +49,7 @@ public class SlivkaPreferences extends JPanel private final ArrayList urls = new ArrayList<>(); - private final ArrayList statuses = new ArrayList<>(); + private final Map statuses = new HashMap<>(); private final AbstractTableModel urlTableModel = new AbstractTableModel() { @@ -66,7 +69,7 @@ public class SlivkaPreferences extends JPanel case 0: return urls.get(rowIndex); case 1: - return statuses.get(rowIndex); + return statuses.getOrDefault(urls.get(rowIndex), WSDiscovererI.STATUS_UNKNOWN); default: throw new NoSuchElementException(); } @@ -141,7 +144,7 @@ public class SlivkaPreferences extends JPanel { String input = (String) JvOptionPane .showInternalInputDialog( - this, + this, MessageManager.getString("label.url:"), UIManager.getString("OptionPane.inputDialogTitle", MessageManager.getLocale()), JOptionPane.QUESTION_MESSAGE, @@ -173,7 +176,7 @@ public class SlivkaPreferences extends JPanel if (input != null) { urls.add(input); - statuses.add(discoverer.getServerStatusFor(input)); + reloadStatusForUrl(input); urlTableModel.fireTableRowsInserted(urls.size(), urls.size()); discoverer.setServiceUrls(urls); } @@ -187,7 +190,8 @@ public class SlivkaPreferences extends JPanel if (input != null) { urls.set(i, input); - statuses.set(i, discoverer.getServerStatusFor(input)); + statuses.remove(input); + reloadStatusForUrl(input); urlTableModel.fireTableRowsUpdated(i, i); discoverer.setServiceUrls(urls); } @@ -284,9 +288,7 @@ public class SlivkaPreferences extends JPanel String url = urls.get(fromIndex); int status = statuses.get(fromIndex); urls.set(fromIndex, urls.get(toIndex)); - statuses.set(fromIndex, statuses.get(toIndex)); urls.set(toIndex, url); - statuses.set(toIndex, status); if (urlListTable.getSelectedRow() == fromIndex) { urlListTable.setRowSelectionInterval(toIndex, toIndex); @@ -330,7 +332,7 @@ public class SlivkaPreferences extends JPanel urls.addAll(discoverer.getServiceUrls()); for (String url : urls) { - statuses.add(discoverer.getServerStatusFor(url)); + reloadStatusForUrl(url); } urlTableModel.fireTableDataChanged(); }; @@ -362,7 +364,18 @@ public class SlivkaPreferences extends JPanel urls.addAll(discoverer.getServiceUrls()); for (String url : urls) { - statuses.add(discoverer.getServerStatusFor(url)); + reloadStatusForUrl(url); } } + + private void reloadStatusForUrl(String url) + { + CompletableFuture.supplyAsync(() -> discoverer.getServerStatusFor(url)) + .thenAccept((status) -> { + statuses.put(url, status); + int row = urls.indexOf(url); + if (row >= 0) + urlTableModel.fireTableCellUpdated(row, 1); + }); + } } -- 1.7.10.2