Skip to content

Commit

Permalink
Ensure download and info button disabled correspond to current model
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan committed Dec 10, 2024
1 parent 86c4892 commit 8561c22
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/main/java/qupath/ext/wsinfer/ui/WSInferController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
Expand Down Expand Up @@ -56,7 +58,6 @@
import qupath.ext.wsinfer.WSInfer;
import qupath.ext.wsinfer.models.WSInferModel;
import qupath.ext.wsinfer.models.WSInferModelCollection;
import qupath.ext.wsinfer.models.WSInferModelLocal;
import qupath.ext.wsinfer.models.WSInferUtils;
import qupath.fx.dialogs.Dialogs;
import qupath.fx.dialogs.FileChoosers;
Expand Down Expand Up @@ -85,7 +86,6 @@
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;


/**
Expand Down Expand Up @@ -141,6 +141,7 @@ public class WSInferController {
private final ExecutorService pool = Executors.newSingleThreadExecutor(ThreadTools.createThreadFactory("wsinfer", true));

private final ObjectProperty<Task<?>> pendingTask = new SimpleObjectProperty<>();
private final BooleanProperty downloadPending = new SimpleBooleanProperty(false);

@FXML
private void initialize() {
Expand All @@ -161,7 +162,7 @@ private void initialize() {
configureBatchSize();

configureMessageLabel();
configureRunInferenceButton();
configureButtons();

configurePendingTaskProperty();
}
Expand Down Expand Up @@ -191,8 +192,6 @@ void refreshAvailableModels() {
modelChoiceBox.setConverter(new ModelStringConverter(models));
modelChoiceBox.getSelectionModel().selectedItemProperty().addListener(
(v, o, n) -> {
downloadButton.setDisable((n == null) || n.isValid());
infoButton.setDisable((n == null) || (!n.isValid()) || !checkFileExists(n.getReadMeFile()));
infoPopover.hide();
});
}
Expand All @@ -216,14 +215,29 @@ private void configureAvailableDevices() {
(value, oldValue, newValue) -> WSInferPrefs.deviceProperty().set(newValue));
}

private void configureRunInferenceButton() {
private void configureButtons() {
// Disable the run button while a task is pending, or we have no model selected, or download is required
runButton.disableProperty().bind(
imageDataProperty.isNull()
.or(pendingTask.isNotNull())
.or(modelChoiceBox.getSelectionModel().selectedItemProperty().isNull())
.or(messageTextHelper.warningText.isNotEmpty())
);
downloadButton.disableProperty().bind(
Bindings.createBooleanBinding(
() -> modelChoiceBox.getSelectionModel().getSelectedItem() == null || modelChoiceBox.getSelectionModel().getSelectedItem().isValid(),
modelChoiceBox.getSelectionModel().selectedItemProperty(),
downloadPending)
);
infoButton.disableProperty().bind(
Bindings.createBooleanBinding(
() -> {
var n = modelChoiceBox.getSelectionModel().getSelectedItem();
return n == null || !n.isValid() || !checkFileExists(n.getReadMeFile());
},
modelChoiceBox.getSelectionModel().selectedItemProperty(),
downloadPending)
);
}

private void configurePendingTaskProperty() {
Expand Down Expand Up @@ -357,7 +371,7 @@ public void downloadModel() {
return;
}

ForkJoinPool.commonPool().execute(() -> {
Thread.ofVirtual().start(() -> {
model.removeCache();
showDownloadingModelNotification(model.getName());
try {
Expand All @@ -367,9 +381,9 @@ public void downloadModel() {
return;
}
showModelAvailableNotification(model.getName());
downloadButton.setDisable(true);
infoButton.setDisable(model instanceof WSInferModelLocal);
downloadPending.set(false);
});
downloadPending.set(true);
}

/**
Expand Down

0 comments on commit 8561c22

Please sign in to comment.