Skip to content

Commit 5d525b5

Browse files
committed
Notify the user when no LinkHandler supports a URI
1 parent 51c03f2 commit 5d525b5

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎src/main/java/org/scijava/desktop/links/DefaultLinkService.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.scijava.desktop.links;
3030

3131
import java.awt.Desktop;
32+
import java.awt.GraphicsEnvironment;
3233
import java.net.URI;
3334
import java.util.Arrays;
3435
import java.util.Collections;
@@ -46,6 +47,8 @@
4647
import org.scijava.plugin.Parameter;
4748
import org.scijava.plugin.Plugin;
4849
import org.scijava.service.Service;
50+
import org.scijava.ui.DialogPrompt;
51+
import org.scijava.ui.UIService;
4952

5053
/**
5154
* Default implementation of {@link LinkService}.
@@ -61,6 +64,12 @@ public class DefaultLinkService extends AbstractHandlerService<URI, LinkHandler>
6164
@Parameter(required = false)
6265
private PlatformService platformService;
6366

67+
@Parameter(required = false)
68+
private UIService uiService;
69+
70+
/** Maximum number of characters of an unsupported URI shown to the user. */
71+
private static final int MAX_URI_DISPLAY_LENGTH = 120;
72+
6473
@EventHandler
6574
private void onEvent(final ContextCreatedEvent evt) {
6675
// Register URI handler with the desktop system, if possible.
@@ -75,6 +84,26 @@ private void onEvent(final ContextCreatedEvent evt) {
7584
installSchemes();
7685
}
7786

87+
@Override
88+
public void handleUnsupported(final URI uri) {
89+
final String appName = System.getProperty("scijava.app.name", "This application");
90+
final String appDir = System.getProperty("scijava.app.directory");
91+
String shown = String.valueOf(uri);
92+
if (shown.length() > MAX_URI_DISPLAY_LENGTH) {
93+
shown = shown.substring(0, MAX_URI_DISPLAY_LENGTH) + "...";
94+
}
95+
final String msg = appName + " cannot handle this link:\n\n" + shown +
96+
"\n\nThe component that supports it may be missing or outdated. " +
97+
"Please make sure it is installed and up to date." +
98+
(appDir == null ? "" : "\n\nIf several installations exist in your system, " +
99+
"the one that received this link may not include the component it " +
100+
"needs. This link has been opened by:\n" + appDir);
101+
if (log != null) log.warn(msg.replace("\n\n", " ").replace("\n", " "));
102+
if (uiService != null && !GraphicsEnvironment.isHeadless()) {
103+
uiService.showDialog(msg, "Unsupported Link", DialogPrompt.MessageType.ERROR_MESSAGE);
104+
}
105+
}
106+
78107
/**
79108
* Installs URI schemes with the operating system.
80109
* <p>

‎src/main/java/org/scijava/desktop/links/LinkService.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,30 @@ default void handle(final URI uri) {
5454
.findFirst();
5555
if (match.isEmpty()) {
5656
// No appropriate link handler plugin was found.
57-
final Logger log = log();
58-
if (log != null) log.debug("No handler for URI: " + uri);
57+
handleUnsupported(uri);
5958
return; // no handler for this URI
6059
}
6160
// Handle the URI using the matching link handler.
6261
match.get().handle(uri);
6362
}
6463

64+
/**
65+
* Called by {@link #handle(URI)} when no {@link LinkHandler} supports the
66+
* given URI. This typically happens when a link is opened by an installation
67+
* that lacks (or has an outdated version of) the component that implements
68+
* it, since the operating system routes links by scheme only.
69+
* <p>
70+
* The default implementation only logs a debug message. Implementations
71+
* with access to a user interface should override it to notify the user.
72+
* </p>
73+
*
74+
* @param uri The URI that no handler supports.
75+
*/
76+
default void handleUnsupported(final URI uri) {
77+
final Logger log = log();
78+
if (log != null) log.debug("No handler for URI: " + uri);
79+
}
80+
6581
default Set<String> getSchemes() {
6682
var schemes = new HashSet<String>();
6783
for (final LinkHandler handler : getInstances()) {

0 commit comments

Comments
 (0)