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

Posted by Wayne Rasband-2 on
URL: http://imagej.273.s1.nabble.com/Persisting-State-In-java-util-prefs-or-Somewhere-Else-tp5024392p5024400.html

> On Jan 18, 2021, at 11:07 PM, Alan Brooks <[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