All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.talk2object.plum.interaction.rich.field.viewBuilder.EnumFieldViewBuilder Maven / Gradle / Ivy

package com.talk2object.plum.interaction.rich.field.viewBuilder;

import java.util.ArrayList;
import java.util.List;

import com.talk2object.plum.interaction.controller.AbstractController;
import com.talk2object.plum.interaction.rich.field.EnumField;
import com.talk2object.plum.interaction.rich.field.InteractiveField;
import com.talk2object.plum.view._abstract.components.listbox.option.ObjectOptions;
import com.talk2object.plum.view._abstract.components.listbox.option.Options;
import com.talk2object.plum.view.component.Component;
import com.talk2object.plum.view.component.listbox.DropdownList;

public class EnumFieldViewBuilder extends AbstractFieldViewBuilder {

	@Override
	protected Component createInputComponent(InteractiveField input) {
		DropdownList dlist = new DropdownList();
		Component component = dlist;

		EnumField enumInput = (EnumField) input;

		List enumList = new ArrayList();
		if (input.isNullable())
			enumList.add(0, null);

		for (Object obj : enumInput.getItemClass().getEnumConstants())
			enumList.add(obj);

		Options options = new ObjectOptions(enumList);
		dlist.setOptions(options);
		dlist.setSelectedValue(enumInput.getInitValue());

		new AbstractController(input, dlist) {

			@Override
			public void updateViewState() {

				getView().setSelectedValue(getModel().getValue());
			}

			@Override
			public void updateModelState() {

				getModel().setValue(getView().getSelectedValue());
			}
		};
		return component;
	}

	@Override
	public Class getModelClass() {

		return EnumField.class;
	}
}