Example usage for com.vaadin.ui CustomField CustomField

List of usage examples for com.vaadin.ui CustomField CustomField

Introduction

In this page you can find the example usage for com.vaadin.ui CustomField CustomField.

Prototype

public CustomField() 

Source Link

Document

Constructs a new custom field.

Usage

From source file:info.magnolia.security.app.dialog.field.WebAccessFieldFactory.java

License:Open Source License

@Override
protected Field<Object> createFieldComponent() {

    final VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(true);/*ww w . j a  va  2s.c o  m*/

    try {

        final JcrNodeAdapter roleItem = (JcrNodeAdapter) item;

        final VerticalLayout aclLayout = new VerticalLayout();

        final Label emptyLabel = new Label(i18n.translate("security.web.field.noAccess"));

        // Since JcrNewNodeAdapter.getJcrItem() returns the parent node we need to skip this step because we don't want to inspect the parent node
        if (!(roleItem instanceof JcrNewNodeAdapter)) {
            Node roleNode = roleItem.getJcrItem();
            if (roleNode.hasNode(ACL_NODE_NAME)) {

                final Node aclNode = roleNode.getNode(ACL_NODE_NAME);
                AbstractJcrNodeAdapter aclItem = new JcrNodeAdapter(aclNode);
                roleItem.addChild(aclItem);

                for (Node entryNode : NodeUtil.getNodes(aclNode)) {

                    AbstractJcrNodeAdapter entryItem = new JcrNodeAdapter(entryNode);
                    aclItem.addChild(entryItem);

                    Component ruleRow = createRuleRow(aclLayout, entryItem, emptyLabel);
                    aclLayout.addComponent(ruleRow);
                }
            }
        }

        if (aclLayout.getComponentCount() == 0) {
            aclLayout.addComponent(emptyLabel);
        }

        final HorizontalLayout buttons = new HorizontalLayout();
        final Button addButton = new Button(i18n.translate("security.web.field.addNew"));
        addButton.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {

                try {
                    AbstractJcrNodeAdapter aclItem = getOrAddAclItem(roleItem, ACL_NODE_NAME);
                    JcrNewNodeAdapter newItem = addAclEntryItem(aclItem);
                    Component ruleRow = createRuleRow(aclLayout, newItem, emptyLabel);
                    aclLayout.removeComponent(emptyLabel);
                    aclLayout.addComponent(ruleRow, aclLayout.getComponentCount() - 1);

                } catch (RepositoryException e) {
                    throw new RuntimeRepositoryException(e);
                }
            }
        });
        buttons.addComponent(addButton);
        aclLayout.addComponent(buttons);

        layout.addComponent(aclLayout);

    } catch (RepositoryException e) {
        throw new RuntimeRepositoryException(e);
    }

    return new CustomField<Object>() {

        @Override
        protected Component initContent() {
            return layout;
        }

        @Override
        public Class<?> getType() {
            return Object.class;
        }
    };
}

From source file:info.magnolia.security.app.dialog.field.WorkspaceAccessFieldFactory.java

License:Open Source License

@Override
protected Field<Object> createFieldComponent() {

    final String aclName = "acl_" + getFieldDefinition().getWorkspace();

    final VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(true);//www  . j  a  v a  2  s  .  c  o m

    try {

        final JcrNodeAdapter roleItem = (JcrNodeAdapter) item;

        final VerticalLayout aclLayout = new VerticalLayout();

        final Label emptyLabel = new Label(i18n.translate("security.workspace.field.noAccess"));

        // Since JcrNewNodeAdapter.getJcrItem() returns the parent node we need to skip this step because we don't want to inspect the parent node
        if (!(roleItem instanceof JcrNewNodeAdapter)) {
            Node roleNode = roleItem.getJcrItem();
            if (roleNode.hasNode(aclName)) {

                final Node aclNode = roleNode.getNode(aclName);

                AccessControlList acl = new AccessControlList();
                acl.readEntries(aclNode);

                AbstractJcrNodeAdapter aclItem = new JcrNodeAdapter(aclNode);
                roleItem.addChild(aclItem);

                aclItem.addItemProperty(INTERMEDIARY_FORMAT_PROPERTY_NAME,
                        new DefaultProperty<String>(String.class, "true"));

                final Set<AccessControlList.Entry> uniqueEntries = new HashSet<AccessControlList.Entry>();
                for (final Node aclEntryNode : NodeUtil.getNodes(aclNode)) {
                    AccessControlList.Entry entry = acl.getEntryByNode(aclEntryNode);
                    if (uniqueEntries.contains(entry)) {
                        continue;
                    }

                    uniqueEntries.add(entry);
                    long permissions = entry.getPermissions();
                    long accessType = entry.getAccessType();
                    String path = entry.getPath();

                    JcrNodeAdapter entryItem = new JcrNodeAdapter(aclEntryNode);
                    entryItem.addItemProperty(INTERMEDIARY_FORMAT_PROPERTY_NAME,
                            new DefaultProperty<String>(String.class, "true"));
                    final Property<Long> permissionsProperty = DefaultPropertyUtil
                            .newDefaultProperty(Long.class, null);
                    entryItem.addItemProperty(AccessControlList.PERMISSIONS_PROPERTY_NAME, permissionsProperty);
                    final Property<Long> accessProperty = DefaultPropertyUtil.newDefaultProperty(Long.class,
                            null);
                    entryItem.addItemProperty(ACCESS_TYPE_PROPERTY_NAME, accessProperty);
                    final Property<String> pathProperty = DefaultPropertyUtil.newDefaultProperty(String.class,
                            null);
                    entryItem.addItemProperty(AccessControlList.PATH_PROPERTY_NAME, pathProperty);

                    permissionsProperty.setValue(permissions);
                    accessProperty.setValue(accessType);
                    pathProperty.setValue(path);

                    aclItem.addChild(entryItem);

                    Component ruleRow = createRuleRow(aclLayout, entryItem, emptyLabel);
                    aclLayout.addComponent(ruleRow);
                }
            }
        }

        if (aclLayout.getComponentCount() == 0) {
            aclLayout.addComponent(emptyLabel);
        }

        final HorizontalLayout buttons = new HorizontalLayout();
        final Button addButton = new Button(i18n.translate("security.workspace.field.addButton"));
        addButton.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                try {

                    AbstractJcrNodeAdapter aclItem = getOrAddAclItem(roleItem, aclName);
                    if (aclItem.getItemProperty(INTERMEDIARY_FORMAT_PROPERTY_NAME) == null) {
                        aclItem.addItemProperty(INTERMEDIARY_FORMAT_PROPERTY_NAME,
                                new DefaultProperty<String>(String.class, "true"));
                    }

                    JcrNodeAdapter entryItem = addAclEntryItem(aclItem);
                    entryItem.addItemProperty(INTERMEDIARY_FORMAT_PROPERTY_NAME,
                            new DefaultProperty<String>(String.class, "true"));
                    entryItem.addItemProperty(AccessControlList.PERMISSIONS_PROPERTY_NAME,
                            new DefaultProperty<Long>(Long.class, Permission.ALL));
                    entryItem.addItemProperty(ACCESS_TYPE_PROPERTY_NAME, new DefaultProperty<Long>(Long.class,
                            AccessControlList.ACCESS_TYPE_NODE_AND_CHILDREN));
                    entryItem.addItemProperty(AccessControlList.PATH_PROPERTY_NAME,
                            new DefaultProperty<String>(String.class, ""));

                    Component ruleRow = createRuleRow(aclLayout, entryItem, emptyLabel);
                    aclLayout.removeComponent(emptyLabel);
                    aclLayout.addComponent(ruleRow, aclLayout.getComponentCount() - 1);
                } catch (RepositoryException e) {
                    throw new RuntimeRepositoryException(e);
                }
            }
        });
        buttons.addComponent(addButton);
        aclLayout.addComponent(buttons);

        layout.addComponent(aclLayout);

    } catch (RepositoryException e) {
        throw new RuntimeRepositoryException(e);
    }

    return new CustomField<Object>() {

        @Override
        protected Component initContent() {
            return layout;
        }

        @Override
        public Class<?> getType() {
            return Object.class;
        }
    };
}