use of qupath.lib.gui.extensions.QuPathExtension in project qupath by qupath.
the class QuPathGUI method refreshExtensions.
/**
* Check the extensions directory, loading any new extensions found there.
* @param showNotification if true, display a notification if a new extension has been loaded
*/
public void refreshExtensions(final boolean showNotification) {
boolean initializing = initializingMenus.get();
initializingMenus.set(true);
// Refresh the extensions
extensionClassLoader.refresh();
extensionLoader.reload();
// Sort the extensions by name, to ensure predictable loading order
// (also, menus are in a better order if ImageJ extension installed before OpenCV extension)
List<QuPathExtension> extensions = new ArrayList<>();
Iterator<QuPathExtension> iterator = extensionLoader.iterator();
while (iterator.hasNext()) {
try {
extensions.add(iterator.next());
} catch (Throwable e) {
if (getStage() != null && getStage().isShowing()) {
Dialogs.showErrorMessage("Extension error", "Error loading extension - check 'View -> Show log' for details.");
}
logger.error(e.getLocalizedMessage(), e);
}
}
Collections.sort(extensions, Comparator.comparing(QuPathExtension::getName));
Version qupathVersion = getVersion();
for (QuPathExtension extension : extensions) {
if (!loadedExtensions.containsKey(extension.getClass())) {
Version version = extension.getVersion();
try {
long startTime = System.currentTimeMillis();
extension.installExtension(this);
long endTime = System.currentTimeMillis();
logger.info("Loaded extension {} ({} ms)", extension.getName(), endTime - startTime);
if (version != null)
logger.debug("{} was written for QuPath {}", extension.getName(), version);
else
logger.debug("{} does not report a compatible QuPath version", extension.getName());
loadedExtensions.put(extension.getClass(), extension);
if (showNotification)
Dialogs.showInfoNotification("Extension loaded", extension.getName());
} catch (Exception | LinkageError e) {
String message = "Unable to load " + extension.getName();
if (showNotification)
Dialogs.showErrorNotification("Extension error", message);
logger.error("Error loading extension " + extension + ": " + e.getLocalizedMessage(), e);
if (!Objects.equals(qupathVersion, version)) {
if (version == null)
logger.warn("QuPath version for which the '{}' was written is unknown!", extension.getName());
else if (version.equals(qupathVersion))
logger.warn("'{}' reports that it is compatible with the current QuPath version {}", extension.getName(), qupathVersion);
else
logger.warn("'{}' was written for QuPath {} but current version is {}", extension.getName(), version, qupathVersion);
}
try {
logger.error("It is recommended that you delete {} and restart QuPath", URLDecoder.decode(extension.getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm(), StandardCharsets.UTF_8));
} catch (Exception e2) {
logger.debug("Error finding code source " + e2.getLocalizedMessage(), e2);
}
defaultActions.SHOW_LOG.handle(null);
}
}
}
// Set the ImageServer to also look on the same search path
List<ImageServerBuilder<?>> serverBuildersBefore = ImageServerProvider.getInstalledImageServerBuilders();
ImageServerProvider.setServiceLoader(ServiceLoader.load(ImageServerBuilder.class, extensionClassLoader));
if (showNotification) {
// A bit convoluted... but try to show new servers that have been loaded by comparing with the past
List<String> serverBuilders = serverBuildersBefore.stream().map(s -> s.getName()).collect(Collectors.toList());
List<String> serverBuildersUpdated = ImageServerProvider.getInstalledImageServerBuilders().stream().map(s -> s.getName()).collect(Collectors.toList());
serverBuildersUpdated.removeAll(serverBuilders);
for (String builderName : serverBuildersUpdated) {
Dialogs.showInfoNotification("Image server loaded", builderName);
}
}
initializingMenus.set(initializing);
}
use of qupath.lib.gui.extensions.QuPathExtension in project qupath by qupath.
the class ShowInstalledExtensionsCommand method showInstalledExtensions.
public static void showInstalledExtensions(final QuPathGUI qupath) {
Stage dialog = new Stage();
dialog.initOwner(qupath.getStage());
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.setTitle("Installed extensions");
GridPane paneExtensions = new GridPane();
paneExtensions.setPadding(new Insets(10, 10, 10, 10));
paneExtensions.setHgap(5);
paneExtensions.setVgap(10);
int row = 0;
int inc = 1;
for (QuPathExtension extension : qupath.getLoadedExtensions()) {
addEntry(paneExtensions, new QuPathExtensionEntry(extension), row);
row += inc;
}
GridPane paneServers = new GridPane();
paneServers.setPadding(new Insets(10, 10, 10, 10));
paneServers.setHgap(5);
paneServers.setVgap(10);
row = 0;
for (ImageServerBuilder<?> builder : ImageServerProvider.getInstalledImageServerBuilders()) {
addEntry(paneServers, new QuPathExtensionEntry(builder), row);
row += inc;
}
TitledPane titledExtensions = new TitledPane("Extensions", paneExtensions);
PaneTools.simplifyTitledPane(titledExtensions, false);
TitledPane titledServers = new TitledPane("Image Servers", paneServers);
PaneTools.simplifyTitledPane(titledServers, false);
VBox vbox = new VBox(titledExtensions, titledServers);
var dir = QuPathGUI.getExtensionDirectory();
if (dir != null) {
var btnOpen = new Button("Open extensions directory");
btnOpen.setOnAction(e -> GuiTools.browseDirectory(dir));
btnOpen.setMaxWidth(Double.MAX_VALUE);
vbox.getChildren().add(btnOpen);
// } else {
// var label = new Label("No user directory has been set for custom user extensions.\n"
// + "Drag an extensions jar file onto QuPath to set a user directory and install the extension.");
// label.setTextAlignment(TextAlignment.CENTER);
// label.setMaxWidth(Double.MAX_VALUE);
// vbox.getChildren().add(label);
}
vbox.setPadding(new Insets(5));
vbox.setMaxWidth(Double.POSITIVE_INFINITY);
dialog.setScene(new Scene(new ScrollPane(vbox)));
dialog.show();
}
Aggregations