From 99fc3b46b7d61c530c84e425e8c044caba738618 Mon Sep 17 00:00:00 2001 From: Pete Date: Fri, 17 Nov 2023 14:33:11 +0000 Subject: [PATCH 1/2] Externalize more strings --- src/main/java/qupath/ext/djl/DjlTools.java | 17 +------ .../qupath/ext/djl/ui/DjlEngineCommand.java | 47 ++++++++++--------- .../qupath/ext/djl/ui/strings.properties | 10 +++- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/main/java/qupath/ext/djl/DjlTools.java b/src/main/java/qupath/ext/djl/DjlTools.java index 6b438e9..21251fc 100644 --- a/src/main/java/qupath/ext/djl/DjlTools.java +++ b/src/main/java/qupath/ext/djl/DjlTools.java @@ -252,32 +252,19 @@ public static Engine getEngine(String name, boolean downloadIfNeeded) throws Ill else System.setProperty("offline", "true"); - // If trying to instantiate a TensorFlow engine for the first time, - // we need to reset the javacpp platform properties for the preload - // path to be correctly identified - if (ENGINE_TENSORFLOW.equalsIgnoreCase(name) && !loadedEngines.contains(name)) { - try { - var f = Loader.class.getDeclaredField("platformProperties"); - f.setAccessible(true); - f.set(null, null); - } catch (Exception e) { - logger.error("Unable to reset JavaCPP platform properties: " + e.getLocalizedMessage(), e); - } - } - var engine = Engine.getEngine(name); if (engine != null) loadedEngines.add(name); return engine; } catch (Exception e) { if (downloadIfNeeded) - logger.error("Unable to get engine " + name + ": " + e.getLocalizedMessage(), e); + logger.error("Unable to get engine " + name + ": " + e.getMessage(), e); else { var msg = e.getLocalizedMessage(); if (msg == null) logger.warn("Unable to get engine {}", name); else - logger.warn("Unable to get engine {} ({})", name, e.getLocalizedMessage()); + logger.warn("Unable to get engine {} ({})", name, e.getMessage()); } return null; } finally { diff --git a/src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java b/src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java index 0ab910d..dc288da 100644 --- a/src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java +++ b/src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java @@ -29,6 +29,7 @@ import javafx.geometry.Pos; import javafx.scene.Cursor; import javafx.scene.Node; +import javafx.scene.layout.StackPane; import javafx.scene.text.TextAlignment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +55,6 @@ import javafx.scene.control.Tooltip; import javafx.scene.layout.GridPane; import javafx.scene.shape.Circle; -import javafx.stage.Modality; import javafx.stage.Stage; import qupath.ext.djl.DjlTools; import qupath.fx.dialogs.Dialogs; @@ -144,7 +144,9 @@ private void init() { var status = available.computeIfAbsent(name, n -> new SimpleObjectProperty<>(EngineStatus.UNKNOWN)); - var path = Utils.getEngineCacheDir(name); + // Names seem to be lower case in practice + // This matters on Linux, since otherwise the directory can't be found/opened + var path = Utils.getEngineCacheDir(name.toLowerCase()); String tooltip = String.format(bundle.getString("tooltip.engine"), name); String labelNameText = name; @@ -183,17 +185,17 @@ private void init() { var tooltipDownload = new Tooltip(); tooltipDownload.textProperty().bind(Bindings.createStringBinding(() -> { switch (status.get()) { - case AVAILABLE: - return name + " is available"; - case UNAVAILABLE: - return name + " is not available - click to download it"; - case FAILED: - return name + " download & initialization failed - you can try again, but it may not be supported on this platform"; - case PENDING: - return name + " download pending"; - case UNKNOWN: - default: - return name + " download status is unknown"; + case AVAILABLE: + return String.format(bundle.getString("tooltip.download.available"), name); + case FAILED: + return String.format(bundle.getString("tooltip.download.failed"), name); + case PENDING: + return String.format(bundle.getString("tooltip.download.pending"), name); + case UNAVAILABLE: + return String.format(bundle.getString("tooltip.download.unavailable"), name); + case UNKNOWN: + default: + return String.format(bundle.getString("tooltip.download.unknown"), name); } }, status)); btnDownload.setTooltip(tooltipDownload); @@ -216,7 +218,7 @@ private void init() { logger.debug("Cannot open {} - directory does not exist", path); }); - var labelVersion = new Label(""); + var labelVersion = new Label(bundle.getString("label.version.unknown")); labelVersion.setMaxWidth(Double.MAX_VALUE); var labelVersionLabel = createLabelForKey("label.version"); @@ -234,7 +236,9 @@ private void init() { updateVersionFromStatus(n, name, labelVersion); }); - pane.add(circle, 0, row, 1, 2); + var circlePane = new StackPane(circle); + circlePane.setPadding(new Insets(5)); + pane.add(circlePane, 0, row, 1, 2); GridPaneUtils.addGridRow(pane, row++, 1, null, labelPathLabel, labelPath); GridPaneUtils.addGridRow(pane, row++, 1, null, labelVersionLabel, labelVersion); @@ -243,13 +247,13 @@ private void init() { GridPaneUtils.setToExpandGridPaneWidth(labelName, labelPath, btnDownload); // Update the engine status quietly - checkEngineStatus(name, status, -1, true); +// checkEngineStatus(name, status, -1, true); } stage = new Stage(); stage.setTitle(TITLE); stage.initOwner(QuPathGUI.getInstance().getStage()); - stage.initModality(Modality.APPLICATION_MODAL); +// stage.initModality(Modality.WINDOW_MODAL); stage.setScene(new Scene(pane)); } @@ -270,7 +274,7 @@ private static void updateVersionFromStatus(EngineStatus status, String engineNa logger.error("Error updating engine version: {}", e.getMessage(), e); } } - labelVersion.setText(""); + labelVersion.setText(bundle.getString("label.version.unknown")); labelVersion.setTooltip(null); } @@ -342,8 +346,7 @@ private void checkEngineStatus(String name, ObjectProperty status, // Wait until the timeout - engine might already be available & return quickly var result = future.get(timeoutMillis, TimeUnit.MILLISECONDS); if (result != null && result.booleanValue()) { - Dialogs.showInfoNotification(TITLE, name + " is available!"); - return; + Dialogs.showInfoNotification(TITLE, String.format(bundle.getString("notify.engine.available"), name)); } else logger.debug("No engine available for {}", name); } catch (InterruptedException e) { @@ -368,7 +371,7 @@ private Boolean checkEngineAvailability(String name, ObjectProperty Date: Thu, 30 Nov 2023 09:44:27 +0000 Subject: [PATCH 2/2] Update build.gradle --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 20b451f..8c159d7 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { implementation "ai.djl:api:$djlVersion" testImplementation libs.junit - testRuntimeOnly libs.junit.engine + testRuntimeOnly libs.junit.platform }