sample.ui.view.PersonEditor.java Source code

Java tutorial

Introduction

Here is the source code for sample.ui.view.PersonEditor.java

Source

/**
 * Copyright 2009-2013 Oy Vaadin Ltd
 *
 * 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 sample.ui.view;

import java.io.Serializable;
import java.lang.reflect.Method;

import javax.inject.Inject;

import com.vaadin.data.Item;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.DefaultFieldGroupFieldFactory;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.FieldGroupFieldFactory;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

import sample.model.Department;
import sample.model.Person;

@SuppressWarnings("serial")
public class PersonEditor extends Window implements FieldGroupFieldFactory {

    @Inject
    private DepartmentSelector departmentSelector;

    private Person person;

    private HorizontalLayout contentArea;

    private Component currentComp;

    public PersonEditor() {
        contentArea = new HorizontalLayout();
        currentComp = new Label();
        contentArea.addComponent(currentComp);
        setContent(contentArea);
    }

    public void initContent(Person person) {
        this.person = person;
        FormLayout formLayout = getLayout();
        getContentArea().replaceComponent(currentComp, formLayout);
        currentComp = formLayout;
        setSizeUndefined();
        setCaption(buildCaption());
    }

    public HorizontalLayout getContentArea() {
        return contentArea;
    }

    public FormLayout getLayout() {
        //      this.departmentSelector = departmentSelector;
        //      this.person = person;
        this.setClosable(true);

        FormLayout layout = new FormLayout();
        // Form for editing the bean
        final BeanFieldGroup<Person> binder = new BeanFieldGroup<Person>(Person.class);
        binder.setItemDataSource(person);
        binder.setFieldFactory(this);
        layout.addComponent(binder.buildAndBind("Name", "firstName"));
        layout.addComponent(binder.buildAndBind("Last name", "lastName"));
        layout.addComponent(binder.buildAndBind("Street", "street"));
        layout.addComponent(binder.buildAndBind("City", "city"));
        layout.addComponent(binder.buildAndBind("Zip Code", "zipCode"));
        layout.addComponent(binder.buildAndBind("Phone", "phoneNumber"));
        layout.addComponent(binder.buildAndBind("Departman", "department"));

        // Buffer the form content
        binder.setBuffered(true);
        // A button to commit the buffer
        layout.addComponent(new Button("OK", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    binder.commit();
                    fireEvent(new Event(PersonEditor.this));
                    Notification.show("Thanks!");
                } catch (CommitException e) {
                    Notification.show("You fail!");
                }
                close();
            }
        }));

        // A button to discard the buffer
        layout.addComponent(new Button("Discard", new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                binder.discard();
                Notification.show("Discarded!");
                close();
            }
        }));

        return layout;
    }

    /**
     * @return the caption of the editor window
     */
    private String buildCaption() {
        return String.format("%s %s", person.getFirstName(), person.getLastName());
        //      return String.format("%s %s", personBeanItem.getItemProperty("firstName").getValue(),
        //            personBeanItem.getItemProperty("lastName").getValue());
    }

    @Override
    public <T extends Field> T createField(Class<?> dataType, Class<T> fieldType) {
        T field = DefaultFieldGroupFieldFactory.get().createField(dataType, fieldType);
        System.out.println("datatype >> " + dataType.getName() + " : fieldtype >> " + fieldType.getName());

        if (dataType == Department.class) { // "department".equals(field.getValue())) {
            field = (T) departmentSelector; // new DepartmentSelector();
        } else if (field instanceof TextField) {
            ((TextField) field).setNullRepresentation("");
        }

        //        field.addValidator(new BeanValidator(Person.class, field.getId()));

        return field;
    }

    public void addListener(EditorSavedListener listener) {
        try {
            Method method = EditorSavedListener.class.getDeclaredMethod("editorSaved",
                    new Class[] { EditorSavedEvent.class });
            addListener(EditorSavedEvent.class, listener, method);
        } catch (final java.lang.NoSuchMethodException e) {
            // This should never happen
            throw new java.lang.RuntimeException("Internal error, editor saved method not found");
        }
    }

    public void removeListener(EditorSavedListener listener) {
        removeListener(EditorSavedEvent.class, listener);
    }

    public static class EditorSavedEvent extends Component.Event {

        private Item savedItem;

        public EditorSavedEvent(Component source, Item savedItem) {
            super(source);
            this.savedItem = savedItem;
        }

        public Item getSavedItem() {
            return savedItem;
        }
    }

    public interface EditorSavedListener extends Serializable {
        public void editorSaved(EditorSavedEvent event);
    }

}