@OptionalWidget for Enum?

Hi,

I try to provide a drop down list as an optional widget in my node settings. First question: Should that be possible? If not, I could still use a @ValueSwitchWidget to show or hide my setting.

If yes, let me describe the issue:

I tried the following optional widgets. The first one (string value) works as expected while the second one (enum value) does show the checkbox but nothing else (no matter if checked or not).

	
	@Widget(title = "Optional One", description = "...")
	@OptionalWidget(defaultProvider = Opt1DefaultProvider.class)
	Optional<String> m_opt1;
	
	static final class Opt1DefaultProvider implements DefaultValueProvider<String> {

        @Override
        public String computeState(final NodeParametersInput context) throws StateComputationFailureException {
            return "optional 1 value";
        }

    }
	
	@Widget(title = "Optional Two", description = "...")
	@OptionalWidget(defaultProvider = Opt2DefaultProvider.class)
	Optional<Opt2Enum> m_opt2;
	
	static final class Opt2DefaultProvider implements DefaultValueProvider<Opt2Enum> {

        @Override
        public Opt2Enum computeState(final NodeParametersInput context) throws StateComputationFailureException {
            return Opt2Enum.TWO;
        }

    }
	
	enum Opt2Enum {
        @Label("one")
        ONE,

        @Label("two")
        TWO,
        
        @Label("three")
        THREE;
	}

When checking the checkbox of the second widget, I get the following error:

ERROR	 main KnimeBrowserView	 TypeError: Cannot use 'in' operator to search for 'includeUnknownValues' in TWO (source: https://org.knime.core.ui.dialog/uiext/defaultdialog/dist/NodeDialogApp-sQeR6LBA.js; line: 1267)
ERROR	 main KnimeBrowserView	 TypeError: Cannot read properties of undefined (reading 'initResizeHeight') (source: https://org.knime.core.ui.dialog/uiext/defaultdialog/dist/NodeDialogApp-sQeR6LBA.js; line: 1267)

I would be happy if anyone can help.

Antje

Hi @niederle,

This appears to be a bug. I’ve created a ticket for it (internal reference: UIEXT-3028).
In the meantime, you can work around the issue by attaching a ChoicesProvider to the field. The provider should return all enum values.

@Widget(title = "Optional Two", description = "...")
@OptionalWidget(defaultProvider = Opt2DefaultProvider.class)
@ChoicesProvider(Opt2Provider.class)
Optional<Opt2Enum> m_opt2 = Optional.empty();

static final class Opt2Provider implements EnumChoicesProvider<Opt2Enum> {
    @Override
    public List<Opt2Enum> choices(final NodeParametersInput context) {
        return Arrays.asList(Opt2Enum.values());
    }
}

Best,
Robin

1 Like

Thank you very much for your proposal how to solve the issue. It works! :slightly_smiling_face:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.