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

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

There is a newer version: 0.1.22
Show newest version
package com.talk2object.plum.interaction.rich.field.component;

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

import com.talk2object.plum.interaction.controller.AbstractController;
import com.talk2object.plum.interaction.controller.Controller;
import com.talk2object.plum.interaction.rich.field.AbstractComponentBridge;
import com.talk2object.plum.interaction.rich.field.ComponentBridge;
import com.talk2object.plum.interaction.rich.field.EnumField;
import com.talk2object.plum.interaction.rich.field.InteractiveField;
import com.talk2object.plum.view._abstract.components.listbox.DropdownList;
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._abstract.window.UIComponent;

public class EnumFieldViewBuilder extends AbstractFieldViewBuilder {

	@Override
	protected UIComponent createComponent(InteractiveField input) {
		DropdownList dlist = new DropdownList();
		UIComponent component = dlist;

		EnumField enumInput = (EnumField) input;

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

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

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

		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;
	}
}