Adding a combo box to the modules settings config panel, which is bound to string field in settings record

Does anyone have an example of how to do this.

Thanks!

A combobox with a fixed list of options, or does it need to be a dynamic list from somewhere?

Either way - the gist is you need to set the editor source for your StringField. In a static block in your PersistentRecord implementation, do something like:

MyField.getFormMeta().setEditorSource(yourEditorSource)

Where yourEditorSource needs to implement IEditorSource.
The newEditorComponent returned should probably extend AbstractEditor. Something like this:

public class LanguageEditor extends AbstractEditor {

    public static final Comparator<Locale> LOCALE_COMPARATOR = 
        Comparator.<Locale, String>comparing(Locale::getDisplayLanguage, String.CASE_INSENSITIVE_ORDER)
            .thenComparing(Locale::getDisplayCountry, String.CASE_INSENSITIVE_ORDER);

    public LanguageEditor(String id, FormMeta formMeta, RecordEditMode editMode, SRecordInstance record) {
        super(id, formMeta, editMode, record);

        LanguageDropdownChoice dropdown = new LanguageDropdownChoice("editor");

        formMeta.installValidators(dropdown);

        dropdown.setLabel(new LenientResourceModel(formMeta.getFieldNameKey()));

        add(dropdown);
    }

    private class LanguageDropdownChoice extends DropDownChoice<Locale> implements IRecordFieldComponent {

        public LanguageDropdownChoice(String id) {
            super(id);

            List<Locale> choices = new ArrayList<>();
            Collections.addAll(choices, Locale.getAvailableLocales());
            choices.sort(LOCALE_COMPARATOR);

            setChoices(choices);
            setChoiceRenderer(new ChoiceRenderer<>() {
                @Override
                public Object getDisplayValue(Locale value) {
                    if (value == null) {
                        return null;
                    }
                    return String.format("%s (%s)", value.getDisplayLanguage(),
                        StringUtils.isBlank(value.getDisplayCountry()) ? "General" : value.getDisplayCountry()
                    );
                }
            });
        }

        @Override
        public SFieldMeta getFieldMeta() {
            return getFormMeta().getField();
        }

    }

    public static class LanguageEditorSource implements IEditorSource {
        public static final LanguageEditorSource INSTANCE = new LanguageEditorSource();

        @Override
        public Component newEditorComponent(String id, RecordEditMode editMode, SRecordInstance record,
                                            FormMeta formMeta) {
            return new LanguageEditor(id, formMeta, editMode, record);
        }
    }
}

The easiest way to get Comboboxes in Ignition's Wicket infrastructure is to use the EnumField, not strings.

1 Like

Yes , thanks all. I would prefer enum and found this in the API for field types. Thanks!