com.freebox.engeneering.application.view.AbstractFieldGroup.java Source code

Java tutorial

Introduction

Here is the source code for com.freebox.engeneering.application.view.AbstractFieldGroup.java

Source

/*
 * Copyright 2012 Lexaden.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.freebox.engeneering.application.view;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.lexaden.webflow.EventProcessor;
import com.vaadin.data.fieldgroup.DefaultFieldGroupFieldFactory;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.server.UserError;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

/**
 * Basic vaadin form for all forms in the application. Contains basic functionality inherited by all forms
 *
 * @author Denis Skarbichev
 */
public abstract class AbstractFieldGroup<T extends AbstractLayout> extends FieldGroup {
    @Autowired
    protected EventProcessor eventProcessor;
    private T content;

    /**
     * Responsible for form initialization
     */
    public abstract void init();

    /**
     * Gets field group content.
     *
     * @return field group content
     */
    public T getContent() {
        return content;
    }

    /**
     * Sets field group content.
     *
     * @param content - field group content
     */
    public void setContent(T content) {
        this.content = content;
    }

    /**
     * Sets component error to field group layout
     *
     * @param localizedMessage - localized message
     */
    public void setComponentError(String localizedMessage) {
        getContent().setComponentError(new UserError(localizedMessage));
    }

    public class DefaultGroupFieldFactory extends DefaultFieldGroupFieldFactory {
        @Override
        public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
            Field field = super.createField(type, fieldType);
            if (field instanceof TextField) {
                final TextField textField = (TextField) field;
                textField.setNullRepresentation(StringUtils.EMPTY);
            }
            if (field instanceof TextArea) {
                final TextArea textField = (TextArea) field;
                textField.setNullRepresentation(StringUtils.EMPTY);
            }
            if (Date.class.isAssignableFrom(type)) {
                field = createDateField();
            }
            return (T) field;
        }

        protected <T extends Field> T createDateField() {
            DateField field = new DateField();
            field.setImmediate(true);
            return (T) field;
        }

    }

    /**
     * Discard changes and unbinds all bound fields
     */
    public void unbindAll() {
        final Collection<Field<?>> fields = new HashSet<Field<?>>(getFields());
        for (Field<?> field : fields) {
            unbind(field);
        }
    }
}