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

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

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

import java.util.List;

import antlr.StringUtils;

import com.talk2object.common.StringUtil;
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.InteractiveField;
import com.talk2object.plum.interaction.viewgeneneration.AbstractViewBuilder;
import com.talk2object.plum.interaction.viewgeneneration.ViewBuildContext;
import com.talk2object.plum.interaction.viewgeneneration.ViewClass;
import com.talk2object.plum.interaction.viewgeneneration.ViewPrivillege;
import com.talk2object.plum.view._abstract.components.TextBox;
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;
import com.talk2object.plum.view.event.AbstractEventListener;
import com.talk2object.plum.view.event.ValueChangeListener;

public abstract class AbstractFieldViewBuilder extends AbstractViewBuilder {

	@Override
	public UIComponent build(Object model, String viewClass,
			ViewBuildContext context, UIComponent parentView) {
		if (ViewClass.matches(ViewClass.FORM_FIELD, viewClass)) {
			InteractiveField field = (InteractiveField) model;
			UIComponent result = null;
			if (field.hasOptions())
				result = createListComponent(field);
			else {
				result = createComponent(field);
			}
			return result;
		}
		return null;
	}

	protected UIComponent createComponent(InteractiveField input) {
		TextBox component = new TextBox();
		if (input.getPrivillege() == ViewPrivillege.READ_ONLY)
			component.setReadOnly(true);

		new AbstractController(
				input, component) {

			@Override
			public void updateViewState() {
				String modelStr = getModel().getValue() == null ? ""
						: getModel().getValue().toString();
				getView().setText(modelStr);
			}

			@Override
			public void updateModelState() {

				getModel().setValue(getView().getText());
			}
		};

		return component;
	}

	protected UIComponent createListComponent(InteractiveField input) {
		DropdownList component = new DropdownList();

		List list = input.getOptions();
		if (input.isNullable())
			list.add(0, null);

		Options options = new ObjectOptions(list);
		component.setOptions(options);

		Controller controller = new AbstractController(
				input, component) {

			@Override
			public void updateViewState() {

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

			@Override
			public void updateModelState() {

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

		return component;
	}

}