Macros: change Dialog checkbox state without clicking it?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Macros: change Dialog checkbox state without clicking it?

Bill Christens-Barry-2
In a macro, I would like to create a Dialog in which changing the checked states of some "control" checkboxes would change the state of other
"dependent" checkboxes. I envision that whenever a control checkbox is checked or unchecked, some logic would decide which of the dependent checkboxes should have their state changed and those changes would immediately be made. The Dialog would remain active for further interaction.

In order to programmatically alter the state of checkboxes, I suppose there would need to be a way to refer to previously created checkboxes (~ Dialog.getState(checkbox)) and a command for changing them (~ Dialog.setState(checkbox, state)). I haven't found any way to do either in the macro language.

Is what I describe currently possible using the ImageJ macro language or other scripting language?

Thanks.

Bill Christens-Barry

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macros: change Dialog checkbox state without clicking it?

ctrueden
Hi Bill,

The SciJava framework of ImageJ2 offers that feature, via parameter
callbacks. In Java, if you declare parameters such as:

@Parameter(label = "x", callback = "xChanged")
private float x;

@Parameter(label = "2x", callback = "twoXChanged")
private float twoX;

And then write the methods:

/** Executed whenever the {@link #x} parameter changes. */
private void xChanged() {
twoX = x * 2;
}

/** Executed whenever the {@link #twoX} parameter changes. */
private void twoXChanged() {
x = twoX / 2;
}

Then whenever the x or 2x field is changed, the callback code is executed
adjusting the other value accordingly, and the dialog box will refresh
automatically.

This code and more can be seen in the WidgetDemo example [1].

Unfortunately, the callback attribute of parameters is not yet implemented
for script languages [2]. There is an open issue for it [3]. The good news
is: upon reading your question and thinking about this issue some more, I
have an idea for how to implement it [4].

Regards,
Curtis

[1]
https://github.com/imagej/tutorials/blob/9880387c96d6b6399c016f755707878f33812fc1/maven-projects/widget-demo/src/main/java/WidgetDemo.java
[2] https://imagej.net/Script_Parameters
[3] https://github.com/scijava/scijava-common/issues/200
[4]
https://github.com/scijava/scijava-common/issues/200#issuecomment-329591887

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Did you know ImageJ has a forum? http://forum.imagej.net/


On Thu, Sep 14, 2017 at 1:59 PM, Bill Christens-Barry <[hidden email]>
wrote:

> In a macro, I would like to create a Dialog in which changing the checked
> states of some "control" checkboxes would change the state of other
> "dependent" checkboxes. I envision that whenever a control checkbox is
> checked or unchecked, some logic would decide which of the dependent
> checkboxes should have their state changed and those changes would
> immediately be made. The Dialog would remain active for further interaction.
>
> In order to programmatically alter the state of checkboxes, I suppose
> there would need to be a way to refer to previously created checkboxes (~
> Dialog.getState(checkbox)) and a command for changing them (~
> Dialog.setState(checkbox, state)). I haven't found any way to do either in
> the macro language.
>
> Is what I describe currently possible using the ImageJ macro language or
> other scripting language?
>
> Thanks.
>
> Bill Christens-Barry
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macros: change Dialog checkbox state without clicking it?

Wayne Rasband-2
In reply to this post by Bill Christens-Barry-2
> On Sep 14, 2017, at 2:59 PM, Bill Christens-Barry <[hidden email]> wrote:
>
> In a macro, I would like to create a Dialog in which changing the checked states of some "control" checkboxes would change the state of other
> "dependent" checkboxes. I envision that whenever a control checkbox is checked or unchecked, some logic would decide which of the dependent checkboxes should have their state changed and those changes would immediately be made. The Dialog would remain active for further interaction.
>
> In order to programmatically alter the state of checkboxes, I suppose there would need to be a way to refer to previously created checkboxes (~ Dialog.getState(checkbox)) and a command for changing them (~ Dialog.setState(checkbox, state)). I haven't found any way to do either in the macro language.
>
> Is what I describe currently possible using the ImageJ macro language or other scripting language?

You can do this in JavaScript by using a DialogListener. Here is an example:
   
    listener = new DialogListener() {
        dialogItemChanged : function(gd, event) {
            state = gd.getNextBoolean();
            checkbox2.setState(state);
            return true;
        }
    };
    gd = new GenericDialog("DialogListener Demo");
    gd.addCheckbox("Checkbox 1", false);
    gd.addCheckbox("Checkbox 2", false);
    checkbox2 = gd.getCheckboxes().get(1);
    gd.addDialogListener(listener);
    gd.showDialog();

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macros: change Dialog checkbox state without clicking it?

Bill Christens-Barry-2
In reply to this post by Bill Christens-Barry-2
Curtis, Wayne,

Thanks for pointing me to a solution. For the moment I'm going with a JavaScript approach since I have much to learn about ImageJ2. This code allows me to set or clear individual or all checkboxes in a CheckboxGroup, based on two other control checkboxes:

    listener = new DialogListener() {
        dialogItemChanged : function(gd, event) {
            doAllState = gd.getNextBoolean();
            doNoneState = gd.getNextBoolean();
            allBoxes = gd.getCheckboxes();
            for (i = 2; i < 22; i++) {
                thisBox = allBoxes.get(i);
                if (doAllState)
                    thisBox.setState(true);
                if (doNoneState)
                    thisBox.setState(false);
            }
    doAll = gd.getCheckboxes().get(0);
    doAll.setState(false);
    doNone = gd.getCheckboxes().get(1);
    doNone.setState(false);
           return true;
        }
    };

    var labels = [];
    var defaults = [];
    for (i = 0; i < 20; i++) {
        labels[i] = "✓box "+(i+1);
        defaults[i] = false;
    }
    gd = new GenericDialog("BoxChecker");
    gd.addCheckbox("Set All", false);
    gd.addCheckbox("Clear All", false);
    gd.addCheckboxGroup(4, 5, labels, defaults);
    gd.addDialogListener(listener);
    gd.showDialog();

The control checkboxes should really be buttons (since their state is not persistent), but I didn't see how to add more buttons into a GenericDialog other than via GenericDialog.enableYesNoCancel(). I instead clear each of those after dealing with the CheckboxGroup.

Bill Christens-Barry

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html