Login  Register

Re: Non-modal dialog with Yes No Cancel

Posted by Stein Rørvik on Oct 12, 2020; 10:16pm
URL: http://imagej.273.s1.nabble.com/Non-modal-dialog-with-Yes-No-Cancel-tp5024006p5024030.html

Michael,

Thanks for the detailed explanation!

Yes I agree that for now, checking for empty string or 'cancel' should work.
New dialog functions would be great for the future. Modeless dialogs are very handy.

Perhaps an alternative could be to have the option to add additional buttons to the waitForUser function?

Stein

-----Original Message-----
From: ImageJ Interest Group <[hidden email]> On Behalf Of Michael Schmid
Sent: 12. oktober 2020 19:32
To: [hidden email]
Subject: Re: Non-modal dialog with Yes No Cancel

Hi Stijn,

to make it clear I should have written:

As soon as gd.wasCanceled() is called, the script or macro aborts if <esc> or <cancel> was pressed.
The relevant piece of code terminating the script is in gd.wasCanceled().

If you have

 > script += "gd.showDialog();\n";
 > script += "IJ.showMessage('Hello');\n";  > script += "wasCanceled = gd.wasCanceled();\n";  > script += "IJ.showMessage('Hello again');\n";

and you press <cancel>, the 'Hello' message will be there, but you won't see the 'Hello again'.

ImageJ determines whether a macro is active by the name of the thread.
For JavaScript that you start from a text editor window, that name is 'JavaScript'.
If you run it with 'eval' from a macro, the thread name will be 'Macro$'.
Im Macro.abort, an exception is thrown if the thread name ends with
'Macro$':
https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fimagej%2Fimagej1%2Fblob%2Fmaster%2Fij%2FMacro.java%23L70&amp;data=02%7C01%7Cstein.rorvik%40sintef.no%7Ca385988e77a74be54c2908d86ed4fc2b%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637381208346450747&amp;sdata=y0IPZbvz2aq8b6OxNjSAU4UYzBuE4ynn7B4PdoQcX8c%3D&amp;reserved=0

I agree that in principle, checking for 'cancel' or an empty String in your macro should should be more tolerant to future changes than just checking for an empty string.
Nevertheless, a cleaner solution (though some work for implementing it) would be adding "Dialog.enableYesNoCancel(yesLabel, noLabel)" and "Dialog.wasOKed()" macro functions, or adding an optional 'options'
field to getBoolean: getBoolean(message, yesLabel, noLabel, "nonblocking").
Any of these would terminate the macro when the user presses <cancel> or <esc>.

So far a few thoughts...

Michael
________________________________________________________________
On 12.10.20 18:32, Stein Rørvik wrote:

> Edit:
>
> I tried this,
>
> ...
> script += "gd.showDialog();\n";
> script += "IJ.showMessage('Hello');\n"; script += "reply = '';\n"; ...
>
> and Hello is shown also when cancel was pressed. So the script execution does not stop.
>
> Stein
>
> -----Original Message-----
> Sent: 12. oktober 2020 18:27
> Subject: Re: Non-modal dialog with Yes No Cancel
>
> Michael,
>
> Your explanation sounds reasonable, but why does the script indeed return 'Cancel' if run from the command line? I discovered this by accident as I use an external editor which launches ImageJ via the command line when writing or testing new macros.
>
> But I will assume that checking for both an empty string and 'Cancel' will suffice.
>
> Stein
>
> -----Original Message-----
> From: ImageJ Interest Group <[hidden email]> On Behalf Of Michael
> Schmid
> Sent: 12. oktober 2020 17:40
> To: [hidden email]
> Subject: Re: Non-modal dialog with Yes No Cancel
>
> Hi Stein,
>
> the problem seems to be that <cancel> or <escape> terminates a running macro or (in this case) script, as soon as "gd.wasCanceled()" is called.
> Then, the Javascript returns nothing.
>
> You can determine determine the 'canceled' condition by checking for an empty reply String.
> This it is a rather nasty hack, however, not necessarily guaranteed to work in the future if the mechanism of handling the 'canceled' condition should change.
>
> Michael
> ________________________________________________________________
>
>
> On 12.10.20 16:58, Stein Rørvik wrote:
>> Thanks,
>> as a javascript this works fine.
>>
>> I tried to wrap it in an ImageJ macro as follows:
>>
>> script = "";
>> script += "gd = new NonBlockingGenericDialog('YesNoCancel Demo');\n";
>> script += "gd.addMessage('This is a Non-blocking YesNoCancel
>> dialog');\n"; script += "gd.enableYesNoCancel('Do something', 'Do
>> something else');\n"; script += "gd.showDialog();\n"; script +=
>> "reply = '';\n"; script += "if (gd.wasCanceled()) { reply =
>> 'Cancel';}\n"; script += "else if (gd.wasOKed()) { reply =
>> 'Yes';}\n"; script += "else {reply = 'No';}\n"; script += "reply;\n";
>> print(script); reply = eval("script", script); showMessage(reply);
>>
>> The strange thing is that the Cancel condition does not work, but Yes and No work fine. It works however if I launch the macro from the command line using the -macro option. I don't understand why that should make a difference. It also works if I copy the assembled string as printed in the log window and run it from a text window as javascript.
>>
>> I also tried eval("js", script); with the same behaviour.
>> What is the difference between eval("js", script); and eval("script", script); ?
>>
>> I am using Windows 10/64 with Java 1.8.0_172 and the latest daily build.
>>
>> Stein
>>
>> -----Original Message-----
>> From: ImageJ Interest Group <[hidden email]> On Behalf Of Wayne
>> Rasband
>> Sent: 11. oktober 2020 22:42
>> To: [hidden email]
>> Subject: Re: Non-modal dialog with Yes No Cancel
>>
>>> On Oct 9, 2020, at 2:58 PM, Stein Rørvik <[hidden email]> wrote:
>>>
>>> I am looking for a non-modal version of a Yes/No/Cancel dialog to use in a macro.
>>> I need a "No" choice that will provide the option to redo the processing instead of continuing or canceling, as the processing involves some manual input that can be adjusted.
>>
>> Use eval(“js”,code) to call this JavaScript code:
>>
>>     gd = new NonBlockingGenericDialog("YesNoCancel Demo");
>>     gd.addMessage("This is a Non-blocking YesNoCancel dialog");
>>     gd.enableYesNoCancel("Do something", "Do something else");
>>     gd.showDialog();
>>     if (gd.wasCanceled())
>>        IJ.log("User clicked 'Cancel'");
>>     else if (gd.wasOKed())
>>         IJ. log("User clicked 'Yes'");
>>     else
>>        IJ. log("User clicked 'No'”);
>>
>> -wayne
>>
>>
>>> We have a getBoolean(message, yesLabel, noLabel) function which
>>> basically does what I want, but I need a non-modal dialog since the user should be allowed to select different windows to inspect that the results of the macro execution is as expected before continuing.
>>>
>>> Here is an example flow with the existing getBoolean dialog:
>>>
>>> //yes-no-cancel dialog works as desired, but it is only available
>>> modal reply = false; while (!reply) {
>>>                  //do some processing here, creating results to be inspected
>>>                  reply = getBoolean("Are the results ok?", "Yes",
>>> "No"); } //continue
>>>
>>>
>>> We have a Dialog.createNonBlocking function that creates a non-modal dialog, but I find no way to add Yes / No / Cancel buttons to it.
>>>
>>> We can add a checkbox, but it looks clumsy:
>>>
>>> //workaround using checkbox
>>> no = true;
>>> while (no) {
>>>                  //do some processing here, creating results to be inspected
>>>                  Dialog.createNonBlocking("Verify");
>>>                  Dialog.addMessage("Are the results ok?");
>>>                  Dialog.addCheckbox("No", no);
>>>                  Dialog.show();
>>>                  no = Dialog.getCheckbox(); } //continue
>>>
>>> We can also use radio buttons, but that looks equally clumsy:
>>>
>>> //workaround using radio buttons
>>> reply = "No";
>>> while (reply == "No") {
>>>                  //do some processing here, creating results to be inspected
>>>                  Dialog.createNonBlocking("Verify");
>>>                  Dialog.addMessage("Are the results ok?");
>>>                  Dialog.addRadioButtonGroup("Reply", newArray("Yes", "No", "Cancel"), 1, 3, reply);
>>>                  Dialog.show;
>>>                  reply = Dialog.getRadioButton;
>>>                  if (reply == "Cancel") exit; } //continue
>>>
>>> Any ideas?
>>> Some JavaScript that can create the desired non-modal Yes/No/Cancel dialog?
>>>
>>> The best solution would be if we could have a way to add custom
>>> buttons in the Dialog.* functions in the macro language, like Dialog.addButton(text, isDefault).
>>>
>>> I am not sure the best way to have a return value from that, perhaps
>>> by passing a return value to Dialog.show; or having a  Dialog.getButton() function that returned the label of the custom button that was pressed on dialog exit.
>>> Then one could add buttons named like "Redo" or "Continue " which could lead to a different action than "OK" or "Cancel".
>>>
>>> Stein
>>
>> --
>> ImageJ mailing list:
>> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimag
>> e
>> j.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.n
>> o
>> %7Cfe249e976b484db1688008d86ec93da1%7Ce1f00f39604145b0b309e0210d8b32a
>> f
>> %7C1%7C0%7C637381157893713821&amp;sdata=muV3RhcxuZcODLxCIIh5pY6XipIP8
>> 0
>> gdk8hsRLk6P8c%3D&amp;reserved=0
>>
>> --
>> ImageJ mailing list:
>> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimag
>> e
>> j.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.n
>> o
>> %7Cfe249e976b484db1688008d86ec93da1%7Ce1f00f39604145b0b309e0210d8b32a
>> f
>> %7C1%7C0%7C637381157893713821&amp;sdata=muV3RhcxuZcODLxCIIh5pY6XipIP8
>> 0
>> gdk8hsRLk6P8c%3D&amp;reserved=0
>>
>
> --
> ImageJ mailing list:
> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimage
> j.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.no
> %7Ca385988e77a74be54c2908d86ed4fc2b%7Ce1f00f39604145b0b309e0210d8b32af
> %7C1%7C0%7C637381208346460704&amp;sdata=EtJDa9WO%2FZR9L0wRUzbGumON7rSo
> mLVavy3YqT8hUss%3D&amp;reserved=0
>
> --
> ImageJ mailing list:
> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimage
> j.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.no
> %7Ca385988e77a74be54c2908d86ed4fc2b%7Ce1f00f39604145b0b309e0210d8b32af
> %7C1%7C0%7C637381208346460704&amp;sdata=EtJDa9WO%2FZR9L0wRUzbGumON7rSo
> mLVavy3YqT8hUss%3D&amp;reserved=0
>
> --
> ImageJ mailing list:
> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimage
> j.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.no
> %7Ca385988e77a74be54c2908d86ed4fc2b%7Ce1f00f39604145b0b309e0210d8b32af
> %7C1%7C0%7C637381208346460704&amp;sdata=EtJDa9WO%2FZR9L0wRUzbGumON7rSo
> mLVavy3YqT8hUss%3D&amp;reserved=0
>

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Cstein.rorvik%40sintef.no%7Ca385988e77a74be54c2908d86ed4fc2b%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637381208346460704&amp;sdata=EtJDa9WO%2FZR9L0wRUzbGumON7rSomLVavy3YqT8hUss%3D&amp;reserved=0

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