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

com.talk2object.plum.interaction.rich.field.InteractiveField Maven / Gradle / Ivy

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

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

import com.talk2object.plum.interaction.input.validation.ValidationPolicy;
import com.talk2object.plum.interaction.input.validation.Validator;
import com.talk2object.plum.interaction.viewgeneneration.ViewPrivillege;

/**
 * It is abstract interactive object, which describes prompt text, validation
 * rule etc. Later, a concrete interaction manager can use these information to
 * render a GUI object.
 * 
 * other informations: options(for enumeration), required, hint, etc.
 * 
 * @author jackding
 * 
 */
public class InteractiveField {
	protected String name;
	/**
	 * the class of field or parameter.
	 */
	protected Class fieldClass;
	protected FieldType type;
	protected ViewPrivillege privillege;
	protected Object initValue;
	/**
	 * optional
	 */
	protected List options;

	protected boolean nullable = true;

	protected ValidationPolicy validationPolicy = ValidationPolicy.STOP_AT_FIRST_FAILURE;
	protected List validators = new ArrayList();

	private FieldValueAccessor valueAccessor = new ObjectHolder();

	public InteractiveField(String name, FieldType type, Class clazz) {
		this.name = name;
		this.type = type;
		this.fieldClass = clazz;
	}

	public boolean validate() {
		int failureCount = 0;

		for (Validator v : validators) {

			boolean result = v.validate(getValue());
			if (result == false) {
				failureCount++;

				if (validationPolicy == ValidationPolicy.STOP_AT_FIRST_FAILURE)
					return false;
			}

		}

		return failureCount == 0;
	}

	public Object getInitValue() {
		return initValue;
	}

	public void setInitValue(Object initValue) {
		this.initValue = initValue;
	}

	public List getOptions() {
		return options;
	}

	public void setOptions(List options) {
		this.options = options;
	}

	public Object getValue() {
		return valueAccessor.get();
	}

	public void setValue(Object val) {
		valueAccessor.set(val);
	}

	/**
	 * set value to initial value.
	 */
	public void initValue() {
		setValue(getInitValue());
	}

	public void addValidator(Validator validator) {
		validators.add(validator);
	}

	public FieldType getType() {
		return type;
	}

	public ViewPrivillege getPrivillege() {
		return privillege;
	}

	public void setPrivillege(ViewPrivillege privillege) {
		this.privillege = privillege;
	}

	public Class getFieldClass() {
		return fieldClass;
	}

	public void setFieldClass(Class clazz) {
		this.fieldClass = clazz;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	public FieldValueAccessor getValueAccessor() {
		return valueAccessor;
	}

	public void setValueAccessor(FieldValueAccessor valueAccessor) {
		this.valueAccessor = valueAccessor;
	}

	public boolean hasOptions() {
		return options != null && !options.isEmpty();
	}
}