Skip to content

Commit

Permalink
Add externalised strings and two examples of preferences in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan committed Feb 7, 2024
1 parent e8b02cf commit 77e30fa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
51 changes: 48 additions & 3 deletions src/main/java/qupath/ext/template/DemoExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import javafx.beans.property.BooleanProperty;
//import javafx.beans.property.StringProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.ext.template.ui.InterfaceController;
import qupath.fx.dialogs.Dialogs;
import qupath.fx.prefs.controlsfx.PropertyItemBuilder;
import qupath.lib.common.Version;
import qupath.lib.gui.QuPathGUI;
import qupath.lib.gui.extensions.GitHubProject;
Expand Down Expand Up @@ -69,11 +72,30 @@ public class DemoExtension implements QuPathExtension, GitHubProject {
private boolean isInstalled = false;

/**
* A 'persistent preference' - showing how to create a property that is stored whenever QuPath is closed
* A 'persistent preference' - showing how to create a property that is stored whenever QuPath is closed.
* This preference will be managed in the main QuPath GUI preferences window.
*/
private BooleanProperty enableExtensionProperty = PathPrefs.createPersistentPreference(
private static BooleanProperty enableExtensionProperty = PathPrefs.createPersistentPreference(
"enableExtension", true);


/**
* Another 'persistent preference'.
* This one will be managed using a GUI element created by the extension.
* We use {@link Property<Integer>} rather than {@link IntegerProperty}
* because of the type of GUI element we use to manage it.
*/
private static Property<Integer> numThreadsProperty = PathPrefs.createPersistentPreference(
"demo.num.threads", 1).asObject();

/**
* An example of how to expose persistent preferences to other classes in your extension.
* @return The persistent preference, so that it can be read or set somewhere else.
*/
public static Property<Integer> numThreadsProperty() {
return numThreadsProperty;
}

/**
* Create a stage for the extension to display
*/
Expand All @@ -87,12 +109,35 @@ public void installExtension(QuPathGUI qupath) {
}
isInstalled = true;
addPreference(qupath);
addPreferenceToPane(qupath);
addMenuItem(qupath);
}

/**
* Demo showing how to add a persistent preference to the QuPath preferences pane.
* @param qupath
* The preference will be in a section of the preference pane based on the
* category you set. The description is used as a tooltip.
* @param qupath The currently running QuPathGUI instance.
*/
private void addPreferenceToPane(QuPathGUI qupath) {
var propertyItem = new PropertyItemBuilder<>(enableExtensionProperty, Boolean.class)
.name("Enable extension")
.category("Demo extension")
.description("Enable the demo extension")
.build();
qupath.getPreferencePane()
.getPropertySheet()
.getItems()
.add(propertyItem);
}

/**
* Demo showing how to add a persistent preference.
* This will be loaded whenever QuPath launches, with the value retained unless
* the preferences are reset.
* However, users will not be able to edit it unless you create a GUI
* element that corresponds with it
* @param qupath The currently running QuPathGUI instance.
*/
private void addPreference(QuPathGUI qupath) {
qupath.getPreferencePane().addPropertyPreference(
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/qupath/ext/template/ui/InterfaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import qupath.ext.template.DemoExtension;
import qupath.fx.dialogs.Dialogs;

import java.io.IOException;
import java.util.ResourceBundle;
Expand All @@ -13,11 +17,10 @@
*/

public class InterfaceController extends VBox {
private static final ResourceBundle resources = ResourceBundle.getBundle("qupath.ext.instanseg.ui.strings");
private static final ResourceBundle resources = ResourceBundle.getBundle("qupath.ext.template.ui.strings");

//TBC if needed in template
@FXML
private TextField tfModelDirectory;
private Spinner<Integer> threadSpinner;

public static InterfaceController createInstance() throws IOException {
return new InterfaceController();
Expand All @@ -29,11 +32,22 @@ private InterfaceController() throws IOException {
loader.setRoot(this);
loader.setController(this);
loader.load();

// For extensions with a small number of options,
// or with options that are very important for how the extension works,
// it may be better to present them all to the user in the main extension GUI,
// binding them to GUI elements, so they are updated when the user interacts with
// the GUI, and so that the GUI elements are updated if the preference changes
threadSpinner.getValueFactory().valueProperty().bindBidirectional(DemoExtension.numThreadsProperty());
threadSpinner.getValueFactory().valueProperty().addListener((observableValue, oldValue, newValue) -> {
Dialogs.showInfoNotification(
resources.getString("title"),
String.format(resources.getString("threads"), newValue));
});
}

@FXML
private void runDemoExtension() {
// ExtensionCommand.runDemoExtension(tfModelDirectory.getText());
System.out.println("Demo extension run");
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/qupath/ext/template/ui/interface.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@

<fx:root type="VBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/20" xmlns:fx="http://javafx.com/fxml/1">
<Button onAction="#runDemoExtension" text="Run"/>
<Spinner fx:id="threadSpinner" prefWidth="75.0">
<valueFactory>
<SpinnerValueFactory.IntegerSpinnerValueFactory max="96" min="1" />
</valueFactory>
</Spinner>
</fx:root>
3 changes: 3 additions & 0 deletions src/main/resources/qupath/ext/template/ui/strings.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title = Demo extension

threads = Threads set to %d

0 comments on commit 77e30fa

Please sign in to comment.