From 69f0cc23203b63439452b96f9fc2e46fcf3412a8 Mon Sep 17 00:00:00 2001 From: finglis <42358257+finglis@users.noreply.github.com> Date: Tue, 6 Feb 2024 17:07:36 +0000 Subject: [PATCH 1/6] interface & controller added --- build.gradle | 3 ++ .../qupath/ext/template/DemoExtension.java | 37 +++++++++++++++---- .../ext/template/InterfaceController.java | 37 +++++++++++++++++++ src/main/resources/interface.fxml | 14 +++++++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 src/main/java/qupath/ext/template/InterfaceController.java create mode 100644 src/main/resources/interface.fxml diff --git a/build.gradle b/build.gradle index cf9a9b8..23807b0 100644 --- a/build.gradle +++ b/build.gradle @@ -50,6 +50,9 @@ dependencies { // See https://docs.gradle.org/current/userguide/platforms.html shadow libs.slf4j + // For JavaFX + shadow libs.qupath.fxtras + // If you aren't using Groovy, this can be removed shadow libs.bundles.groovy diff --git a/src/main/java/qupath/ext/template/DemoExtension.java b/src/main/java/qupath/ext/template/DemoExtension.java index 07de097..b724df4 100644 --- a/src/main/java/qupath/ext/template/DemoExtension.java +++ b/src/main/java/qupath/ext/template/DemoExtension.java @@ -1,16 +1,21 @@ package qupath.ext.template; import javafx.beans.property.BooleanProperty; +//import javafx.beans.property.StringProperty; +import javafx.scene.Scene; import javafx.scene.control.MenuItem; +import javafx.stage.Stage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import qupath.fx.dialogs.Dialogs; import qupath.lib.common.Version; import qupath.lib.gui.QuPathGUI; -import qupath.lib.gui.dialogs.Dialogs; import qupath.lib.gui.extensions.GitHubProject; import qupath.lib.gui.extensions.QuPathExtension; import qupath.lib.gui.prefs.PathPrefs; +import java.io.IOException; + /** * This is a demo to provide a template for creating a new QuPath extension. @@ -68,6 +73,11 @@ public class DemoExtension implements QuPathExtension, GitHubProject { private BooleanProperty enableExtensionProperty = PathPrefs.createPersistentPreference( "enableExtension", true); + /** + * Create a stage for the extension to display + */ + private Stage stage; + @Override public void installExtension(QuPathGUI qupath) { if (isInstalled) { @@ -99,15 +109,28 @@ private void addPreference(QuPathGUI qupath) { private void addMenuItem(QuPathGUI qupath) { var menu = qupath.getMenu("Extensions>" + EXTENSION_NAME, true); MenuItem menuItem = new MenuItem("My menu item"); - menuItem.setOnAction(e -> { - Dialogs.showMessageDialog(EXTENSION_NAME, - "Hello! This is my Java extension."); - }); + menuItem.setOnAction(e -> createStage()); menuItem.disableProperty().bind(enableExtensionProperty.not()); menu.getItems().add(menuItem); } - - + + /** + * Demo showing how to create a new stage with a JavaFX FXML interface. + */ + private void createStage() { + if (stage == null) { + try { + stage = new Stage(); + Scene scene = new Scene(InterfaceController.createInstance()); + stage.setScene(scene); + } catch (IOException e) { + Dialogs.showErrorMessage("Extension Error", "GUI loading failed"); + logger.error("Unable to load extension interface FXML", e); + } + } + stage.show(); + } + @Override public String getName() { return EXTENSION_NAME; diff --git a/src/main/java/qupath/ext/template/InterfaceController.java b/src/main/java/qupath/ext/template/InterfaceController.java new file mode 100644 index 0000000..f4f3e78 --- /dev/null +++ b/src/main/java/qupath/ext/template/InterfaceController.java @@ -0,0 +1,37 @@ +package qupath.ext.template; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.control.TextField; +import javafx.scene.layout.VBox; + +import java.io.IOException; +import java.util.ResourceBundle; + +/** + * Controller for UI pane contained in interface.fxml + */ + +public class InterfaceController extends VBox { + private static final ResourceBundle resources = ResourceBundle.getBundle("qupath.ext.instanseg.ui.strings"); + + @FXML + private TextField tfModelDirectory; + + public static InterfaceController createInstance() throws IOException { + return new InterfaceController(); + } + + private InterfaceController() throws IOException { + var url = InterfaceController.class.getResource("interface.fxml"); + FXMLLoader loader = new FXMLLoader(url, resources); + loader.setRoot(this); + loader.setController(this); + loader.load(); + } + + @FXML + private void runDemoExtension() { +// ExtensionCommand.runDemoExtension(tfModelDirectory.getText()); + } +} diff --git a/src/main/resources/interface.fxml b/src/main/resources/interface.fxml new file mode 100644 index 0000000..0ba8e1c --- /dev/null +++ b/src/main/resources/interface.fxml @@ -0,0 +1,14 @@ + + + + + + + + + + + From e61096851ce6aa044ef632b6be878c09d1cf2d3c Mon Sep 17 00:00:00 2001 From: finglis <42358257+finglis@users.noreply.github.com> Date: Wed, 7 Feb 2024 12:13:40 +0000 Subject: [PATCH 2/6] updates --- .../qupath/ext/template/InterfaceController.java | 4 ++++ src/main/resources/interface.fxml | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/qupath/ext/template/InterfaceController.java b/src/main/java/qupath/ext/template/InterfaceController.java index f4f3e78..4763dd4 100644 --- a/src/main/java/qupath/ext/template/InterfaceController.java +++ b/src/main/java/qupath/ext/template/InterfaceController.java @@ -15,6 +15,7 @@ public class InterfaceController extends VBox { private static final ResourceBundle resources = ResourceBundle.getBundle("qupath.ext.instanseg.ui.strings"); + //TBC if needed in template @FXML private TextField tfModelDirectory; @@ -33,5 +34,8 @@ private InterfaceController() throws IOException { @FXML private void runDemoExtension() { // ExtensionCommand.runDemoExtension(tfModelDirectory.getText()); + System.out.println("Demo extension run"); } + + } diff --git a/src/main/resources/interface.fxml b/src/main/resources/interface.fxml index 0ba8e1c..d0ece76 100644 --- a/src/main/resources/interface.fxml +++ b/src/main/resources/interface.fxml @@ -6,9 +6,12 @@ - + +