/*
 * Copyright (c) 2015 com.haulmont.ts.gui.entities.project
 */
package com.haulmont.ts.gui.entities.project;

import com.haulmont.cuba.gui.DialogParams;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
import com.haulmont.ts.entity.Client;
import com.haulmont.ts.entity.Project;
import com.haulmont.ts.entity.ProjectStatus;
import com.haulmont.ts.service.ProjectsService;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author gorelov
 */
public class ProjectEdit extends AbstractEditor<Project> {
    @Inject
    private FieldGroup fieldGroup;
    @Inject
    private ComponentsFactory componentsFactory;
    @Inject
    private CollectionDatasource<Project, UUID> projectsDs;
    @Inject
    private ProjectsService projectsService;

    @Named("fieldGroup.parent")
    private LookupPickerField parentField;
    @Named("fieldGroup.client")
    private LookupPickerField clientField;

    private List<Project> childrenProjects;

    @Override
    public void init(final Map<String, Object> params) {

        parentField.addAction(createLookupAction(parentField));
        parentField.addAction(new PickerField.ClearAction(parentField));

        clientField.addAction(createLookupAction(clientField));
        clientField.addAction(new PickerField.ClearAction(clientField));

        parentField.addListener(new ValueListener() {
            @Override
            public void valueChanged(Object source, String property, Object prevValue, Object value) {
                clientField.setEnabled(value == null);
                if (value != null) {
                    Project parent = (Project) value;
                    if (!parent.getClient().equals(getItem().getClient()))
                        clientField.setValue(parent.getClient());
                } else if (prevValue != null) {
                    clientField.setValue(null);
                }
            }
        });

        clientField.addListener(new ValueListener() {
            @Override
            public void valueChanged(Object source, String property, Object prevValue, Object value) {
                if (childrenProjects != null && value != null) {
                    Client client = (Client) value;
                    for (Project child : childrenProjects)
                        projectsService.setClient(child, client);
                }
            }
        });

        fieldGroup.addCustomField("description", new FieldGroup.CustomFieldGenerator() {
            @Override
            public Component generateField(Datasource datasource, String propertyId) {
                ResizableTextArea textArea = componentsFactory.createComponent(ResizableTextArea.NAME);
                textArea.setHeight("100px");
                textArea.setResizable(true);
                return textArea;
            }
        });
    }

    @Override
    protected void postInit() {
        Project project = getItem();
        projectsDs.excludeItem(project);
        childrenProjects = projectsService.getChildren(project);
        for (Project child : childrenProjects)
            projectsDs.excludeItem(child);
        project.setStatus(ProjectStatus.OPEN);
    }

    private PickerField.LookupAction createLookupAction(PickerField pickerField) {
        PickerField.LookupAction lookupAction = new PickerField.LookupAction(pickerField);
        lookupAction.setLookupScreenOpenType(WindowManager.OpenType.DIALOG);
        lookupAction.setLookupScreenDialogParams(new DialogParams()
                .setHeight(480)
                .setWidth(640)
                .setResizable(true));
        return lookupAction;
    }
}
@@ -106,16 +106,61 @@
 oject;%0A%0A
+import com.haulmont.cuba.core.entity.Entity;%0A
 import c
@@ -194,16 +194,16 @@
 Params;%0A
-
 import c
@@ -281,16 +281,140 @@
 ents.*;%0A
+import com.haulmont.cuba.gui.components.actions.CreateAction;%0Aimport com.haulmont.cuba.gui.components.actions.RemoveAction;%0A
 import c
@@ -862,57 +862,9 @@
 til.
-List;%0Aimport java.util.Map;%0Aimport java.util.UUID
+*
 ;%0A%0A/
@@ -1184,16 +1184,65 @@
 Service;
+%0A    @Inject%0A    private Table participantsTable;
 %0A%0A    @N
@@ -1384,16 +1384,105 @@
 ntField;
+%0A    @Named(%22participantsTable.create%22)%0A    private CreateAction participantsTableCreate;
 %0A%0A    pr
@@ -3391,16 +3391,629 @@
      %7D);
+%0A%0A        participantsTableCreate.setOpenType(WindowManager.OpenType.DIALOG);%0A%0A        participantsTable.addGeneratedColumn(%22remove%22, new Table.ColumnGenerator() %7B%0A            @Override%0A            public Component generateCell(Entity entity) %7B%0A                LinkButton removeButton = componentsFactory.createComponent(LinkButton.NAME);%0A                removeButton.setIcon(%22icons/remove.png%22);%0A                removeButton.setAction(new ParticipantRemoveAction(participantsTable, entity));%0A                return removeButton;%0A            %7D%0A        %7D);%0A        participantsTable.setColumnCaption(%22remove%22, %22%22);
 %0A    %7D%0A%0A
@@ -4761,24 +4761,24 @@
 ble(true));%0A
-
         retu
@@ -4799,10 +4799,639 @@
 n;%0A    %7D
+%0A%0A    private class ParticipantRemoveAction extends RemoveAction %7B%0A%0A        private Entity entity;%0A%0A        public ParticipantRemoveAction(ListComponent target, Entity entity) %7B%0A            super(target);%0A            this.entity = entity;%0A        %7D%0A%0A        @Override%0A        public void actionPerform(Component component) %7B%0A            if (!isEnabled()) %7B%0A                return;%0A            %7D%0A%0A            Set%3CEntity%3E selected = new HashSet%3C%3E(1);%0A            selected.add(entity);%0A            confirmAndRemove(selected);%0A        %7D%0A%0A        @Override%0A        public String getCaption() %7B%0A            return null;%0A        %7D%0A    %7D
 %0A%7D

Remove button for participant table