Binding bean into bean

Dear,

I got a bean object that is bind to a BeanFieldGroup. I use the bindMemberFields to bing my formlayout.


public class UserPermissionDialog extends Window implements Button.ClickListener, Window.CloseListener {

    private final static Log logger = LogFactory.getLog(UserFormPanel.class);

    final BeanFieldGroup<UserPermissionDTO> binder = new BeanFieldGroup<UserPermissionDTO>(UserPermissionDTO.class);

    private UserView userView;
    private UserPermissionForm userPermissionForm;

    public UserPermissionDialog(UserFormPermissionDTO userPermission, UserView userView) {
        super("Add Permission");
        this.userView = userView;
        setModal(true);

        setPositionX(200);
        setPositionY(100);

        userPermissionForm = new UserPermissionForm(userView);
        userPermissionForm.addButtonsClick(this);
        userPermissionForm.setCaption("Add permission for: " + userPermission.getUser().getName());
        binder.setItemDataSource(userPermission);
        binder.bindMemberFields(userPermissionForm);
        setContent(userPermissionForm);
    }

    public void buttonClick(Button.ClickEvent event) {

        Button source = event.getButton();
        if (source == userPermissionForm.getSaveButton()) {
            // Run our validators since if they are reporting problems, we'll fail to commit anyway.
            if (!binder.isValid()) {
                Notification.show("form.save.invalid");
                return;
            }
            try{
                binder.commit();
                for (UserViewListener listener : userView.getListener()) {
                    //listener.saveUser(binder.getItemDataSource().getBean());
                }

                Notification.show("Permissie voor gebruiker " + binder.getItemDataSource().getBean().getUser().getName() + " is succesvol opgeslagen in de Database.");
            } catch (ImesLdapException ldape) {
                Notification.show("Something went wrong while updating ldap");
            } catch (FieldGroup.CommitException e) {
                logger.error(e.getMessage());
            }
        } else if (source ==  userPermissionForm.getCancelButton()) {
            UserApplicationUI.getCurrent().removeWindow(this);
        }
    }

    @Override
    public void windowClose(Window.CloseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

public class UserPermissionForm extends FormLayout implements Property.ValueChangeListener {

    @PropertyId("application")
    private NativeSelect applicationSelect = new NativeSelect("Applicatie");
    @PropertyId("companySite.id")
    private NativeSelect companySiteSelect = new NativeSelect("Company site");
    @PropertyId("permission.id")
    private NativeSelect permissionSelect = new NativeSelect("Permissie");
    @PropertyId("startDate")
    private PopupDateField startDateField = new PopupDateField("Start date");
    @PropertyId("endDate")
    private PopupDateField endDateField = new PopupDateField("End date");

    private Button saveButton = new Button();
    private Button cancelButton = new Button();
    private UserView userView;

    public UserPermissionForm(UserView userView) {
        this.userView = userView;

        List<CompanySiteDTO> companySites = new ArrayList<CompanySiteDTO>();
        for(UserViewListener uvl : userView.getListener()) {
            companySites.addAll(uvl.getAllCompanySites());
        }

        for (CompanySiteDTO c : companySites) {
            companySiteSelect.addItem(c.getId());
            companySiteSelect.setItemCaption(c.getId(), c.getName());
        }

        companySiteSelect.setImmediate(true);

        List<ApplicationDTO> applications = new ArrayList<ApplicationDTO>();
        for(UserViewListener uvl : userView.getListener()) {
            applications.addAll(uvl.getAllApplications());
        }

        for (ApplicationDTO a : applications) {
            applicationSelect.addItem(a.getId());
            applicationSelect.setItemCaption(a.getId(), a.getName());
        }

        applicationSelect.setImmediate(true);
        applicationSelect.addValueChangeListener(this);

        startDateField.setImmediate(true);
        startDateField.setTimeZone(TimeZone.getTimeZone("UTC"));
        startDateField.setLocale(UserApplicationUI.getCurrent().getSession().getLocale());
        startDateField.setResolution(Resolution.MINUTE);

        endDateField.setImmediate(true);
        endDateField.setTimeZone(TimeZone.getTimeZone("UTC"));
        endDateField.setLocale(UserApplicationUI.getCurrent().getSession().getLocale());
        endDateField.setResolution(Resolution.MINUTE);

        addComponent(companySiteSelect);
        addComponent(applicationSelect);
        addComponent(permissionSelect);
        addComponent(startDateField);
        addComponent(endDateField);

        addStyleName("bordered"); // Custom style
        setWidth("420px");

    }

    private void updatePermissionSelect(ApplicationDTO application) {
        List<PermissionDTO> permissions = new ArrayList<PermissionDTO>();
        for(UserViewListener uvl : userView.getListener()) {
            permissions.addAll(uvl.getPermissionsByApplication(application));
        }
        permissionSelect.removeAllItems();
        for (PermissionDTO p : permissions) {
            permissionSelect.addItem(p.getId());
            permissionSelect.setItemCaption(p.getId(), p.getName());
        }
    }

    public void addButtonsClick(Button.ClickListener listener) {
        saveButton = new Button("save", listener);
        addComponent(saveButton);
        cancelButton = new Button("cancel", listener);
        addComponent(cancelButton);
    }

    public Button getSaveButton() {
        return saveButton;
    }

    public Button getCancelButton() {
        return cancelButton;
    }

    @Override
    public void valueChange(Property.ValueChangeEvent event) {
        if(((Field.ValueChangeEvent) event).getSource().equals(applicationSelect)) {
            updatePermissionSelect((ApplicationDTO) event.getProperty().getValue());
        }
    }

}

Everything works fine when i want to bind Primitive types to my form. But when i want to bind my NativeSelect object with a PropertyId annotation
(i like to put my object in it, not only the id) then i failes.

Is it possible to just say to my formlayout

@PropertyId(“application”)
private NativeSelect applicationSelect = new NativeSelect(“Applicatie”);

and on commit put the object value into my bean?

Or do i have to work with some sort of parsers?

Grtz,

E.

I found my problem. I tried to bind a bean that was null to the nested propertie.
When you create a new form with a new bean object with neste beans you have to call the constructur of this nestedbean before you add this obect to the beanfieldgroup.

Then you can set trough a select the entire bean object.