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

com.vaadin.v7.data.fieldgroup.DefaultFieldGroupFieldFactory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.v7.data.fieldgroup;

import java.util.Date;
import java.util.EnumSet;

import com.vaadin.data.Binder;
import com.vaadin.data.HasValue;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.fieldgroup.FieldGroup.BindException;
import com.vaadin.v7.ui.AbstractField;
import com.vaadin.v7.ui.AbstractSelect;
import com.vaadin.v7.ui.AbstractTextField;
import com.vaadin.v7.ui.CheckBox;
import com.vaadin.v7.ui.ComboBox;
import com.vaadin.v7.ui.DateField;
import com.vaadin.v7.ui.Field;
import com.vaadin.v7.ui.InlineDateField;
import com.vaadin.v7.ui.ListSelect;
import com.vaadin.v7.ui.NativeSelect;
import com.vaadin.v7.ui.OptionGroup;
import com.vaadin.v7.ui.PopupDateField;
import com.vaadin.v7.ui.RichTextArea;
import com.vaadin.v7.ui.Table;
import com.vaadin.v7.ui.TextField;

/**
 * This class contains a basic implementation for {@link FieldGroupFieldFactory}
 * .The class is singleton, use {@link #get()} method to get reference to the
 * instance.
 *
 * @author Vaadin Ltd
 * @deprecated As of 8.0, no direct replacement available.
 *             {@link Binder#forMemberField(HasValue)} and
 *             {@link Binder#bindInstanceFields(Object)} should be used instead.
 */
@Deprecated
public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory {

    private static final DefaultFieldGroupFieldFactory INSTANCE = new DefaultFieldGroupFieldFactory();

    public static final Object CAPTION_PROPERTY_ID = "Caption";

    protected DefaultFieldGroupFieldFactory() {
    }

    /**
     * Gets the singleton instance.
     *
     * @since 7.4
     *
     * @return the singleton instance
     */
    public static DefaultFieldGroupFieldFactory get() {
        return INSTANCE;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public  T createField(Class type, Class fieldType) {
        if (Enum.class.isAssignableFrom(type)) {
            return createEnumField(type, fieldType);
        } else if (Date.class.isAssignableFrom(type)) {
            return createDateField(fieldType);
        } else if (Boolean.class.isAssignableFrom(type)
                || boolean.class.isAssignableFrom(type)) {
            return createBooleanField(fieldType);
        }
        if (AbstractTextField.class.isAssignableFrom(fieldType)) {
            return fieldType.cast(createAbstractTextField(
                    fieldType.asSubclass(AbstractTextField.class)));
        } else if (fieldType == RichTextArea.class) {
            return fieldType.cast(createRichTextArea());
        }
        return createDefaultField(type, fieldType);
    }

    protected RichTextArea createRichTextArea() {
        RichTextArea rta = new RichTextArea();
        rta.setImmediate(true);

        return rta;
    }

    @SuppressWarnings({ "unchecked" })
    private > T createEnumField(Class type,
            Class fieldType) {
        // Determine first if we should (or can) create a select for the enum
        Class selectClass = null;
        if (AbstractSelect.class.isAssignableFrom(fieldType)) {
            selectClass = (Class) fieldType;
        } else if (anySelect(fieldType)) {
            selectClass = AbstractSelect.class;
        }

        if (selectClass != null) {
            AbstractSelect s = createCompatibleSelect(selectClass);
            populateWithEnumData(s, (Class>) type);
            return (T) s;
        } else if (AbstractTextField.class.isAssignableFrom(fieldType)) {
            return (T) createAbstractTextField(
                    (Class) fieldType);
        }

        return null;
    }

    @SuppressWarnings({ "unchecked" })
    private > T createDateField(Class fieldType) {
        AbstractField field;

        if (InlineDateField.class.isAssignableFrom(fieldType)) {
            field = new InlineDateField();
        } else if (anyField(fieldType)
                || DateField.class.isAssignableFrom(fieldType)) {
            field = new PopupDateField();
        } else if (AbstractTextField.class.isAssignableFrom(fieldType)) {
            field = createAbstractTextField(
                    (Class) fieldType);
        } else {
            return null;
        }

        field.setImmediate(true);
        return (T) field;
    }

    protected AbstractSelect createCompatibleSelect(
            Class fieldType) {
        AbstractSelect select;
        if (fieldType.isAssignableFrom(ListSelect.class)) {
            select = new ListSelect();
            select.setMultiSelect(false);
        } else if (fieldType.isAssignableFrom(NativeSelect.class)) {
            select = new NativeSelect();
        } else if (fieldType.isAssignableFrom(OptionGroup.class)) {
            select = new OptionGroup();
            select.setMultiSelect(false);
        } else if (fieldType.isAssignableFrom(Table.class)) {
            Table t = new Table();
            t.setSelectable(true);
            select = t;
        } else {
            select = new ComboBox(null);
        }
        select.setImmediate(true);
        select.setNullSelectionAllowed(false);

        return select;
    }

    /**
     * @since 7.4
     * @param fieldType
     *            the type of the field
     * @return true if any AbstractField can be assigned to the field
     */
    protected boolean anyField(Class fieldType) {
        return fieldType == Field.class || fieldType == AbstractField.class;
    }

    /**
     * @since 7.4
     * @param fieldType
     *            the type of the field
     * @return true if any AbstractSelect can be assigned to the field
     */
    @SuppressWarnings("rawtypes")
    protected boolean anySelect(Class fieldType) {
        return anyField(fieldType) || fieldType == AbstractSelect.class;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected  T createBooleanField(Class fieldType) {
        if (fieldType.isAssignableFrom(CheckBox.class)) {
            CheckBox cb = new CheckBox(null);
            cb.setImmediate(true);
            return (T) cb;
        } else if (AbstractTextField.class.isAssignableFrom(fieldType)) {
            return (T) createAbstractTextField(
                    (Class) fieldType);
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    protected  T createAbstractTextField(
            Class fieldType) {
        if (fieldType == AbstractTextField.class) {
            fieldType = (Class) TextField.class;
        }
        try {
            T field = fieldType.newInstance();
            field.setImmediate(true);
            return field;
        } catch (Exception e) {
            throw new BindException(
                    "Could not create a field of type " + fieldType, e);
        }
    }

    /**
     * Fallback when no specific field has been created. Typically returns a
     * TextField.
     *
     * @param 
     *            The type of field to create
     * @param type
     *            The type of data that should be edited
     * @param fieldType
     *            The type of field to create
     * @return A field capable of editing the data or null if no field could be
     *         created
     */
    @SuppressWarnings("rawtypes")
    protected  T createDefaultField(Class type,
            Class fieldType) {
        if (fieldType.isAssignableFrom(TextField.class)) {
            return fieldType.cast(createAbstractTextField(TextField.class));
        }
        return null;
    }

    /**
     * Populates the given select with all the enums in the given {@link Enum}
     * class. Uses {@link Enum}.toString() for caption.
     *
     * @param select
     *            The select to populate
     * @param enumClass
     *            The Enum class to use
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected void populateWithEnumData(AbstractSelect select,
            Class enumClass) {
        select.removeAllItems();
        for (Object p : select.getContainerPropertyIds()) {
            select.removeContainerProperty(p);
        }
        select.addContainerProperty(CAPTION_PROPERTY_ID, String.class, "");
        select.setItemCaptionPropertyId(CAPTION_PROPERTY_ID);
        EnumSet enumSet = EnumSet.allOf(enumClass);
        for (Object r : enumSet) {
            Item newItem = select.addItem(r);
            newItem.getItemProperty(CAPTION_PROPERTY_ID).setValue(r.toString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy