Skip to content

Commit 58b6c02

Browse files
authored
Merge pull request #47 from petebankhead/cleanup
Minor cleanup, make easier to close window
2 parents cce175d + 7108a55 commit 58b6c02

3 files changed

Lines changed: 40 additions & 55 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55

66
qupathExtension {
77
name = "qupath-extension-djl"
8-
version = "0.4.2"
8+
version = "0.4.3"
99
group = "io.github.qupath"
1010
description = "QuPath extension to use Deep Java Library"
1111
automaticModule = "qupath.extension.djl"

src/main/java/qupath/ext/djl/DjlDnnModelBuilder.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*-
2-
* Copyright 2022 QuPath developers, University of Edinburgh
2+
* Copyright 2022 - 2026 QuPath developers, University of Edinburgh
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,21 +37,15 @@ public class DjlDnnModelBuilder implements DnnModelBuilder {
3737
private static String getEngineName(String framework) {
3838
if (DjlTools.ALL_ENGINES.contains(framework))
3939
return framework;
40-
41-
switch(framework) {
42-
case DnnModelParams.FRAMEWORK_TENSORFLOW:
43-
return DjlTools.ENGINE_TENSORFLOW;
44-
case DnnModelParams.FRAMEWORK_TF_LITE:
45-
return DjlTools.ENGINE_TFLITE;
46-
case DnnModelParams.FRAMEWORK_ONNX_RUNTIME:
47-
return DjlTools.ENGINE_ONNX_RUNTIME;
48-
case DnnModelParams.FRAMEWORK_PYTORCH:
49-
return DjlTools.ENGINE_PYTORCH;
50-
case DnnModelParams.FRAMEWORK_MXNET:
51-
return DjlTools.ENGINE_MXNET;
52-
default:
53-
return null;
54-
}
40+
41+
return switch (framework) {
42+
case DnnModelParams.FRAMEWORK_TENSORFLOW -> DjlTools.ENGINE_TENSORFLOW;
43+
case DnnModelParams.FRAMEWORK_TF_LITE -> DjlTools.ENGINE_TFLITE;
44+
case DnnModelParams.FRAMEWORK_ONNX_RUNTIME -> DjlTools.ENGINE_ONNX_RUNTIME;
45+
case DnnModelParams.FRAMEWORK_PYTORCH -> DjlTools.ENGINE_PYTORCH;
46+
case DnnModelParams.FRAMEWORK_MXNET -> DjlTools.ENGINE_MXNET;
47+
default -> null;
48+
};
5549
}
5650

5751

src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*-
2-
* Copyright 2022-2023 QuPath developers, University of Edinburgh
2+
* Copyright 2022-2026 QuPath developers, University of Edinburgh
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@
1919
import java.nio.file.Files;
2020
import java.util.ResourceBundle;
2121
import java.util.Set;
22-
import java.util.concurrent.Callable;
2322
import java.util.concurrent.ExecutionException;
2423
import java.util.concurrent.Executors;
24+
import java.util.concurrent.Future;
2525
import java.util.concurrent.TimeUnit;
2626
import java.util.concurrent.TimeoutException;
2727

@@ -58,6 +58,7 @@
5858
import javafx.stage.Stage;
5959
import qupath.ext.djl.DjlTools;
6060
import qupath.fx.dialogs.Dialogs;
61+
import qupath.fx.utils.FXUtils;
6162
import qupath.fx.utils.GridPaneUtils;
6263
import qupath.lib.common.GeneralTools;
6364
import qupath.lib.common.ThreadTools;
@@ -166,36 +167,24 @@ private void init() {
166167
status.get() == EngineStatus.AVAILABLE || status.get() == EngineStatus.PENDING,
167168
status));
168169
btnDownload.textProperty().bind(Bindings.createStringBinding(() -> {
169-
switch (status.get()) {
170-
case AVAILABLE:
171-
return bundle.getString("button.download.available");
172-
case FAILED:
173-
return bundle.getString("button.download.failed");
174-
case PENDING:
175-
return bundle.getString("button.download.pending");
176-
case UNAVAILABLE:
177-
return bundle.getString("button.download.unavailable");
178-
case UNKNOWN:
179-
default:
180-
return bundle.getString("button.download.unknown");
181-
}
170+
return switch (status.get()) {
171+
case AVAILABLE -> bundle.getString("button.download.available");
172+
case FAILED -> bundle.getString("button.download.failed");
173+
case PENDING -> bundle.getString("button.download.pending");
174+
case UNAVAILABLE -> bundle.getString("button.download.unavailable");
175+
default -> bundle.getString("button.download.unknown");
176+
};
182177
}, status));
183178

184179
var tooltipDownload = new Tooltip();
185180
tooltipDownload.textProperty().bind(Bindings.createStringBinding(() -> {
186-
switch (status.get()) {
187-
case AVAILABLE:
188-
return String.format(bundle.getString("tooltip.download.available"), name);
189-
case FAILED:
190-
return String.format(bundle.getString("tooltip.download.failed"), name);
191-
case PENDING:
192-
return String.format(bundle.getString("tooltip.download.pending"), name);
193-
case UNAVAILABLE:
194-
return String.format(bundle.getString("tooltip.download.unavailable"), name);
195-
case UNKNOWN:
196-
default:
197-
return String.format(bundle.getString("tooltip.download.unknown"), name);
198-
}
181+
return switch (status.get()) {
182+
case AVAILABLE -> String.format(bundle.getString("tooltip.download.available"), name);
183+
case FAILED -> String.format(bundle.getString("tooltip.download.failed"), name);
184+
case PENDING -> String.format(bundle.getString("tooltip.download.pending"), name);
185+
case UNAVAILABLE -> String.format(bundle.getString("tooltip.download.unavailable"), name);
186+
default -> String.format(bundle.getString("tooltip.download.unknown"), name);
187+
};
199188
}, status));
200189
btnDownload.setTooltip(tooltipDownload);
201190

@@ -254,12 +243,13 @@ private void init() {
254243
stage.initOwner(QuPathGUI.getInstance().getStage());
255244
// stage.initModality(Modality.WINDOW_MODAL);
256245
stage.setScene(new Scene(pane));
246+
FXUtils.addCloseWindowShortcuts(stage);
257247
}
258248

259249
private static void updateIconFromStatus(EngineStatus status, Node node) {
260250
if (status == null)
261251
status = EngineStatus.UNKNOWN;
262-
node.getStyleClass().setAll("djl-engine-status", status.name().toString().toLowerCase());
252+
node.getStyleClass().setAll("djl-engine-status", status.name().toLowerCase());
263253
}
264254

265255
private static void updateVersionFromStatus(EngineStatus status, String engineName, Label labelVersion) {
@@ -336,23 +326,24 @@ private static String getCudaString() {
336326

337327
private void checkEngineStatus(String name, ObjectProperty<EngineStatus> status, long timeoutMillis, boolean doQuietly) {
338328
// Request the engine in a background thread, triggering download if necessary
339-
var pool = Executors.newSingleThreadExecutor(ThreadTools.createThreadFactory("djl-engine-request", true));
340-
updateStatus(status, EngineStatus.PENDING);
341-
var future = pool.submit((Callable<Boolean>)() -> checkEngineAvailability(name, status, doQuietly));
342-
if (timeoutMillis <= 0)
343-
return;
344-
pool.shutdown();
329+
Future<Boolean> future;
330+
try (var pool = Executors.newSingleThreadExecutor(ThreadTools.createThreadFactory("djl-engine-request", true))) {
331+
updateStatus(status, EngineStatus.PENDING);
332+
future = pool.submit(() -> checkEngineAvailability(name, status, doQuietly));
333+
if (timeoutMillis <= 0)
334+
return;
335+
}
345336
try {
346337
// Wait until the timeout - engine might already be available & return quickly
347338
var result = future.get(timeoutMillis, TimeUnit.MILLISECONDS);
348-
if (result != null && result.booleanValue()) {
339+
if (result != null && result) {
349340
Dialogs.showInfoNotification(TITLE, String.format(bundle.getString("notify.engine.available"), name));
350341
} else
351342
logger.debug("No engine available for {}", name);
352343
} catch (InterruptedException e) {
353-
logger.error("Requesting for engine " + name + "interrupted!", e);
344+
logger.error("Request for engine {} interrupted!", name, e);
354345
} catch (ExecutionException e) {
355-
logger.error("Error requesting engine " + name + ": " + e.getLocalizedMessage(), e);
346+
logger.error("Error requesting engine {}: {}", name, e.getMessage(), e);
356347
} catch (TimeoutException e) {
357348
logger.debug("Request for engine {} timed out - likely either initializing or downloading", name);
358349
}

0 commit comments

Comments
 (0)