scijava-common icon indicating copy to clipboard operation
scijava-common copied to clipboard

Persistence of enum choices in Commands

Open stefanhahmann opened this issue 2 years ago • 6 comments

With https://github.com/scijava/scijava-common/commit/664b3ab482f7cda3303154507b7e515a48cc6dee and https://github.com/scijava/scijava-common/commit/61e98d7cdc820cf85f8957f11b93ebb1dc2115d8 it was made possible to use enums with custom labels in scijava commands

However, currently enums with custom labels are not persisted, i.e. if the user makes some settings using an enum with custom labels in a dialog, closes this dialog and comes back to this dialog, the selected value would fall back to the default parameter instead of the last selected parameter, which users may consider a bug, since this behavior would be different to other parameter types.

stefanhahmann avatar Oct 12 '23 13:10 stefanhahmann

I think this issue is general for enums (i.e. not only enums with custom labels).

Looking at MoBIE's OpenCollectionTableCommand.DataRoot: https://github.com/mobie/mobie-viewer-fiji/blob/18700b576efcc9574a9395598939bbf354f970e7/src/main/java/org/embl/mobie/command/open/OpenCollectionTableCommand.java#L49-L53

... non-default values seem to be correctly persisted to preference storage:

#@ PrefService ps
#@ ConvertService cs

import org.embl.mobie.command.open.OpenCollectionTableCommand
import org.embl.mobie.command.open.OpenCollectionTableCommand.DataRoot

stringValue = ps.get(OpenCollectionTableCommand.class, "dataRoot")

println(stringValue)  // e.g. UseTableFolder

converted = cs.convert(stringValue, DataRoot.class)

println(converted)  // e.g. UseTableFolder
println(converted.class)  // class org.embl.mobie.command.open.OpenCollectionTableCommand$DataRoot

However, the command dialog still falls back to default value every time.

Hence I think the issue is somewhere in the command module processing, not in the preferences.

@ctrueden would you have a best guess where to dig next?

imagejan avatar Sep 27 '24 12:09 imagejan

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/scijava-persist-not-working-for-enums/99827/7

imagesc-bot avatar Mar 26 '25 15:03 imagesc-bot

I noticed that persisted enum values are actually harvested just fine: as long as you don't touch the dropdown widget, the persisted value is kept. It's just the UI that isn't updated to reflect the persisted value 🤦

Consider this Groovy script:

#@ org.scijava.ItemIO itemio

println itemio

When you run it, the dialog will always show the first enum value, INPUT:

Image

However, if you choose an alternative value (e.g. BOTH), press OK, and re-run the script, this time without changing in the dropdown (that again shows INPUT), the correct, persisted value (i.e. BOTH) will be printed.

That means this issue actually belongs to scijava-ui-swing 🚀 .

(NB: This didn't work in older Fiji installation of mine that had scijava-common-2.94.2, so the persisting behavior must have changed between that version and scijava-common-2.99.0)

imagejan avatar Mar 27 '25 15:03 imagejan

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/scijava-persist-not-working-for-enums/99827/9

imagesc-bot avatar Mar 28 '25 15:03 imagesc-bot

Hi @imagejan and @ctrueden, are those changes already shipped with Fiji? I think I observed some strange behaviour related to the persistence of enums. I would investigate more if you tell me that those changes are already in place.

tischi avatar Apr 16 '25 07:04 tischi

I am sorry 😬 !

I did not carefully read @imagejan's above post. In fact it seems that he exactly found what I found. I also saw now that the PR has not been merged yet.

tl;dr please simply ignore all my comments.

tischi avatar Apr 16 '25 15:04 tischi