Search in sources :

Example 6 with FormItem

use of org.jboss.hal.ballroom.form.FormItem in project console by hal.

the class HandlerView method updateHostRefs.

@SuppressWarnings("ConstantConditions")
private void updateHostRefs(final Table<NamedNode> table, final Form<NamedNode> form) {
    FormItem<String> formItem = form.getFormItem(HOSTS);
    if (formItem != null) {
        if (table.hasSelection()) {
            ResourceAddress address = HOST_TEMPLATE.append(LOCATION + "=*").resolve(mbuiContext.statementContext());
            Operation operation = new Operation.Builder(address, READ_RESOURCE_OPERATION).build();
            mbuiContext.dispatcher().execute(operation, result -> {
                String hosts = result.asList().stream().filter(node -> table.selectedRow().getName().equals(node.get(RESULT).get(HANDLER).asString())).map(node -> {
                    ResourceAddress adr = new ResourceAddress(node.get(ADDRESS));
                    ResourceAddress host = adr.getParent();
                    ResourceAddress server = host.getParent();
                    return server.lastValue() + "/" + host.lastValue();
                }).distinct().sorted().collect(Collectors.joining(", "));
                formItem.setValue(hosts);
            }, (op1, failure) -> formItem.clearValue());
        } else {
            formItem.clearValue();
        }
    }
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) HOST_TEMPLATE(org.jboss.hal.client.configuration.subsystem.undertow.AddressTemplates.HOST_TEMPLATE) Table(org.jboss.hal.ballroom.table.Table) Operation(org.jboss.hal.dmr.Operation) MbuiViewImpl(org.jboss.hal.core.mbui.MbuiViewImpl) Collectors(java.util.stream.Collectors) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) VerticalNavigation(org.jboss.hal.ballroom.VerticalNavigation) ModelNodeHelper.failSafePropertyList(org.jboss.hal.dmr.ModelNodeHelper.failSafePropertyList) FormItem(org.jboss.hal.ballroom.form.FormItem) MbuiContext(org.jboss.hal.core.mbui.MbuiContext) MbuiElement(org.jboss.hal.spi.MbuiElement) ModelDescriptionConstants(org.jboss.hal.dmr.ModelDescriptionConstants) PostConstruct(javax.annotation.PostConstruct) MbuiView(org.jboss.hal.spi.MbuiView) NamedNode(org.jboss.hal.dmr.NamedNode) Form(org.jboss.hal.ballroom.form.Form) ModelNodeHelper.asNamedNodes(org.jboss.hal.dmr.ModelNodeHelper.asNamedNodes) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) Operation(org.jboss.hal.dmr.Operation)

Example 7 with FormItem

use of org.jboss.hal.ballroom.form.FormItem in project console by hal.

the class RemotingView method updateGenericConnector.

@SuppressWarnings("HardCodedStringLiteral")
private void updateGenericConnector(NamedNode node, Form<NamedNode> form, Form<ModelNode> securityForm, Form<ModelNode> policyForm) {
    FormItem<Object> properties = form.getFormItem(PROPERTY);
    FormItem<Object> securityProperties = securityForm.getFormItem(PROPERTY);
    if (node != null) {
        Map<String, String> p = failSafePropertyList(node, PROPERTY).stream().collect(toMap(Property::getName, property -> property.getValue().get(VALUE).asString()));
        ModelNode security = failSafeGet(node, "security/sasl");
        Map<String, String> sp = failSafePropertyList(node, "security/sasl/" + PROPERTY).stream().collect(toMap(Property::getName, property -> property.getValue().get(VALUE).asString()));
        ModelNode policy = failSafeGet(node, "security/sasl/sasl-policy/policy");
        form.view(node);
        properties.setValue(p);
        securityForm.view(security);
        securityProperties.setValue(sp);
        policyForm.view(policy);
    } else {
        form.clear();
        properties.clearValue();
        securityForm.clear();
        securityProperties.clearValue();
        policyForm.clear();
    }
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) MbuiViewImpl(org.jboss.hal.core.mbui.MbuiViewImpl) Tabs(org.jboss.hal.ballroom.Tabs) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm) Property(org.jboss.hal.dmr.Property) PROPERTY(org.jboss.hal.dmr.ModelDescriptionConstants.PROPERTY) Collectors.toMap(java.util.stream.Collectors.toMap) MbuiElement(org.jboss.hal.spi.MbuiElement) VALUE(org.jboss.hal.dmr.ModelDescriptionConstants.VALUE) Map(java.util.Map) READ_RESOURCE_OPERATION(org.jboss.hal.dmr.ModelDescriptionConstants.READ_RESOURCE_OPERATION) Metadata(org.jboss.hal.meta.Metadata) Names(org.jboss.hal.resources.Names) Nullable(javax.annotation.Nullable) ModelNodeHelper.asNamedNodes(org.jboss.hal.dmr.ModelNodeHelper.asNamedNodes) Table(org.jboss.hal.ballroom.table.Table) Operation(org.jboss.hal.dmr.Operation) AddressTemplates(org.jboss.hal.client.configuration.subsystem.remoting.AddressTemplates) Ids(org.jboss.hal.resources.Ids) VerticalNavigation(org.jboss.hal.ballroom.VerticalNavigation) ModelNodeHelper.failSafeGet(org.jboss.hal.dmr.ModelNodeHelper.failSafeGet) ModelNodeHelper.failSafePropertyList(org.jboss.hal.dmr.ModelNodeHelper.failSafePropertyList) FormItem(org.jboss.hal.ballroom.form.FormItem) PropertiesItem(org.jboss.hal.ballroom.form.PropertiesItem) MbuiContext(org.jboss.hal.core.mbui.MbuiContext) PostConstruct(javax.annotation.PostConstruct) MbuiView(org.jboss.hal.spi.MbuiView) NamedNode(org.jboss.hal.dmr.NamedNode) Form(org.jboss.hal.ballroom.form.Form) ModelNode(org.jboss.hal.dmr.ModelNode)

Example 8 with FormItem

use of org.jboss.hal.ballroom.form.FormItem in project console by hal.

the class ExactlyOneAlternativeValidation method validate.

@Override
public ValidationResult validate(final Form<T> form) {
    LabelBuilder labelBuilder = new LabelBuilder();
    Set<String> emptyItems = requiredAlternatives.stream().filter(name -> {
        FormItem formItem = form.getFormItem(name);
        boolean empty = formItem != null && formItem.isEmpty();
        // there is a special case for SwitchItem of Boolean type, the SwitchItem.isEmpty() tests if the value is
        // null, but for this validation case we must ensure the value is set to false to allow the validation
        // to work
        boolean switchItemFalse = false;
        if (formItem != null && formItem.getClass().equals(SwitchItem.class)) {
            Object value = formItem.getValue();
            switchItemFalse = value != null && !Boolean.parseBoolean(value.toString());
        }
        return empty || switchItemFalse;
    }).collect(toSet());
    if (requiredAlternatives.size() == emptyItems.size()) {
        // show an error on each related form item
        requiredAlternatives.forEach(alternative -> {
            FormItem<Object> formItem = form.getFormItem(alternative);
            if (formItem.isEmpty()) {
                formItem.showError(messages.exactlyOneAlternativeError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
            }
        });
        // return overall result
        return ValidationResult.invalid(messages.exactlyOneAlternativesError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
    } else {
        return ValidationResult.OK;
    }
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) Iterables(com.google.common.collect.Iterables) SortedSet(java.util.SortedSet) Messages(org.jboss.hal.resources.Messages) Set(java.util.Set) TreeSet(java.util.TreeSet) FormValidation(org.jboss.hal.ballroom.form.FormValidation) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder) FormItem(org.jboss.hal.ballroom.form.FormItem) SwitchItem(org.jboss.hal.ballroom.form.SwitchItem) Constants(org.jboss.hal.resources.Constants) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) Collectors.toSet(java.util.stream.Collectors.toSet) Form(org.jboss.hal.ballroom.form.Form) FormItem(org.jboss.hal.ballroom.form.FormItem) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder)

Example 9 with FormItem

use of org.jboss.hal.ballroom.form.FormItem in project console by hal.

the class ModelNodeForm method prepare.

@Override
protected void prepare(State state) {
    super.prepare(state);
    SecurityContext securityContext = metadata.getSecurityContext();
    switch(state) {
        case EMPTY:
            ElementGuard.processElements(AuthorisationDecision.from(Core.INSTANCE.environment(), securityContext), element());
            break;
        case READONLY:
        case EDITING:
            // change restricted and enabled state
            for (FormItem formItem : getBoundFormItems()) {
                String name = formItem.getName();
                int pos = name.indexOf('.');
                if (pos > 0) {
                    name = name.substring(0, pos);
                }
                formItem.setRestricted(!securityContext.isReadable(name));
                // don't touch disabled form items
                if (formItem.isEnabled()) {
                    formItem.setEnabled(securityContext.isWritable(name));
                }
            }
            break;
        default:
            break;
    }
    // adjust form links in any case
    if (!securityContext.isWritable()) {
        formLinks.setVisible(Operation.EDIT, false);
        formLinks.setVisible(Operation.RESET, false);
        formLinks.setVisible(Operation.REMOVE, false);
    }
}
Also used : FormItem(org.jboss.hal.ballroom.form.FormItem) SecurityContext(org.jboss.hal.meta.security.SecurityContext) Constraint(org.jboss.hal.meta.security.Constraint)

Example 10 with FormItem

use of org.jboss.hal.ballroom.form.FormItem in project console by hal.

the class DefaultFormItemProvider method createFrom.

@Override
public FormItem<?> createFrom(Property property) {
    FormItem<?> formItem = null;
    String name = property.getName();
    String label = labelBuilder.label(property);
    ModelNode attributeDescription = property.getValue();
    // don't use 'required' here!
    boolean required = attributeDescription.hasDefined(NILLABLE) && !attributeDescription.get(NILLABLE).asBoolean();
    boolean expressionAllowed = attributeDescription.hasDefined(EXPRESSIONS_ALLOWED) && attributeDescription.get(EXPRESSIONS_ALLOWED).asBoolean();
    boolean readOnly = attributeDescription.hasDefined(ACCESS_TYPE) && (READ_ONLY.equals(attributeDescription.get(ACCESS_TYPE).asString()) || METRIC.equals(attributeDescription.get(ACCESS_TYPE).asString()));
    String unit = attributeDescription.hasDefined(UNIT) ? attributeDescription.get(UNIT).asString() : null;
    Deprecation deprecation = attributeDescription.hasDefined(DEPRECATED) ? new Deprecation(attributeDescription.get(DEPRECATED)) : null;
    if (attributeDescription.hasDefined(TYPE)) {
        ModelType type = attributeDescription.get(TYPE).asType();
        ModelType valueType = (attributeDescription.has(VALUE_TYPE) && attributeDescription.get(VALUE_TYPE).getType() != ModelType.OBJECT) ? ModelType.valueOf(attributeDescription.get(VALUE_TYPE).asString()) : null;
        switch(type) {
            case BOOLEAN:
                {
                    SwitchItem switchItem = new SwitchItem(name, label);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        switchItem.assignDefaultValue(attributeDescription.get(DEFAULT).asBoolean());
                    }
                    formItem = switchItem;
                    break;
                }
            case BIG_INTEGER:
            case INT:
            case LONG:
                {
                    long min, max;
                    if (type == ModelType.INT) {
                        min = attributeDescription.get(MIN).asLong(Integer.MIN_VALUE);
                        max = attributeDescription.get(MAX).asLong(Integer.MAX_VALUE);
                    } else {
                        min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                        max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    }
                    NumberItem numberItem = new NumberItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        long defaultValue = attributeDescription.get(DEFAULT).asLong();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case DOUBLE:
                {
                    long min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                    long max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    NumberDoubleItem numberItem = new NumberDoubleItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        double defaultValue = attributeDescription.get(DEFAULT).asDouble();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case LIST:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                        if (!allowedValues.isEmpty()) {
                            MultiSelectBoxItem multiSelectBoxItem = new MultiSelectBoxItem(name, label, allowedValues);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    multiSelectBoxItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = multiSelectBoxItem;
                        } else {
                            ListItem listItem = new ListItem(name, label);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    listItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = listItem;
                            checkCapabilityReference(attributeDescription, formItem);
                        }
                    } else if (isSimpleTuple(attributeDescription)) {
                        // process OBJECT type attribute if all of its subattributes are simple types
                        formItem = new TuplesListItem(name, label, metadata.forComplexAttribute(property.getName()));
                    } else {
                        logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                        break;
                    }
                    break;
                }
            case OBJECT:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        PropertiesItem propertiesItem = new PropertiesItem(name, label);
                        List<Property> properties = ModelNodeHelper.getOrDefault(attributeDescription, DEFAULT, () -> attributeDescription.get(DEFAULT).asPropertyList(), emptyList());
                        if (!properties.isEmpty()) {
                            Map<String, String> defaultValues = new HashMap<>();
                            for (Property p : properties) {
                                defaultValues.put(p.getName(), p.getValue().asString());
                            }
                            propertiesItem.assignDefaultValue(defaultValues);
                        }
                        formItem = propertiesItem;
                    }
                    break;
                }
            case STRING:
                {
                    List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                    if (allowedValues.isEmpty()) {
                        FormItem<String> textBoxItem = new TextBoxItem(name, label, null);
                        boolean sensitive = failSafeGet(attributeDescription, ACCESS_CONSTRAINTS + "/" + SENSITIVE).isDefined();
                        if (PASSWORD.equals(name) || sensitive) {
                            textBoxItem.mask();
                        }
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            textBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = textBoxItem;
                        checkCapabilityReference(attributeDescription, formItem);
                    } else {
                        SingleSelectBoxItem singleSelectBoxItem = new SingleSelectBoxItem(name, label, allowedValues, !required);
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            singleSelectBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = singleSelectBoxItem;
                    }
                    break;
                }
            // unsupported types
            case BIG_DECIMAL:
            case BYTES:
            case EXPRESSION:
            case PROPERTY:
            case TYPE:
            case UNDEFINED:
                logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                break;
            default:
                break;
        }
        if (formItem != null) {
            formItem.setRequired(required);
            formItem.setDeprecated(deprecation);
            if (formItem.supportsExpressions()) {
                formItem.setExpressionAllowed(expressionAllowed);
                formItem.addResolveExpressionHandler(event -> {
                    // resend as application event
                    Core.INSTANCE.eventBus().fireEvent(event);
                });
            }
            if (readOnly) {
                formItem.setEnabled(false);
                // if the attribute is read-only and required, the form validation prevents to save the form
                // remove the required constraint to allow the save operation.
                formItem.setRequired(false);
            }
        }
    }
    return formItem;
}
Also used : TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) SingleSelectBoxItem(org.jboss.hal.ballroom.form.SingleSelectBoxItem) FormItem(org.jboss.hal.ballroom.form.FormItem) NumberItem(org.jboss.hal.ballroom.form.NumberItem) MultiSelectBoxItem(org.jboss.hal.ballroom.form.MultiSelectBoxItem) PropertiesItem(org.jboss.hal.ballroom.form.PropertiesItem) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) Deprecation(org.jboss.hal.dmr.Deprecation) ModelType(org.jboss.hal.dmr.ModelType) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NumberDoubleItem(org.jboss.hal.ballroom.form.NumberDoubleItem) ListItem(org.jboss.hal.ballroom.form.ListItem) TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) ModelNode(org.jboss.hal.dmr.ModelNode) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.jboss.hal.dmr.Property) SwitchItem(org.jboss.hal.ballroom.form.SwitchItem)

Aggregations

FormItem (org.jboss.hal.ballroom.form.FormItem)16 ModelNode (org.jboss.hal.dmr.ModelNode)12 Form (org.jboss.hal.ballroom.form.Form)9 Map (java.util.Map)7 List (java.util.List)6 ModelDescriptionConstants (org.jboss.hal.dmr.ModelDescriptionConstants)6 Operation (org.jboss.hal.dmr.Operation)6 Collectors.toList (java.util.stream.Collectors.toList)5 NamedNode (org.jboss.hal.dmr.NamedNode)5 ResourceAddress (org.jboss.hal.dmr.ResourceAddress)5 EventBus (com.google.web.bindery.event.shared.EventBus)4 NameToken (com.gwtplatform.mvp.client.annotations.NameToken)4 ProxyCodeSplit (com.gwtplatform.mvp.client.annotations.ProxyCodeSplit)4 ProxyPlace (com.gwtplatform.mvp.client.proxy.ProxyPlace)4 HashMap (java.util.HashMap)4 Inject (javax.inject.Inject)4 Finder (org.jboss.hal.core.finder.Finder)4 FinderPath (org.jboss.hal.core.finder.FinderPath)4 FinderPathFactory (org.jboss.hal.core.finder.FinderPathFactory)4 ModelNodeForm (org.jboss.hal.core.mbui.form.ModelNodeForm)4