/*
 * Copyright (c) 2015 com.haulmont.ts.gui.projectparticipant
 */
package com.haulmont.timesheets.gui.projectparticipant;

import com.haulmont.cuba.gui.DialogParams;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.haulmont.cuba.gui.components.LookupPickerField;
import com.haulmont.cuba.gui.components.PickerField;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.timesheets.entity.Project;
import com.haulmont.timesheets.entity.ProjectParticipant;

import javax.inject.Named;
import java.util.*;

/**
 * @author gorelov
 */
public class ProjectParticipantEdit extends AbstractEditor<ProjectParticipant> {

    @Named("fieldGroup.user")
    protected LookupPickerField userField;
    @Named("fieldGroup.project")
    protected PickerField projectField;

    @Override
    public void init(Map<String, Object> params) {
        userField.addAction(createLookupAction(userField));
        userField.addAction(new PickerField.ClearAction(userField));

        final UniqueUserValidator validator = new UniqueUserValidator();
        userField.addValidator(validator);

        @SuppressWarnings("unchecked")
        Collection<User> assignedUsers = (Collection<User>) params.get("assignedUsers");
        if (assignedUsers != null)
            validator.setAssignedUsers(assignedUsers);

        projectField.addListener(new ValueListener() {
            @Override
            public void valueChanged(Object source, String property, Object prevValue, Object value) {
                if (value != null) {
                    if (!value.equals(prevValue)) {
                        Project project = (Project) value;
                        validator.setAssignedUsers(getAssignedUsers(project));
                    }
                } else {
                    validator.setAssignedUsers(null);
                }
            }
        });
    }

    @Override
    protected void postInit() {
        super.postInit();
        ProjectParticipant participant = getItem();
        if (participant.getProject() != null) {
            projectField.setEnabled(false);
        }
    }

    protected PickerField.LookupAction createLookupAction(PickerField pickerField) {
        PickerField.LookupAction lookupAction = new PickerField.LookupAction(pickerField);
        lookupAction.setLookupScreenOpenType(WindowManager.OpenType.DIALOG);
        lookupAction.setLookupScreenDialogParams(new DialogParams()
                .setWidth(800)
                .setHeight(500)
                .setResizable(true));
        return lookupAction;
    }

    protected Collection<User> getAssignedUsers(Project project) {
        Set<ProjectParticipant> participants = project.getParticipants();
        if (!participants.isEmpty()) {
            List<User> assignedUsers = new ArrayList<>(participants.size());
            for (ProjectParticipant participant : participants) {
                assignedUsers.add(participant.getUser());
            }
            return assignedUsers;
        }
        return Collections.emptyList();
    }
}
@@ -365,32 +365,132 @@
 ts.PickerField;%0A
+import com.haulmont.cuba.gui.data.Datasource;%0Aimport com.haulmont.cuba.gui.data.DatasourceListener;%0A
 import com.haulm
@@ -514,32 +514,32 @@
 .ValueListener;%0A
-
 import com.haulm
@@ -671,24 +671,52 @@
 rticipant;%0A%0A
+import javax.inject.Inject;%0A
 import javax
@@ -860,16 +860,94 @@
 ant%3E %7B%0A%0A
+    @Inject%0A    private Datasource%3CProjectParticipant%3E projectParticipantDs;%0A%0A
     @Nam
@@ -1612,24 +1612,606 @@
 nedUsers);%0A%0A
+        projectParticipantDs.addListener(new DatasourceListener%3CProjectParticipant%3E() %7B%0A            @Override%0A            public void itemChanged(Datasource%3CProjectParticipant%3E ds, ProjectParticipant prevItem, ProjectParticipant item) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void stateChanged(Datasource%3CProjectParticipant%3E ds, Datasource.State prevState, Datasource.State state) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void valueChanged(ProjectParticipant source, String property, Object prevValue, Object value) %7B%0A%0A            %7D%0A        %7D);%0A%0A
         proj
@@ -3585,24 +3585,24 @@
 icipants();%0A
-
         if (
@@ -3601,16 +3601,40 @@
     if (
+participants != null && 
 !partici

Fix NullPointerException in unsaved Project