JAL-3878 Create web service class - container for service metadata and actions
[jalview.git] / src / jalview / gui / SlivkaPreferences.java
index f4f6a9a..89ff3e4 100644 (file)
@@ -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;
@@ -29,6 +32,7 @@ import javax.swing.JPanel;
 import javax.swing.JProgressBar;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
+import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.table.AbstractTableModel;
 import javax.swing.table.DefaultTableCellRenderer;
@@ -45,7 +49,7 @@ public class SlivkaPreferences extends JPanel
 
   private final ArrayList<String> urls = new ArrayList<>();
 
-  private final ArrayList<Integer> statuses = new ArrayList<>();
+  private final Map<String, Integer> statuses = new HashMap<>();
 
   private final AbstractTableModel urlTableModel = new AbstractTableModel()
   {
@@ -65,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();
       }
@@ -140,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,
@@ -172,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);
     }
@@ -186,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);
       }
@@ -283,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);
@@ -306,13 +309,20 @@ public class SlivkaPreferences extends JPanel
 
   // Discoverer buttons action listeners
   private ActionListener refreshServicesAction = (ActionEvent e) -> {
-    new Thread(() -> {
-      progressBar.setVisible(true);
-      Cache.log.info("Requesting service reload");
-      Desktop.instance.startServiceDiscovery(discoverer, true);
-      Cache.log.info("Reloading done");
-      progressBar.setVisible(false);
-    }).start();
+    progressBar.setVisible(true);
+    Cache.log.info("Requesting service reload");
+    discoverer.startDiscoverer().handle((_discoverer, exception) -> {
+      if (exception == null)
+      {
+        Cache.log.info("Reloading done");
+      }
+      else
+      {
+        Cache.log.error("Reloading failed", exception);
+      }
+      SwingUtilities.invokeLater(() -> progressBar.setVisible(false));
+      return null;
+    });
   };
 
   private ActionListener resetServicesAction = (ActionEvent e) -> {
@@ -322,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();
   };
@@ -354,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);
+        });
+  }
 }