Last week Jesse Gallagher blogged about using enums in a combo box. For those who are not familiar with enums, if you’ve started using Java it’s well worth reviewing the article where I covered them.
Thankfully I was involved in a chat around that topic with Jesse and Nathan. I had previously written code to create an ArrayList from an enum, because I was using an enum for options for my Keyword documents. But Jesse’s solution is far more elegant, not least because it uses the SelectItem class that the Combo Box etc use. Why is that more elegant? Because an ArrayList is going to need to be converted into SelectItem objects anyway, so why not go straight to the source!
In most cases, a Combo Box is probably a good choice for selecting options from an enum, because enums do not typically have a large number of options to select from. But what if you want to use a picker?
But the discussion and his article made me revisit an Enum Picker dataProvider Nathan had done an initial draft of in the OpenNTF Domino API. I’d done some tweaking of it when getting the other pickers working, but was unclear how to test it. But I revisited the code now I realilsed how to test it, and got it working. (The only fix I had to make was the length of the property array to serialize.)
There is a slight difference to the format of the options. The visible label is still the enum name, but the value stored is “enumClass enumName”, e.g. “org.openntf.domino.impl.Document$RemoveType HARD_TRUE”. The benefit of using this is that the OpenNTF Domino API has a TypeUtils class with a toEnum(String) method that expects this format. So the value is stored as a String, but can easily be converted to the relevant Enum.
One point to make here is that a normal enum class uses the standard dot notation, so e.g. org.openntf.domino.AutoMime which is an enum of the AutoMime options the OpenNTF Domino API uses. However, an enum that is contained within another class needs className$enumClass, so org.openntf.domino.impl.Document$RemoveType for the RemoveType enum. And those enums are in the impl classes, not the core org.openntf.domino class that will be imported for SSJS or Java.
The next release of OpenNTF Domino API will be coming out shortly, so this will include the Enum Picker, as well as the Collections Picker and Map Picker.