EnumField Example

Does anyone have an example of how to use the following to make a drop down options list on a settings page?

EnumField

Thanks,

Nick

Answering my own question here and hopefully helping someone down the line, here is how to do it:

1) In the Settings File Create an enum

public enum alarmPriorities { Diagnostic, Low, Medium, High, Critical };

2) Create a EnumField

    public static final EnumField<alarmPriorities> MinimumPriority =
            new EnumField<>(META, "MinimumPriority", alarmPriorities.class, SFieldFlags.SMANDATORY).
                    setDefault(alarmPriorities.Medium);

I still need to make more access methods so the field value is actually used for filtering, but for testing I just made a simple method that sets the default value so the module could install without error:

3) Create necessary access methods

public void setDefaultAlarmPriority() { setEnum(MinimumPriority, alarmPriorities.Medium); }

Then when the module is installed it looks like this:

I’ll post the access methods later on.

Nick

4 Likes

Here are two additional access methods that were made:

The first is to get the string value

    public String getAlarmPriorityString() { 
        return getEnum(MinimumPriority).toString(); 
    }

The second is to get the integer value of the string index from the enum. This is needed because the alarm priority which will be filtered against is an integer 0-4.

Get the index value as an integer

    public int getAlarmPriorityInt() { 
        return alarmPriorities.valueOf(
                getAlarmPriorityString()
        ).ordinal(); 
    }

Then the integer value is used like this on the alarm event action

int minimumPriority = kafkaSettings.getAlarmPriorityInt();
Boolean hasPriority = alarm.getPriority().ordinal() >= minimumPriority;

Rgds,

Nick