Question

I tried doing some research on the SmartGWT Showcase and forums, but I could not find the answer. I also tried a more wide-spread Google search and couldn't find an answer either. So, I am hoping I can get some help here.

I have a enum java class in my "shared" folder:

public enum PointInTimeType {

FIRST("F", "First"),
START("S", "Start"),
END("E", "End"),
LAST("L", "Last"),
MIN("MI", "Minimum"),
MAX("MX", "Maximum");

private String code;
private String name;

private PointInTimeType(String code, String name) {
    this.code = code;
    this.name = name;

}

public String getCode() {
    return code;
}

public String getName() {
    return name;
}

I know I can just create a DataSourceEnumField and set the valuemap, but these values are hard-coded and the enum java class could add more enums in the future. I wouldn't want to have to update the ValueMap whenever the enums is updated.

So, knowing that I am going to have a form with a drop-down (SelectItem), I'd like to set the optionDataSource to a list of values from the enumfield.

So, I guess I am asking how do I setup a DataSource to get this enum data, or be tied to this enum java class?

This way, when I create my form and tie it to another datasource, then I can create a SlectItem, and set it up the way I would any other drop-down. I am just used to using DataSources tied to a list of objects, and not enums.

Was it helpful?

Solution

I faced the same problem and I found out the "Enum" in the name class is indeed misleading.

Anyway, even if you could use an Enum, it would be useless for you since you have custom attributes.

But, you can workaround this problem iterating over the enum values and filling the map. One generic way is creating an interface like this:

public interface CodeNameEnum {
    String getCode();
    String getName();
}

Then, make you enum implement it:

public enum PointInTimeType implements CodeNameEnum {

    FIRST("F", "First"),
    START("S", "Start"),
    END("E", "End"),
    LAST("L", "Last"),
    MIN("MI", "Minimum"),
    MAX("MX", "Maximum");

    private String code;
    private String name;

    private PointInTimeType(String code, String name) {
        this.code = code;
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public String getName() {
        return name;
    }

}

Finally, use this method:

public class EnumToMap {

    public static Map<String, String> getMapFromEnum(CodeNameEnum[] codeNameEnumArray) {
        Map <String, String> map = new HashMap<String, String>();
        for (CodeNameEnum codeNameEnum : codeNameEnumArray) {
            map.put(codeNameEnum.getCode(), codeNameEnum.getName());
        }
        return map;
    }

    public static void main(String[] args) {
        Map <String, String> map = getMapFromEnum(PointInTimeType.values());
        System.out.println(map);
    }

}

So you can use this method for every enum containing code and name.

Otherwise, if it's a isolated case, just do this:

Map <String, String> map = new HashMap<String, String>();
for (PointInTimeType item : PointInTimeType.values()) {
    map.put(item.getCode(), item.getName());
}
return map;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top