From 92675056ba5450ef88659fd8a34b111edec547ab Mon Sep 17 00:00:00 2001 From: Leo Leplat <60394504+Rylern@users.noreply.github.com> Date: Thu, 15 Feb 2024 10:08:35 +0000 Subject: [PATCH] Prompt user to skip authentication when opening browser --- .../browser/serverbrowser/BrowserCommand.java | 82 +++++++++++++------ .../NewConnectionOptions.java | 34 ++++++++ .../newconnectionoptions/package-info.java | 5 ++ .../new_connection_options.fxml | 13 +++ .../qupath/ext/omero/strings.properties | 3 + .../qupath/ext/omero/strings_fr.properties | 3 + 6 files changed, 113 insertions(+), 27 deletions(-) create mode 100644 src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/NewConnectionOptions.java create mode 100644 src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/package-info.java create mode 100644 src/main/resources/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/new_connection_options.fxml diff --git a/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/BrowserCommand.java b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/BrowserCommand.java index 1161a0c..8e7a83c 100644 --- a/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/BrowserCommand.java +++ b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/BrowserCommand.java @@ -7,6 +7,7 @@ import qupath.ext.omero.core.WebClient; import qupath.ext.omero.core.WebClients; import qupath.ext.omero.gui.UiUtilities; +import qupath.ext.omero.gui.browser.serverbrowser.newconnectionoptions.NewConnectionOptions; import qupath.fx.dialogs.Dialogs; import java.io.IOException; @@ -54,36 +55,63 @@ public BrowserCommand(URI uri) { @Override public void run() { if (browser == null) { - WebClients.createClient(uri.toString(), true).thenAccept(client -> Platform.runLater(() -> { - if (client.getStatus().equals(WebClient.Status.SUCCESS)) { - try { - browser = new Browser(client); - this.client = client; - } catch (IOException e) { - logger.error("Error while creating the browser", e); - } - } else if (client.getStatus().equals(WebClient.Status.FAILED)) { - Optional failReason = client.getFailReason(); - String message = null; + Optional existingClient = WebClients.getClients().stream() + .filter(client -> client.getApisHandler().getWebServerURI().equals(uri)) + .findAny(); - if (failReason.isPresent()) { - if (failReason.get().equals(WebClient.FailReason.INVALID_URI_FORMAT)) { - message = MessageFormat.format(resources.getString("Browser.BrowserCommand.invalidURI"), uri.toString()); - } else if (failReason.get().equals(WebClient.FailReason.ALREADY_CREATING)) { - message = MessageFormat.format(resources.getString("Browser.BrowserCommand.alreadyCreating"), uri.toString()); - } - } else { - message = MessageFormat.format(resources.getString("Browser.BrowserCommand.connectionFailed"), uri.toString()); - } + if (existingClient.isPresent()) { + try { + browser = new Browser(existingClient.get()); + this.client = existingClient.get(); + } catch (IOException e) { + logger.error("Error while creating the browser", e); + } + } else { + NewConnectionOptions newConnectionOptions = null; + try { + newConnectionOptions = new NewConnectionOptions(); + } catch (IOException e) { + logger.error("Error when creating the new connection options form", e); + } - if (message != null) { - Dialogs.showErrorMessage( - resources.getString("Browser.BrowserCommand.webServer"), - message - ); - } + boolean dialogConfirmed = newConnectionOptions != null && Dialogs.showConfirmDialog( + resources.getString("Browser.NewConnectionOptions.title"), + newConnectionOptions + ); + + if (dialogConfirmed) { + WebClients.createClient(uri.toString(), newConnectionOptions.canSkipAuthentication()).thenAccept(client -> Platform.runLater(() -> { + if (client.getStatus().equals(WebClient.Status.SUCCESS)) { + try { + browser = new Browser(client); + this.client = client; + } catch (IOException e) { + logger.error("Error while creating the browser", e); + } + } else if (client.getStatus().equals(WebClient.Status.FAILED)) { + Optional failReason = client.getFailReason(); + String message = null; + + if (failReason.isPresent()) { + if (failReason.get().equals(WebClient.FailReason.INVALID_URI_FORMAT)) { + message = MessageFormat.format(resources.getString("Browser.BrowserCommand.invalidURI"), uri.toString()); + } else if (failReason.get().equals(WebClient.FailReason.ALREADY_CREATING)) { + message = MessageFormat.format(resources.getString("Browser.BrowserCommand.alreadyCreating"), uri.toString()); + } + } else { + message = MessageFormat.format(resources.getString("Browser.BrowserCommand.connectionFailed"), uri.toString()); + } + + if (message != null) { + Dialogs.showErrorMessage( + resources.getString("Browser.BrowserCommand.webServer"), + message + ); + } + } + })); } - })); + } } else { UiUtilities.showWindow(browser); } diff --git a/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/NewConnectionOptions.java b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/NewConnectionOptions.java new file mode 100644 index 0000000..3d2d1f6 --- /dev/null +++ b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/NewConnectionOptions.java @@ -0,0 +1,34 @@ +package qupath.ext.omero.gui.browser.serverbrowser.newconnectionoptions; + +import javafx.fxml.FXML; +import javafx.scene.control.CheckBox; +import javafx.scene.layout.VBox; +import qupath.ext.omero.gui.UiUtilities; + +import java.io.IOException; + +/** + * A form to prompt the user for parameters before attempting to create a connection to a server. + */ +public class NewConnectionOptions extends VBox { + + @FXML + private CheckBox skipAuthentication; + + /** + * Creates the new connection options form. + * + * @throws IOException if an error occurs while creating the form + */ + public NewConnectionOptions() throws IOException { + UiUtilities.loadFXML(this, NewConnectionOptions.class.getResource("new_connection_options.fxml")); + } + + /** + * @return whether to try skipping authentication when connecting to the server + */ + public boolean canSkipAuthentication() { + return skipAuthentication.isSelected(); + } +} + diff --git a/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/package-info.java b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/package-info.java new file mode 100644 index 0000000..2271ce0 --- /dev/null +++ b/src/main/java/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/package-info.java @@ -0,0 +1,5 @@ +/** + *

This package provides a form to prompt the user for parameters before attempting to create a connection to a server.

+ *

This is handled by {@link qupath.ext.omero.gui.browser.serverbrowser.newconnectionoptions.NewConnectionOptions NewConnectionOptions}.

+ */ +package qupath.ext.omero.gui.browser.serverbrowser.newconnectionoptions; \ No newline at end of file diff --git a/src/main/resources/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/new_connection_options.fxml b/src/main/resources/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/new_connection_options.fxml new file mode 100644 index 0000000..c9af6b3 --- /dev/null +++ b/src/main/resources/qupath/ext/omero/gui/browser/serverbrowser/newconnectionoptions/new_connection_options.fxml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/src/main/resources/qupath/ext/omero/strings.properties b/src/main/resources/qupath/ext/omero/strings.properties index 8b1d972..59a5da1 100644 --- a/src/main/resources/qupath/ext/omero/strings.properties +++ b/src/main/resources/qupath/ext/omero/strings.properties @@ -105,6 +105,9 @@ Browser.BrowserCommand.alreadyCreating = A connection to {0} is already being es Browser.BrowserCommand.connectionFailed = Connection to {0} failed.\n\nCheck the following:\n- Valid credentials.\n- Access permission.\n- Correct URL. Browser.BrowserCommand.webServer = Web server +Browser.NewConnectionOptions.title = New connection +Browser.NewConnectionOptions.skipAuthentication = Enable logging in as a public user + Browser.ServerBrowser.omeroWebServer = OMERO web server Browser.ServerBrowser.server = Server: Browser.ServerBrowser.username = Username: diff --git a/src/main/resources/qupath/ext/omero/strings_fr.properties b/src/main/resources/qupath/ext/omero/strings_fr.properties index 06d1fcd..377f278 100644 --- a/src/main/resources/qupath/ext/omero/strings_fr.properties +++ b/src/main/resources/qupath/ext/omero/strings_fr.properties @@ -105,6 +105,9 @@ Browser.BrowserCommand.alreadyCreating = Une connection vers {0} est déjà en t Browser.BrowserCommand.connectionFailed = Impossible de se connecter à {0}.\n\nVérifiez les informations suivantes:\n- Identifiants valides.\n- Droits d'accès.\n- URL correcte. Browser.BrowserCommand.webServer = Serveur web +Browser.NewConnectionOptions.title = Nouvelle connection +Browser.NewConnectionOptions.skipAuthentication = Est-ce qu'il faut passer l'authentification si le serveur le permet + Browser.ServerBrowser.omeroWebServer = Serveur web OMERO Browser.ServerBrowser.server = Serveur: Browser.ServerBrowser.username = Nom d'utilisateur: