/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: Konstantin Krivopustov
 * Created: 29.01.2009 15:08:16
 *
 * $Id$
 */
package com.haulmont.cuba.web.app.ui.security.group.browse;

import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.security.entity.Group;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.cuba.core.entity.Entity;

import java.util.*;

public class GroupBrowser extends AbstractWindow
{
    public GroupBrowser(Window frame) {
        super(frame);
    }

    protected void init(Map<String, Object> params) {
        final Tree tree = getComponent("groups");

        final CollectionDatasource treeDS = tree.getDatasource();
        treeDS.refresh();
        tree.expandTree();

        final Collection itemIds = treeDS.getItemIds();
        if (!itemIds.isEmpty()) {
            tree.setSelected(treeDS.getItem(itemIds.iterator().next()));
        }

        final TreeActionsHelper helper = new TreeActionsHelper(this, tree);
        helper.createCreateAction(WindowManager.OpenType.DIALOG);
        helper.createEditAction(WindowManager.OpenType.DIALOG);
        helper.createRemoveAction();

        final Table users = getComponent("users");
        Table constraints = getComponent("constraints");

        final TableActionsHelper usersActions = new TableActionsHelper(this, users);
        usersActions.createCreateAction(new ValueProvider() {
            public Map<String, Object> getValues() {
                final Map<String, Object> map = new HashMap<String, Object>();
                map.put("group", tree.getSelected());
                return map;
            }

            public Map<String, Object> getParameters() {
                return Collections.emptyMap();
            }
        });
        usersActions.createEditAction();
        users.addAction(new AbstractAction("moveToGroup") {
            public String getCaption() {
                return getMessage("users.moveToGroup");
            }

            public boolean isEnabled() {
                return true;
            }

            public void actionPerform(Component component) {
                final Set<User> selected = users.getSelected();
                if (!selected.isEmpty()) {
                    openLookup("sec$Group.lookup", new Lookup.Handler() {
                        public void handleLookup(Collection items) {
                            if (items.size() == 1) {
                                Group group = (Group) items.iterator().next();
                                for (User user : selected) {
                                    user.setGroup(group);
                                }
                                
                                final CollectionDatasource ds = users.getDatasource();
                                ds.commit();
                                ds.refresh();
                                users.setSelected((Entity) null);
                            }
                        }
                    }, WindowManager.OpenType.DIALOG);
                }
            }
        });
        usersActions.createRefreshAction();

        final TableActionsHelper constraintsActions = new TableActionsHelper(this, constraints);
        constraintsActions.createCreateAction(new ValueProvider() {
            public Map<String, Object> getValues() {
                final Map<String, Object> map = new HashMap<String, Object>();
                map.put("group", tree.getSelected());
                return map;
            }

            public Map<String, Object> getParameters() {
                return Collections.emptyMap();
            }
        });
        constraintsActions.createEditAction();
        constraintsActions.createRemoveAction();
        constraintsActions.createRefreshAction();
    }
}

@@ -670,18 +670,17 @@
 ctWindow
-%0D%0A
+ 
 %7B%0D%0A    p
@@ -2071,32 +2071,96 @@
 eEditAction();%0D%0A
+        usersActions.addListener(new EditUserListener(users));%0D%0A
         users.ad
@@ -3019,42 +3019,10 @@
  %7D%0D%0A
-                                
 %0D%0A
+
     
@@ -4099,32 +4099,32 @@
 emoveAction();%0D%0A
-
         constrai
@@ -4164,12 +4164,566 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    private class EditUserListener implements TableActionsHelper.Listener %7B%0D%0A%0D%0A        private Table users;%0D%0A%0D%0A        private EditUserListener(Table users) %7B%0D%0A            this.users = users;%0D%0A        %7D%0D%0A%0D%0A        public void entityCreated(Entity entity) %7B%0D%0A        %7D%0D%0A%0D%0A        public void entityEdited(Entity entity) %7B%0D%0A            final CollectionDatasource ds = users.getDatasource();%0D%0A            ds.refresh();%0D%0A            users.setSelected((Entity) null);%0D%0A        %7D%0D%0A%0D%0A        public void entityRemoved(Set%3CEntity%3E entity) %7B%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

move user between group