Skip to content

Commit

Permalink
CYTOSCAPE-12713 #comment If there is a modal dialog showing, openURL(…
Browse files Browse the repository at this point in the history
…String url) tries to use an external browser first, because CyBrowser would be blocked and maybe hidden.
  • Loading branch information
chrtannus authored and dotasek committed Aug 14, 2020
1 parent 44222fa commit 037045f
Showing 1 changed file with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.cytoscape.util.swing.internal;

import java.awt.Desktop;
import java.awt.Dialog;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -54,7 +57,6 @@ public class OpenBrowserImpl implements OpenBrowser {
private AvailableCommands availableCommands;
private CommandExecutorTaskFactory taskFactory;
private SynchronousTaskManager<?> taskManager;
private Properties props;

private final CyServiceRegistrar serviceRegistrar;

Expand Down Expand Up @@ -111,16 +113,28 @@ public boolean openURL(final String url) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException("URL has an incorrect format: " + url);
}

if (openURLWithCyBrowser(url, new HashMap<>()))
return true;

if (openURLWithDesktop(uri)) {
return true;

if (isDialogBlocking()) {
// Try to open with the default OS browser first,
// because a modal dialog would block the CyBrowser window...
if (openURLWithDefault(url))
return true;

if (openURLWithDesktop(uri))
return true;

if (openURLWithCyBrowser(url, new HashMap<>()))
return true;
} else {
if (openURLWithDefault(url)) {
// No blocking dialog open? Then this is the preferred sequence...
if (openURLWithCyBrowser(url, new HashMap<>()))
return true;

if (openURLWithDesktop(uri))
return true;

if (openURLWithDefault(url))
return true;
}
}

logger.warn("Cytoscape was unable to open your web browser.. "
Expand All @@ -143,15 +157,24 @@ private boolean openURLWithDesktop(final URI uri) {

private boolean openURLWithDefault(final String url) {
// See if the override browser works
String defBrowser = props.getProperty(DEF_WEB_BROWSER_PROP_NAME);

if (defBrowser != null && openURLWithBrowser(url, defBrowser))
return true;
var defBrowser = getProperty(DEF_WEB_BROWSER_PROP_NAME);

for (final String browser : BROWSERS) {
if (openURLWithBrowser(url, browser))
try {
if (defBrowser != null && openURLWithBrowser(url, defBrowser))
return true;
} catch (Exception e) {
// Ignore...
}

for (var browser : BROWSERS) {
try {
if (openURLWithBrowser(url, browser))
return true;
} catch (Exception e) {
// Ignore...
}
}

return false;
}

Expand All @@ -173,12 +196,9 @@ private boolean openURLWithCyBrowser(final String url, Map<String, Object> extra
availableCommands = serviceRegistrar.getService(AvailableCommands.class);
taskFactory = serviceRegistrar.getService(CommandExecutorTaskFactory.class);
taskManager = serviceRegistrar.getService(SynchronousTaskManager.class);
CyProperty<Properties> cyProps = serviceRegistrar.getService(CyProperty.class,
"(cyPropertyName=cytoscape3.props)");
props = cyProps.getProperties();
}

String useCyBrowser = props.getProperty(USE_CYBROWSER);
var useCyBrowser = getProperty(USE_CYBROWSER);

if (useCyBrowser != null && !Boolean.parseBoolean(useCyBrowser))
return false;
Expand All @@ -203,4 +223,32 @@ private boolean openURLWithCyBrowser(final String url, Map<String, Object> extra

return true;
}

private boolean isDialogBlocking() {
boolean blocking = false;
var windows = Window.getWindows();

for (var w : windows) {
if (!w.isShowing())
continue;

if (w instanceof Dialog) {
var modalityType = ((Dialog) w).getModalityType();

if (modalityType == ModalityType.APPLICATION_MODAL || modalityType == ModalityType.TOOLKIT_MODAL) {
blocking = true;
break;
}
}
}

return blocking;
}

private String getProperty(String key) {
var cyProps = serviceRegistrar.getService(CyProperty.class, "(cyPropertyName=cytoscape3.props)");
var props = (Properties) cyProps.getProperties();

return props.getProperty(key);
}
}

0 comments on commit 037045f

Please sign in to comment.