From: Mateusz Warowny Date: Mon, 28 Mar 2022 13:52:33 +0000 (+0200) Subject: JAL-3878 Patch preferences window to use new discoverer. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c8f6d382599ca3429c19ee037dded3187dae6039;p=jalview.git JAL-3878 Patch preferences window to use new discoverer. --- diff --git a/src/jalview/gui/SlivkaPreferences.java b/src/jalview/gui/SlivkaPreferences.java index 89ff3e4..ed1d5f7 100644 --- a/src/jalview/gui/SlivkaPreferences.java +++ b/src/jalview/gui/SlivkaPreferences.java @@ -1,10 +1,5 @@ package jalview.gui; -import jalview.bin.Cache; -import jalview.util.MessageManager; -import jalview.ws.WSDiscovererI; -import jalview.ws.slivkaws.SlivkaWSDiscoverer; - import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; @@ -21,6 +16,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; +import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import javax.swing.BorderFactory; @@ -37,6 +33,11 @@ import javax.swing.UIManager; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; +import jalview.bin.Cache; +import jalview.util.MessageManager; +import jalview.ws2.client.api.WebServiceDiscovererI; +import jalview.ws2.client.slivka.SlivkaWSDiscoverer; + @SuppressWarnings("serial") public class SlivkaPreferences extends JPanel { @@ -45,11 +46,11 @@ public class SlivkaPreferences extends JPanel setPreferredSize(new Dimension(500, 450)); } - WSDiscovererI discoverer; + WebServiceDiscovererI discoverer; - private final ArrayList urls = new ArrayList<>(); + private final ArrayList urls = new ArrayList<>(); - private final Map statuses = new HashMap<>(); + private final Map statuses = new HashMap<>(); private final AbstractTableModel urlTableModel = new AbstractTableModel() { @@ -67,9 +68,9 @@ public class SlivkaPreferences extends JPanel switch (columnIndex) { case 0: - return urls.get(rowIndex); + return urls.get(rowIndex).toString(); case 1: - return statuses.getOrDefault(urls.get(rowIndex), WSDiscovererI.STATUS_UNKNOWN); + return statuses.getOrDefault(urls.get(rowIndex), WebServiceDiscovererI.STATUS_UNKNOWN); default: throw new NoSuchElementException(); } @@ -100,16 +101,16 @@ public class SlivkaPreferences extends JPanel hasFocus, row, column); switch ((Integer) value) { - case WSDiscovererI.STATUS_NO_SERVICES: + case WebServiceDiscovererI.STATUS_NO_SERVICES: setForeground(Color.ORANGE); break; - case WSDiscovererI.STATUS_OK: + case WebServiceDiscovererI.STATUS_OK: setForeground(Color.GREEN); break; - case WSDiscovererI.STATUS_INVALID: + case WebServiceDiscovererI.STATUS_INVALID: setForeground(Color.RED); break; - case WSDiscovererI.STATUS_UNKNOWN: + case WebServiceDiscovererI.STATUS_UNKNOWN: default: setForeground(Color.LIGHT_GRAY); } @@ -140,7 +141,7 @@ public class SlivkaPreferences extends JPanel JButton moveUrlDown = new JButton( MessageManager.getString("action.move_down")); - private String showEditUrlDialog(String oldUrl) + private URL showEditUrlDialog(String oldUrl) { String input = (String) JvOptionPane .showInternalInputDialog( @@ -157,7 +158,7 @@ public class SlivkaPreferences extends JPanel } try { - new URL(input); + return new URL(input); } catch (MalformedURLException ex) { JvOptionPane.showInternalMessageDialog(this, @@ -167,18 +168,17 @@ public class SlivkaPreferences extends JPanel JOptionPane.WARNING_MESSAGE); return null; } - return input; } // Button Action Listeners private ActionListener newUrlAction = (ActionEvent e) -> { - final String input = showEditUrlDialog(""); + final URL input = showEditUrlDialog(""); if (input != null) { urls.add(input); reloadStatusForUrl(input); urlTableModel.fireTableRowsInserted(urls.size(), urls.size()); - discoverer.setServiceUrls(urls); + discoverer.setUrls(urls); } }; @@ -186,14 +186,14 @@ public class SlivkaPreferences extends JPanel final int i = urlListTable.getSelectedRow(); if (i >= 0) { - final String input = showEditUrlDialog(urls.get(i)); + final URL input = showEditUrlDialog(urls.get(i).toString()); if (input != null) { urls.set(i, input); statuses.remove(input); reloadStatusForUrl(input); urlTableModel.fireTableRowsUpdated(i, i); - discoverer.setServiceUrls(urls); + discoverer.setUrls(urls); } } }; @@ -205,7 +205,7 @@ public class SlivkaPreferences extends JPanel urls.remove(i); statuses.remove(i); urlTableModel.fireTableRowsDeleted(i, i); - discoverer.setServiceUrls(urls); + discoverer.setUrls(urls); } }; @@ -214,7 +214,7 @@ public class SlivkaPreferences extends JPanel if (i > 0) { moveTableRow(i, i - 1); - discoverer.setServiceUrls(urls); + discoverer.setUrls(urls); } }; @@ -223,7 +223,7 @@ public class SlivkaPreferences extends JPanel if (i >= 0 && i < urls.size() - 1) { moveTableRow(i, i + 1); - discoverer.setServiceUrls(urls); + discoverer.setUrls(urls); } }; @@ -285,7 +285,7 @@ public class SlivkaPreferences extends JPanel private void moveTableRow(int fromIndex, int toIndex) { - String url = urls.get(fromIndex); + URL url = urls.get(fromIndex); int status = statuses.get(fromIndex); urls.set(fromIndex, urls.get(toIndex)); urls.set(toIndex, url); @@ -311,13 +311,16 @@ public class SlivkaPreferences extends JPanel private ActionListener refreshServicesAction = (ActionEvent e) -> { progressBar.setVisible(true); Cache.log.info("Requesting service reload"); - discoverer.startDiscoverer().handle((_discoverer, exception) -> { + discoverer.startDiscoverer().handle((services, exception) -> { if (exception == null) { Cache.log.info("Reloading done"); } - else + else if (exception instanceof CancellationException) { + Cache.log.info("Reloading cancelled"); + } + else { Cache.log.error("Reloading failed", exception); } SwingUtilities.invokeLater(() -> progressBar.setVisible(false)); @@ -326,11 +329,11 @@ public class SlivkaPreferences extends JPanel }; private ActionListener resetServicesAction = (ActionEvent e) -> { - discoverer.setServiceUrls(null); + discoverer.setUrls(null); urls.clear(); statuses.clear(); - urls.addAll(discoverer.getServiceUrls()); - for (String url : urls) + urls.addAll(discoverer.getUrls()); + for (URL url : urls) { reloadStatusForUrl(url); } @@ -361,16 +364,16 @@ public class SlivkaPreferences extends JPanel { // Initial URLs loading discoverer = SlivkaWSDiscoverer.getInstance(); - urls.addAll(discoverer.getServiceUrls()); - for (String url : urls) + urls.addAll(discoverer.getUrls()); + for (URL url : urls) { reloadStatusForUrl(url); } } - private void reloadStatusForUrl(String url) + private void reloadStatusForUrl(URL url) { - CompletableFuture.supplyAsync(() -> discoverer.getServerStatusFor(url)) + CompletableFuture.supplyAsync(() -> discoverer.getStatusForUrl(url)) .thenAccept((status) -> { statuses.put(url, status); int row = urls.indexOf(url);