Example usage for com.vaadin.event Action.Handler Action.Handler

List of usage examples for com.vaadin.event Action.Handler Action.Handler

Introduction

In this page you can find the example usage for com.vaadin.event Action.Handler Action.Handler.

Prototype

Action.Handler

Source Link

Usage

From source file:com.garyclayburg.vconsole.VConsole.java

License:Open Source License

private Table createUserTable(BeanContainer<String, User> userBeanContainer) {
    final Collection[] selectedRows = new Collection[1];
    final Table userTable = new Table();
    userTable.setSizeFull();/* w  w w  . ja  v  a 2  s  .  co  m*/
    userTable.setSelectable(true);
    userTable.setMultiSelect(false);
    userTable.setImmediate(true);
    userTable.setContainerDataSource(userBeanContainer);
    userTable.addItemClickListener(new ItemClickEvent.ItemClickListener() {
        @Override
        public void itemClick(ItemClickEvent event) {
            Collection<?> itemPropertyIds = event.getItem().getItemPropertyIds();
            log.info("properties clicked: " + itemPropertyIds);
            log.info("multiple select? " + selectedRows[0]);
        }
    });
    userTable.addValueChangeListener(new Property.ValueChangeListener() {
        @Override
        public void valueChange(Property.ValueChangeEvent event) {
            //                selectedRows[0] = (Collection) userTable.getValue();
            log.info("selected: " + userTable.getValue());
        }
    });

    userTable.addActionHandler(new Action.Handler() {
        @Override
        public Action[] getActions(Object target, Object sender) {
            Table selectedUserTable = (Table) sender;
            Item item = selectedUserTable.getItem(selectedUserTable.getValue());
            Action[] actions = new Action[0];
            if (item instanceof BeanItem) {
                log.debug("create right-click menu items");
                //                    if (target == null){ //create actions for item user clicked on
                Item targetItem = selectedUserTable.getItem(target);
                if (targetItem != null) {
                    User targetUser = (User) ((BeanItem) targetItem).getBean();

                    if (targetUser != null) {
                        Set<String> allEntitledTargets = attributeService.getEntitledTargets(targetUser);
                        Set<Action> entitledTargetActions = new HashSet<>();

                        for (String targetName : allEntitledTargets) {
                            final Action action = new Action(targetName);
                            entitledTargetActions.add(action);
                        }
                        actions = entitledTargetActions.toArray(new Action[entitledTargetActions.size()]);
                        log.debug("right-click actions for user {}: {}", target, entitledTargetActions);
                    }
                }
            } else {
                log.debug("Cannot create right-click menu items");
            }

            return actions;
        }

        @Override
        public void handleAction(Action action, Object sender, Object target) {
            if (action != null) {
                Table selectedUserTable = (Table) sender;
                selectedUserTable.getValue();
                Item item = selectedUserTable.getItem(selectedUserTable.getValue());

                if (item instanceof BeanItem) {
                    log.debug("create target window");
                    User user = (User) ((BeanItem) item).getBean();
                    targetWindows.showTargetWindow(user, action.getCaption());
                } else {
                    log.debug("Cannot create window");
                }
            } else {
                Window multiWindow = new Window("multi-user myAD attributes");
                UI.getCurrent().addWindow(multiWindow);
                Label stuff = new Label("2 users here " + selectedRows[0] + " ");
                multiWindow.setContent(stuff);
            }
        }
    });
    userTable.setVisibleColumns("firstname", "lastname");
    return userTable;
}