Re: Persisting State In java.util.prefs or Somewhere Else?

Posted by Alan Brooks on
URL: http://imagej.273.s1.nabble.com/Persisting-State-In-java-util-prefs-or-Somewhere-Else-tp5024392p5024401.html

Thanks. This is cleaner and works for me.

Alan

> On Jan 19, 2021, at 8:02 PM, Wayne Rasband <[hidden email]> wrote:
>
>> On Jan 18, 2021, at 11:07 PM, Alan Brooks <[hidden email] <mailto:[hidden email]>> wrote:
>>
>> I am using the newly available Toolbar.setIcon() method to provide the ability to set a visualization configuration that is persistent across ImageJ sessions. I am currently using java.util.prefs.Preferences to accomplish this, as shown below. Is there something for user preferences within ImageJ I should be considering instead?
>
> You can use the Prefs.set() and Prefs.get() methods. Here is a version of the "Toggle Icon Action Tool” that uses these methods:
>
>   macro "Toggle Icon Action Tool - C037N44F00nn" {
>       state = call("ij.Prefs.get", "toggletool.state", "box");
>       if (state=="box") {
>          call("ij.gui.Toolbar.setIcon", "Toggle Icon Action Tool", "Ca30N44V00nn");
>          call("ij.Prefs.set", "toggletool.state", "circle");
>       } else {
>          call("ij.gui.Toolbar.setIcon", "Toggle Icon Action Tool", "C037N44F00nn");
>          call("ij.Prefs.set", "toggletool.state", "box");
>       }
>  }
>
> -wayne
>
>
>> isOn = eval("script", 'java.util.prefs.Preferences.userRoot().getBoolean("ab.rotator.isOn", false);');
>> if (isOn=="false") {
>>  call("ij.gui.Toolbar.setIcon", "Toggle Rotator Action Tool", "Cc00V00ff");
>>  eval("script", 'java.util.prefs.Preferences.userRoot().putBoolean("ab.rotator.isOn", true);');
>>  showStatus("Rotator enabled");
>> } else {
>>  call("ij.gui.Toolbar.setIcon", "Toggle Rotator Action Tool", "C00cF00ff");
>>  eval("script", 'java.util.prefs.Preferences.userRoot().putBoolean("ab.rotator.isOn", false);');
>>  showStatus("Rotator disabled");
>> }
>>
>> Thanks,
>> Alan
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html <http://imagej.nih.gov/ij/list.html>

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